1212use Ajax \service \JArray ;
1313use Ajax \semantic \widgets \base \InstanceViewer ;
1414use Ajax \semantic \html \collections \table \traits \TableTrait ;
15- use Ajax \semantic \html \elements \HtmlLabel ;
1615
1716/**
1817 * DataTable widget for displaying list of objects
2221 *
2322 */
2423class DataTable extends Widget {
25- use TableTrait,DataTableFieldAsTrait;
24+ use TableTrait,DataTableFieldAsTrait,HasCheckboxesTrait ;
2625 protected $ _searchField ;
2726 protected $ _urls ;
2827 protected $ _pagination ;
29- protected $ _hasCheckboxes ;
3028 protected $ _compileParts ;
3129 protected $ _deleteBehavior ;
3230 protected $ _editBehavior ;
3331 protected $ _visibleHover =false ;
34- protected $ _hasCheckedMessage =false ;
3532 protected $ _targetSelector ;
36- protected $ _checkedMessage ;
37- protected $ _checkedClass ;
33+
3834
3935 public function __construct ($ identifier ,$ model ,$ modelInstance =NULL ) {
4036 parent ::__construct ($ identifier , $ model ,$ modelInstance );
@@ -57,26 +53,7 @@ public function run(JsUtils $js){
5753 return parent ::run ($ js );
5854 }
5955
60- protected function _runCheckboxes (JsUtils $ js ){
61- $ checkedMessageCall ="" ;
62- if ($ this ->_hasCheckedMessage ){
63- $ msg =$ this ->getCheckedMessage ();
64- $ checkedMessageFunction ="function updateChecked(){var msg=' " .$ msg [0 ]."',count= \$('# {$ this ->identifier } [name= \"selection[] \"]:checked').length,all= \$('# {$ this ->identifier } [name= \"selection[] \"]').length;
65- if(count==1) msg=' " .$ msg [1 ]."';
66- else if(count>1) msg=' " .$ msg ["other " ]."';
67- \$('#checked-count- " .$ this ->identifier ."').contents().filter(function() {return this.nodeType == 3;}).each(function(){this.textContent = msg.replace('{count}',count);});
68- \$('#toolbar- {$ this ->identifier } .visibleOnChecked').toggle(count>0);} \$('#toolbar- " .$ this ->identifier ." .visibleOnChecked').hide(); " ;
69- $ checkedMessageCall ="updateChecked(); " ;
70- if (isset ($ this ->_checkedClass )){
71- $ checkedMessageCall .="$(this).closest('tr').toggleClass(' " .$ this ->_checkedClass ."',$(this).prop('checked')); " ;
72- }
73- $ js ->exec ($ checkedMessageFunction ,true );
74- }
75- $ js ->execOn ("change " , "# " .$ this ->identifier ." [name='selection[]'] " , "
76- var \$parentCheckbox= \$('#ck-main-ck- {$ this ->identifier }'), \$checkbox= \$('# {$ this ->identifier } [name= \"selection[] \"]'),allChecked=true,allUnchecked=true;
77- \$checkbox.each(function() {if($(this).prop('checked')){allUnchecked = false;}else{allChecked = false;}});
78- if(allChecked) { \$parentCheckbox.checkbox('set checked');}else if(allUnchecked){ \$parentCheckbox.checkbox('set unchecked');}else{ \$parentCheckbox.checkbox('set indeterminate');}; " .$ checkedMessageCall );
79- }
56+
8057
8158 protected function _generateBehavior ($ op ,$ params ,JsUtils $ js ){
8259 if (isset ($ this ->_urls [$ op ])){
@@ -134,24 +111,7 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
134111 return parent ::compile ($ js ,$ view );
135112 }
136113
137- private function _generateMainCheckbox (&$ captions ){
138- $ ck =new HtmlCheckbox ("main-ck- " .$ this ->identifier ,"" );
139- $ checkedMessageCall ="" ;
140- if ($ this ->_hasCheckedMessage )
141- $ checkedMessageCall ="updateChecked(); " ;
142-
143- $ ck ->setOnChecked ($ this ->_setAllChecked ("true " ).$ checkedMessageCall );
144- $ ck ->setOnUnchecked ($ this ->_setAllChecked ("false " ).$ checkedMessageCall );
145- \array_unshift ($ captions , $ ck );
146- }
147114
148- private function _setAllChecked ($ checked ){
149- $ result ="$('# " .$ this ->identifier ." [name=%quote%selection[]%quote%]').prop('checked', " .$ checked ."); " ;
150- if (isset ($ this ->_checkedClass )){
151- $ result .="$('# " .$ this ->identifier ." tr').toggleClass(' " .$ this ->_checkedClass ."', " .$ checked ."); " ;
152- }
153- return $ result ;
154- }
155115
156116 protected function _generateContent ($ table ){
157117 $ objects =$ this ->_modelInstance ;
@@ -263,14 +223,7 @@ public function paginate($items_per_page=10,$page=1){
263223 $ this ->_pagination =new Pagination ($ items_per_page ,4 ,$ page );
264224 }
265225
266- public function getHasCheckboxes () {
267- return $ this ->_hasCheckboxes ;
268- }
269226
270- public function setHasCheckboxes ($ _hasCheckboxes ) {
271- $ this ->_hasCheckboxes =$ _hasCheckboxes ;
272- return $ this ;
273- }
274227
275228 public function refresh ($ compileParts =["tbody " ]){
276229 $ this ->_compileParts =$ compileParts ;
@@ -323,42 +276,4 @@ public function setTargetSelector($_targetSelector) {
323276 $ this ->_targetSelector =$ _targetSelector ;
324277 return $ this ;
325278 }
326-
327- protected function getCheckedMessage () {
328- $ result = $ this ->_checkedMessage ;
329- if (!isset ($ result )){
330- $ result =[0 =>"none selected " ,1 =>"one item selected " ,"other " =>"{count} items selected " ];
331- }
332- return $ result ;
333- }
334-
335- /**
336- * Defines the message displayed when checkboxes are checked or unchecked
337- * with an associative array 0=>no selection,1=>one item selected, other=>{count} items selected
338- * @param array $_checkedMessage
339- * @return \Ajax\semantic\widgets\datatable\DataTable
340- */
341- public function setCheckedMessage (array $ _checkedMessage ) {
342- $ this ->_checkedMessage =$ _checkedMessage ;
343- return $ this ;
344- }
345-
346- /**
347- * @param array $checkedMessage
348- * @param callable $callback
349- */
350- public function addCountCheckedInToolbar (array $ checkedMessage =null ,$ callback =null ){
351- if (isset ($ checkedMessage ))
352- $ this ->_checkedMessage =$ checkedMessage ;
353- $ checkedMessage =$ this ->getCheckedMessage ();
354- $ this ->_hasCheckboxes =true ;
355- $ this ->_hasCheckedMessage =true ;
356- $ element =new HtmlLabel ("checked-count- " .$ this ->identifier ,$ checkedMessage [0 ]);
357- $ this ->addInToolbar ($ element ,$ callback );
358- }
359-
360- public function setCheckedClass ($ _checkedClass ) {
361- $ this ->_checkedClass =$ _checkedClass ;
362- return $ this ;
363- }
364279}
0 commit comments