1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Tools; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
use Encore\Admin\Grid; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
class ColumnSelector extends AbstractTool |
10
|
|
|
{ |
11
|
|
|
const SELECT_COLUMN_NAME = '_columns_'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var Grid |
15
|
|
|
*/ |
16
|
|
|
protected $grid; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create a new Export button instance. |
20
|
|
|
* |
21
|
|
|
* @param Grid $grid |
22
|
|
|
*/ |
23
|
|
|
public function __construct(Grid $grid) |
24
|
|
|
{ |
25
|
|
|
$this->grid = $grid; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function render() |
34
|
|
|
{ |
35
|
|
|
$show = array_filter(explode(',', request(static::SELECT_COLUMN_NAME))); |
36
|
|
|
|
37
|
|
|
$columns = $this->getGridColumns(); |
38
|
|
|
|
39
|
|
|
$this->setupScript($show, $columns); |
40
|
|
|
|
41
|
|
|
$lists = $columns->map(function ($val, $key) use ($show) { |
42
|
|
|
|
43
|
|
|
if (empty($show)) { |
44
|
|
|
$checked = 'checked'; |
45
|
|
|
} else { |
46
|
|
|
$checked = in_array($key, $show) ? 'checked' : ''; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return "<li><a href=\"#\" data-value=\"{$key}\" tabIndex=\"-1\"><input type=\"checkbox\" {$checked}/> {$val}</a></li>"; |
50
|
|
|
})->implode("\r\n"); |
51
|
|
|
|
52
|
|
|
return <<<EOT |
53
|
|
|
|
54
|
|
|
<div class="dropdown pull-right column-selector" style="margin-right: 10px"> |
55
|
|
|
<button type="button" class="btn btn-sm btn-instagram dropdown-toggle" data-toggle="dropdown"> |
56
|
|
|
<i class="fa fa-table"></i> |
57
|
|
|
|
58
|
|
|
<span class="caret"></span> |
59
|
|
|
</button> |
60
|
|
|
<ul class="dropdown-menu" role="menu"> |
61
|
|
|
{$lists} |
62
|
|
|
<li class="divider"></li> |
63
|
|
|
<li style="padding: 0 15px;"> |
64
|
|
|
<button class="btn btn-xs btn-instagram column-select-all">全选</button> |
65
|
|
|
<button class="btn btn-xs btn-primary column-select-submit" style="float: right">确定</button> |
66
|
|
|
</li> |
67
|
|
|
</ul> |
68
|
|
|
</div> |
69
|
|
|
EOT; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return Collection |
74
|
|
|
*/ |
75
|
|
|
protected function getGridColumns() |
76
|
|
|
{ |
77
|
|
|
return $this->grid->columns()->map(function (Grid\Column $column) { |
78
|
|
|
|
79
|
|
|
$name = $column->getName(); |
80
|
|
|
|
81
|
|
|
if (in_array($name, ['__row_selector__', '__actions__'])) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return [$name => $column->getLabel()]; |
86
|
|
|
})->filter()->collapse(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $show |
91
|
|
|
* @param $columns |
92
|
|
|
*/ |
93
|
|
|
protected function setupScript($show, $columns) |
94
|
|
|
{ |
95
|
|
|
if (empty($show)) { |
96
|
|
|
$show = $columns->keys()->toArray(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$show = json_encode($show); |
100
|
|
|
|
101
|
|
|
$script = <<<SCRIPT |
102
|
|
|
|
103
|
|
|
var selected_columns = {$show}; |
104
|
|
|
|
105
|
|
|
$('.column-selector .dropdown-menu a').on('click', function(event) { |
106
|
|
|
|
107
|
|
|
var \$target = $( event.currentTarget ), |
108
|
|
|
val = \$target.attr('data-value'), |
109
|
|
|
\$inp = \$target.find('input'), |
110
|
|
|
idx; |
111
|
|
|
|
112
|
|
|
if ((idx = selected_columns.indexOf(val)) > -1) { |
113
|
|
|
selected_columns.splice(idx, 1); |
114
|
|
|
setTimeout(function() {\$inp.prop('checked', false)}, 0); |
115
|
|
|
} else { |
116
|
|
|
selected_columns.push(val); |
117
|
|
|
setTimeout(function() {\$inp.prop('checked', true)}, 0); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$(event.target).blur(); |
121
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
}); |
124
|
|
|
|
125
|
|
|
$('.column-select-submit').on('click', function () { |
126
|
|
|
if (selected_columns.length == 0) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
var url = new URL(location); |
131
|
|
|
|
132
|
|
|
// select all |
133
|
|
|
if ($('.column-selector .dropdown-menu a input').length == selected_columns.length) { |
134
|
|
|
url.searchParams.delete('_columns_'); |
135
|
|
|
} else { |
136
|
|
|
url.searchParams.set('_columns_', selected_columns.join()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$.pjax({container:'#pjax-container', url: url.toString() }); |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
$('.column-select-all').on('click', function () { |
143
|
|
|
selected_columns = []; |
144
|
|
|
$('.column-selector .dropdown-menu a input').prop('checked', true); |
145
|
|
|
$('.column-selector .dropdown-menu a').each(function (_, val) { |
146
|
|
|
selected_columns.push($(val).data('value')); |
147
|
|
|
}); |
148
|
|
|
|
149
|
|
|
return false; |
150
|
|
|
}); |
151
|
|
|
|
152
|
|
|
SCRIPT; |
153
|
|
|
|
154
|
|
|
Admin::script($script); |
155
|
|
|
} |
156
|
|
|
} |