GridFilterTrait.php
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 自定义 grid filter 字段
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Traits
* @package App\Admin\Traits
* @author Richer <yangzi1028@163.com>
* @date 2021年11月11日14:15:12
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Traits;
use App\Models\Area\Area;
use App\Models\System\SystemConfig;
use Carbon\Carbon;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Arr;
/**
* Trait GridFilterTrait
*
* @category App\Admin\Traits
* @package App\Admin\Traits
* @author Richer <yangzi1028@163.com>
* @date 2021年11月11日14:15:12
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
trait GridFilterTrait
{
/**
* 用户筛选
*
* @param $filter
* @param string $relation
*/
public function filterUser($filter, $relation = '')
{
$nickname = 'nickname';
$mobile = 'mobile';
$relation && $nickname = $relation. '.'.$nickname;
$relation && $mobile = $relation. '.'.$mobile;
// $nickname = $relation ? $relation. '.'.$nickname : $nickname;
// $mobile = $relation ? $relation. '.'.$mobile : $mobile;
// $nickname = $relation ? $relation .'.nickname' : 'nickname'; ;
// $mobile = $relation ? $relation .'.mobile' : 'mobile';;
$this->filterText($filter, $nickname, __('nickname'));
$this->filterText($filter, $mobile, __('mobile'));
}
/**
* 文本筛选
*
* @param $filter
* @param $field
* @param $comment
*/
public function filterText($filter, $field, $comment = '')
{
if (!$comment) {
$comment = __($field);
}
$filter->like($field, $comment);
}
/**
* 数字文本 筛选
*
* @param $filter
* @param $key
*/
public function filterNumber($filter, $key)
{
$filter->like($key, __($key))->integer();
}
/**
* 单选筛选
*
* @param $filter
* @param $key
* @param string $comment
* @param array $options
*/
public function filterSingleSelect($filter, $key, $comment = '', $options = [])
{
if (!$options) {
// $options = SystemConfig::getSelectOptions($key);
}
if (!$comment) {
$comment = __($key);
}
foreach ($options as &$option) {
$option = __($option);
}
$filter->equal($key)->select($options)->placeholder($comment);
}
/**
* 日期 筛选
*
* @param $filter
* @param $key
* @param string $comment
* @param string $function
* @param bool $range
*/
public function filterDate($filter, $key, $comment = '', $function = 'date', $range = true)
{
if (!$comment) {
$comment = __($key);
}
if ($range) {
$filter->between($key, $comment)->$function();
} else {
$filter->equal($key, $comment)->$function();
}
}
}