1- <?php
2- /**
3- * @copyright Copyright (c) 2018 Herve Guenot
4- * @license MIT
5- * @author Herve Guenot <[email protected] > 6- */
7- namespace nullref \ datatable ;
8-
9- use yii \ base \ Widget ;
10- use yii \helpers \ Html ;
11- use yii \helpers \Inflector ;
12- use yii \web \ JsExpression ;
13-
14- class DataTableColumn extends Widget
15- {
16- /**
17- * @var string the attribute name associated with this column.
18- */
19- public $ data ;
20-
21- /**
22- * @var string label to be displayed in the header.
23- * If it is not set [[\yii\helpers\Inflector::camel2words()]] will be used to get a label .
24- */
25- public $ title ;
26-
27- /**
28- * @var array|null|false Indicating if a filter will be displayed or not.
29- *
30- * - If this property is not set, a text field will be generated as the filter input with attributes defined
31- * with [[filterInputOptions]].
32- * - If this property is an array, a dropdown list will be generated that uses this property value as
33- * the list options .
34- * - If you don't want a filter for this data column, set this value to be false.
35- */
36- private $ filter ;
37-
38- /**
39- * @var array the HTML attributes for the filter input fields. This property is used in combination with
40- * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to
41- * render the HTML attributes for the generated filter input fields.
42- *
43- * Empty `id` in the default value ensures that id would not be obtained from the model attribute thus
44- * providing better performance.
45- *
46- * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered .
47- */
48- public $ filterInputOptions = [ ' class ' => ' form-control ' , ' id ' => null ];
49-
50- /** @var JsExpression Javascript (function or expression) used to display the filter */
51- public $ renderFilter ;
52-
53- /** @var JsExpression Javascript ( function) used to display the value. */
54- public $ render ;
55-
56- public function init () {
57- parent :: init ();
58-
59- if ( $ this -> data === null ) {
60- throw new InvalidConfigException ( get_class ( $ this ) . ' ::$data must be set. ' );
61- }
62-
63- if ( $ this -> title === null ) {
64- $ this -> title = Inflector:: camel2words ( $ this -> attribute );
65- }
66-
67- if ( $ this -> render === null ) {
68- $ this -> render = $ this -> getJsRender ();
69- }
70-
71- if ( $ this -> renderFilter === null ) {
72- $ this -> renderFilter = $ this -> getJsFilter ();
73- }
74- }
75-
76- public function setAttribute ( $ attribute )
77- {
78- $ this ->data = $ attribute ;
79- }
80-
81- public function getAttribute ()
82- {
83- return $ this -> data ;
84- }
85-
86- public function setLabel ( $ label )
87- {
88- $ this -> title = $ label ;
89- }
90-
91- public function getLabel ()
92- {
93- return $ this -> title ;
94- }
95-
96- /**
97- * @return array|false|null
98- */
99- public function getFilter () {
100- return $ this ->filter ;
101- }
102-
103- /**
104- * @param array|false|null $filter
105- */
106- public function setFilter ( $ filter ): void {
107- $ this -> filter = $ filter ;
108- }
109-
110- public function getJsFilter ()
111- {
112- $ jsTitle = Html:: encode ( $ this -> label );
113- $ jsClass = Html:: encode ( $ this -> filterInputOptions [ ' class ' ]);
114- $ jsId = $ this -> filterInputOptions [ ' id ' ] ? Html:: encode ( $ this -> filterInputOptions [ ' id ' ]) : $ this -> getId () ;
115- if ( is_array ( $ this -> filter )) {
116- $ select = " jQuery('<select type= \" text \" placeholder= \" Search { $ jsTitle }\" ></select>') \n" .
117- "\t .addClass(' { $ jsClass } ') \n" .
118- "\t .width('100%') \n" .
119- "\t .attr('id', ' { $ jsId } ') \n" .
120- "\t .append(jQuery('<option value= \"\" ></option>')) " ;
121-
122- foreach ( $ this -> filter as $ key => $ value ) {
123- $ key = Html:: encode ( $ key );
124- $ value = Html:: encode ( $ value );
125- $ select .= "\n\t .append(jQuery('<option></option>', { \n\t\t "
126- . " 'value': serverSide ? ' { $ key } ' : ' { $ value } ', \n\t\t"
127- . " 'text': ' { $ value } ' \n\t"
128- . " })) " ;
129- }
130-
131- return new JsExpression ( " function(table) { var serverSide = table.page.info().serverSide; return { $ select } ; } " );
132- } else if ( $ this -> filter !== false ) {
133- return new JsExpression (
134- " function() { " .
135- " return jQuery('<input type= \" text \" placeholder= \" Search { $ jsTitle }\" />') \n" .
136- "\t .addClass(' { $ jsClass } ') \n" .
137- "\t .width('100%') \n" .
138- "\t .attr('id', ' { $ jsId } '); \n" .
139- " } "
140- );
141- } else {
142- return new JsExpression ( ' jQuery() ' );
143- }
144- }
145-
146- public function getJsRender ()
147- {
148- $ jsTitle = Html:: encode ( $ this -> label );
149- $ jsClass = Html:: encode ( $ this ->filterInputOptions [ ' class ' ]) ;
150- $ jsId = $ this -> filterInputOptions [ ' id ' ] ? Html:: encode ( $ this -> filterInputOptions [ ' id ' ]) : $ this -> getId ();
151- if ( is_array ( $ this -> filter )) {
152- $ select = " switch (data) { " ;
153-
154- foreach ( $ this -> filter as $ key => $ value ) {
155- $ key = Html:: encode ( $ key );
156- $ value = Html:: encode ( $ value );
157- $ select .= "\n\t case ' { $ key } ': return ' { $ value } '; " ;
158- }
159- $ select .= "\n\t default: return data; " ;
160- $ select .= "\n } " ;
161-
162- return new JsExpression ( " function render(data, type, row, meta) { { $ select } } " );
163- } else {
164- return new JsExpression ( " function render(data, type, row, meta){ return data; } " );
165- }
166- }
167-
1+ <?php
2+ /**
3+ * @copyright Copyright (c) 2018 Herve Guenot
4+ * @license MIT
5+ * @author Herve Guenot <[email protected] > 6+ */
7+
8+ namespace nullref \ datatable ;
9+
10+ use yii \base \ Widget ;
11+ use yii \helpers \Html ;
12+ use yii \helpers \ Inflector ;
13+ use yii \ web \ JsExpression ;
14+
15+ class DataTableColumn extends Widget
16+ {
17+ /**
18+ * @var string the attribute name associated with this column.
19+ */
20+ public $ data ;
21+
22+ /**
23+ * @var string label to be displayed in the header .
24+ * If it is not set [[\yii\helpers\Inflector::camel2words()]] will be used to get a label.
25+ */
26+ public $ title ;
27+ /**
28+ * @var array the HTML attributes for the filter input fields. This property is used in combination with
29+ * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to
30+ * render the HTML attributes for the generated filter input fields.
31+ *
32+ * Empty `id` in the default value ensures that id would not be obtained from the model attribute thus
33+ * providing better performance .
34+ *
35+ * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
36+ */
37+ public $ filterInputOptions = [ ' class ' => ' form-control ' , ' id ' => null ];
38+ /** @var JsExpression Javascript (function or expression) used to display the filter */
39+ public $ renderFilter ;
40+ /** @var JsExpression Javascript (function) used to display the value. */
41+ public $ render;
42+ /* *
43+ * @var array|null|false Indicating if a filter will be displayed or not.
44+ *
45+ * - If this property is not set, a text field will be generated as the filter input with attributes defined
46+ * with [[filterInputOptions]] .
47+ * - If this property is an array, a dropdown list will be generated that uses this property value as
48+ * the list options.
49+ * - If you don't want a filter for this data column, set this value to be false.
50+ */
51+ private $ filter ;
52+
53+ public function init ()
54+ {
55+ parent :: init ();
56+
57+ if ( $ this -> data === null ) {
58+ throw new InvalidConfigException ( get_class ( $ this ) . ' ::$data must be set. ' );
59+ }
60+
61+ if ( $ this -> title === null ) {
62+ $ this -> title = Inflector:: camel2words ( $ this -> attribute );
63+ }
64+
65+ if ( $ this -> render === null ) {
66+ $ this -> render = $ this -> getJsRender ();
67+ }
68+
69+ if ( $ this -> renderFilter === null ) {
70+ $ this -> renderFilter = $ this -> getJsFilter ();
71+ }
72+ }
73+
74+ public function getJsRender ()
75+ {
76+ $ jsTitle = Html:: encode ( $ this -> label );
77+ $ jsClass = Html:: encode ( $ this -> filterInputOptions [ ' class ' ]);
78+ $ jsId = $ this ->filterInputOptions [ ' id ' ] ? Html:: encode ( $ this -> filterInputOptions [ ' id ' ]) : $ this -> getId () ;
79+ if ( is_array ( $ this -> filter )) {
80+ $ select = " switch (data) { " ;
81+
82+ foreach ( $ this -> filter as $ key => $ value ) {
83+ $ key = Html:: encode ( $ key ) ;
84+ $ value = Html:: encode ( $ value );
85+ $ select .= "\n\t case ' { $ key } ': return ' { $ value } '; " ;
86+ }
87+ $ select .= "\n\t default: return data; " ;
88+ $ select .= "\n } " ;
89+
90+ return new JsExpression ( " function render(data, type, row, meta) { { $ select } } " );
91+ } else {
92+ return new JsExpression ( " function render(data, type, row, meta){ return data; } " );
93+ }
94+ }
95+
96+ public function getJsFilter ()
97+ {
98+ $ jsTitle = Html:: encode ( $ this -> label );
99+ $ jsClass = Html:: encode ( $ this -> filterInputOptions [ ' class ' ]);
100+ $ jsId = $ this ->filterInputOptions [ ' id ' ] ? Html:: encode ( $ this -> filterInputOptions [ ' id ' ]) : $ this -> getId () ;
101+ if ( is_array ( $ this -> filter )) {
102+ $ select = " jQuery('<select type= \" text \" placeholder= \" Search { $ jsTitle }\" ></select>') \n" .
103+ "\t .addClass(' { $ jsClass } ') \n" .
104+ "\t .width('100%') \n" .
105+ "\t .attr('id', ' { $ jsId } ') \n" .
106+ "\t .append(jQuery('<option value= \"\" ></option>')) " ;
107+
108+ foreach ( $ this -> filter as $ key => $ value ) {
109+ $ key = Html:: encode ( $ key );
110+ $ value = Html:: encode ( $ value );
111+ $ select .= "\n\t .append(jQuery('<option></option>', { \n\t\t"
112+ . " 'value': serverSide ? ' { $ key } ' : ' { $ value } ', \n\t\t"
113+ . " 'text': ' { $ value } ' \n\t"
114+ . " })) " ;
115+ }
116+
117+ return new JsExpression ( " function(table) { var serverSide = table.page.info().serverSide; return { $ select } ; } " );
118+ } else if ( $ this -> filter !== false ) {
119+ return new JsExpression (
120+ " function() { " .
121+ " return jQuery('<input type= \" text \" placeholder= \" Search { $ jsTitle }\" />') \n" .
122+ "\t .addClass(' { $ jsClass } ') \n" .
123+ "\t .width('100%') \n" .
124+ "\t .attr('id', ' { $ jsId } '); \n" .
125+ " } "
126+ );
127+ } else {
128+ return new JsExpression ( ' jQuery() ' ) ;
129+ }
130+ }
131+
132+ public function setAttribute ( $ attribute )
133+ {
134+ $ this -> data = $ attribute ;
135+ }
136+
137+ public function getAttribute ()
138+ {
139+ return $ this -> data ;
140+ }
141+
142+ public function setLabel ( $ label )
143+ {
144+ $ this -> title = $ label ;
145+ }
146+
147+ public function getLabel ()
148+ {
149+ return $ this ->title ;
150+ }
151+
152+ /**
153+ * @return array|false|null
154+ */
155+ public function getFilter ()
156+ {
157+ return $ this -> filter ;
158+ }
159+
160+ /**
161+ * @param array|false|null $filter
162+ */
163+ public function setFilter ( $ filter ): void
164+ {
165+ $ this -> filter = $ filter ;
166+ }
167+
168168}
0 commit comments