1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\GridBundle\Grid\Renderer; |
4
|
|
|
|
5
|
|
|
use Dtc\GridBundle\Grid\Column\AbstractGridColumn; |
6
|
|
|
|
7
|
|
|
class DataTablesRenderer extends AbstractJqueryRenderer |
8
|
|
|
{ |
9
|
|
|
public static $defaultOptions = [ |
10
|
|
|
'processing' => true, |
11
|
|
|
'searchDelay' => 350, |
12
|
|
|
'table_attr' => [ |
13
|
|
|
'class' => 'display table table-striped table-bordered small-font', |
14
|
|
|
], |
15
|
|
|
'serverSide' => true, |
16
|
|
|
'language' => [ |
17
|
|
|
'lengthMenu' => '_MENU_ records per page', |
18
|
|
|
], |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
protected $dataTablesCss = []; |
22
|
|
|
protected $dataTablesJs = []; |
23
|
|
|
private $localCss = []; |
24
|
|
|
private $localJs = []; |
25
|
|
|
|
26
|
|
|
const MODE_AJAX = 1; |
27
|
|
|
const MODE_SERVER = 2; |
28
|
|
|
|
29
|
|
|
protected $mode = 1; |
30
|
|
|
|
31
|
|
|
public function setMode($mode) |
32
|
|
|
{ |
33
|
|
|
$this->mode = $mode; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the type (bootstrap, bootstrap4, foundation, etc.). |
38
|
|
|
* |
39
|
|
|
* @param $type |
40
|
|
|
*/ |
41
|
|
|
public function setDataTablesCss($css) |
42
|
|
|
{ |
43
|
|
|
$this->dataTablesCss = $css; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getDataTablesCss() |
47
|
|
|
{ |
48
|
|
|
return $this->dataTablesCss; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setDataTablesJs($js) |
52
|
|
|
{ |
53
|
|
|
$this->dataTablesJs = $js; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getDataTablesJs() |
57
|
|
|
{ |
58
|
|
|
return $this->dataTablesJs; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setDataTablesLocalCss(array $localCss) |
62
|
|
|
{ |
63
|
|
|
$this->localCss = $localCss; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setDataTablesLocalJs(array $localJs) |
67
|
|
|
{ |
68
|
|
|
$this->localJs = $localJs; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getDataTablesLocalCss() |
72
|
|
|
{ |
73
|
|
|
return $this->localCss; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getDataTablesLocalJs() |
77
|
|
|
{ |
78
|
|
|
return $this->localJs; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setDataTablesClass($class) |
82
|
|
|
{ |
83
|
|
|
$this->options['table_attr']['class'] = $class; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getDataTablesClass() |
87
|
|
|
{ |
88
|
|
|
return isset($this->options['table_attr']['class']) ? $this->options['table_attr']['class'] : null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getParams(array &$params = null) |
92
|
|
|
{ |
93
|
|
|
if (null === $params) { |
94
|
|
|
$params = []; |
95
|
|
|
} |
96
|
|
|
parent::getParams($params); |
97
|
|
|
$params['dtc_grid_datatables_css'] = $this->dataTablesCss; |
98
|
|
|
$params['dtc_grid_datatables_js'] = $this->dataTablesJs; |
99
|
|
|
$params['dtc_grid_local_css'] = $this->localCss; |
100
|
|
|
$params['dtc_grid_local_js'] = $this->localJs; |
101
|
|
|
|
102
|
|
|
return $params; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function afterBind() |
106
|
|
|
{ |
107
|
|
|
$id = $this->gridSource->getDivId(); |
108
|
|
|
$this->options['pager'] = "{$id}-pager"; |
109
|
|
|
|
110
|
|
|
$fields = array_keys($this->gridSource->getColumns()); |
111
|
|
|
|
112
|
|
|
// We need to pass filter information here. |
113
|
|
|
$params = [ |
114
|
|
|
'id' => $this->gridSource->getId(), |
115
|
|
|
'renderer' => 'datatables', |
116
|
|
|
'filter' => $this->gridSource->getFilter(), |
117
|
|
|
'parameters' => $this->gridSource->getParameters(), |
118
|
|
|
'order' => $this->gridSource->getOrderBy(), |
119
|
|
|
'fields' => $fields, |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
$sortInfo = $this->gridSource->getDefaultSort(); |
123
|
|
|
$defaultSortColumn = isset($sortInfo['column']) ? $sortInfo['column'] : null; |
124
|
|
|
$defaultSortDirection = isset($sortInfo['direction']) ? $sortInfo['direction'] : 'ASC'; |
125
|
|
|
$defaultSortDirection = strtolower($defaultSortDirection); |
126
|
|
|
$defaultSortColumnIdx = 0; |
127
|
|
|
|
128
|
|
|
$url = $this->router->generate('dtc_grid_data', $params); |
129
|
|
|
$this->options['sAjaxSource'] = $url; |
130
|
|
|
|
131
|
|
|
$columnsDef = []; |
132
|
|
|
/** @var AbstractGridColumn $column */ |
133
|
|
|
$idx = 0; |
134
|
|
|
foreach ($this->gridSource->getColumns() as $index => $column) { |
135
|
|
|
$info = []; |
136
|
|
|
$name = $column->getField(); |
137
|
|
|
$info['bSortable'] = $column->getOption('sortable') ? true : false; |
138
|
|
|
$info['sName'] = $name; |
139
|
|
|
|
140
|
|
|
if ($width = $column->getOption('width')) { |
141
|
|
|
$info['sWidth'] = $width; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$info['aTargets'] = [$index]; |
145
|
|
|
$info = array_merge($info, $column->getOptions()); |
146
|
|
|
$columnsDef[] = $info; |
147
|
|
|
if ($index === $defaultSortColumn) { |
148
|
|
|
$defaultSortColumnIdx = $idx; |
149
|
|
|
} |
150
|
|
|
++$idx; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->options['order'] = [[$defaultSortColumnIdx, $defaultSortDirection]]; |
154
|
|
|
$this->options['aoColumns'] = $columnsDef; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getData() |
158
|
|
|
{ |
159
|
|
|
$columns = $this->gridSource->getColumns(); |
160
|
|
|
$gridSource = $this->gridSource; |
161
|
|
|
$records = $gridSource->getRecords(); |
162
|
|
|
$count = $gridSource->getCount(); |
163
|
|
|
|
164
|
|
|
$retVal = [ |
165
|
|
|
'page' => $gridSource->getPager() |
166
|
|
|
->getCurrentPage(), |
167
|
|
|
'total_pages' => $gridSource->getPager() |
168
|
|
|
->getTotalPages(), |
169
|
|
|
'iTotalRecords' => (int) $count, |
170
|
|
|
'iTotalDisplayRecords' => $count, |
171
|
|
|
'id' => $gridSource->getId(), // unique id |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
$data = []; |
175
|
|
|
foreach ($records as $record) { |
176
|
|
|
$info = []; |
177
|
|
|
/** @var AbstractGridColumn $column */ |
178
|
|
|
foreach ($columns as $column) { |
179
|
|
|
if (method_exists($column, 'setRouter')) { |
180
|
|
|
$column->setRouter($this->router); |
181
|
|
|
} |
182
|
|
|
if (method_exists($column, 'setGridSourceId')) { |
183
|
|
|
$column->setGridSourceId($gridSource->getId()); |
184
|
|
|
} |
185
|
|
|
$info[] = $column->format($record, $gridSource); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$data[] = $info; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$retVal['aaData'] = $data; |
192
|
|
|
|
193
|
|
|
return $retVal; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function render() |
197
|
|
|
{ |
198
|
|
|
$id = $this->gridSource->getDivId(); |
199
|
|
|
|
200
|
|
|
$options = $this->options; |
201
|
|
|
unset($options['table_attr']); |
202
|
|
|
|
203
|
|
|
$params = [ |
204
|
|
|
'options' => $options, |
205
|
|
|
'table_attr' => $this->options['table_attr'], |
206
|
|
|
'columns' => $this->gridSource->getColumns(), |
207
|
|
|
'id' => $id, |
208
|
|
|
]; |
209
|
|
|
|
210
|
|
|
$template = '@DtcGrid/Grid/datatables.html.twig'; |
211
|
|
|
|
212
|
|
|
return $this->twig->render($template, $params); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|