1212use Ajax \service \JArray ;
1313use Ajax \semantic \widgets \base \InstanceViewer ;
1414use Ajax \semantic \html \collections \table \traits \TableTrait ;
15+ use Ajax \semantic \html \collections \HtmlMessage ;
1516
1617/**
1718 * DataTable widget for displaying list of objects
@@ -31,12 +32,17 @@ class DataTable extends Widget {
3132 protected $ _visibleHover =false ;
3233 protected $ _targetSelector ;
3334 protected $ _refreshSelector ;
35+ protected $ _emptyMessage ;
36+ protected $ _json ;
37+ protected $ _rowClass ="" ;
3438
3539
3640 public function __construct ($ identifier ,$ model ,$ modelInstance =NULL ) {
3741 parent ::__construct ($ identifier , $ model ,$ modelInstance );
3842 $ this ->_init (new InstanceViewer ($ identifier ), "table " , new HtmlTable ($ identifier , 0 ,0 ), false );
3943 $ this ->_urls =[];
44+ $ this ->_emptyMessage =new HtmlMessage ("" ,"nothing to display " );
45+ $ this ->_emptyMessage ->setIcon ("info circle " );
4046 }
4147
4248 public function run (JsUtils $ js ){
@@ -100,7 +106,7 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
100106 }
101107
102108 if (isset ($ this ->_pagination ) && $ this ->_pagination ->getVisible ()){
103- $ this ->_generatePagination ($ table );
109+ $ this ->_generatePagination ($ table, $ js );
104110 }
105111 if (isset ($ this ->_toolbar )){
106112 $ this ->_setToolbarPosition ($ table , $ captions );
@@ -119,32 +125,46 @@ protected function _generateContent($table){
119125 if (isset ($ this ->_pagination )){
120126 $ objects =$ this ->_pagination ->getObjects ($ this ->_modelInstance );
121127 }
122- InstanceViewer::setIndex (0 );
123- $ table ->fromDatabaseObjects ($ objects , function ($ instance ) use ($ table ){
124- $ this ->_instanceViewer ->setInstance ($ instance );
125- InstanceViewer::$ index ++;
126- $ values = $ this ->_instanceViewer ->getValues ();
127- if ($ this ->_hasCheckboxes ){
128- $ ck =new HtmlCheckbox ("ck- " .$ this ->identifier ,"" );
129- $ field =$ ck ->getField ();
130- $ field ->setProperty ("value " ,$ this ->_instanceViewer ->getIdentifier ());
131- $ field ->setProperty ("name " , "selection[] " );
132- \array_unshift ($ values , $ ck );
133- }
134- $ result =$ table ->newRow ();
135- $ result ->setIdentifier ($ this ->identifier ."-tr- " .$ this ->_instanceViewer ->getIdentifier ());
136- $ result ->setValues ($ values );
137- return $ result ;
138- });
128+ InstanceViewer::setIndex (0 );
129+ $ table ->fromDatabaseObjects ($ objects , function ($ instance ) use ($ table ){
130+ return $ this ->_generateRow ($ instance , $ table );
131+ });
132+ if ($ table ->getRowCount ()==0 ){
133+ $ result =$ table ->addRow ();
134+ $ result ->mergeRow ();
135+ $ result ->setValues ([$ this ->_emptyMessage ]);
136+ }
139137 }
140138
141- private function _generatePagination ($ table ){
139+ protected function _generateRow ($ instance ,&$ table ){
140+ $ this ->_instanceViewer ->setInstance ($ instance );
141+ InstanceViewer::$ index ++;
142+ $ values = $ this ->_instanceViewer ->getValues ();
143+ if ($ this ->_hasCheckboxes ){
144+ $ ck =new HtmlCheckbox ("ck- " .$ this ->identifier ,"" );
145+ $ field =$ ck ->getField ();
146+ $ field ->setProperty ("value " ,$ this ->_instanceViewer ->getIdentifier ());
147+ $ field ->setProperty ("name " , "selection[] " );
148+ \array_unshift ($ values , $ ck );
149+ }
150+ $ result =$ table ->newRow ();
151+ $ result ->setIdentifier ($ this ->identifier ."-tr- " .$ this ->_instanceViewer ->getIdentifier ());
152+ $ result ->setValues ($ values );
153+ $ result ->addToProperty ("class " ,$ this ->_rowClass );
154+ return $ result ;
155+ }
156+
157+ protected function _generatePagination ($ table ,$ js =NULL ){
142158 $ footer =$ table ->getFooter ();
143159 $ footer ->mergeCol ();
144160 $ menu =new HtmlPaginationMenu ("pagination- " .$ this ->identifier ,$ this ->_pagination ->getPagesNumbers ());
145161 $ menu ->floatRight ();
146162 $ menu ->setActiveItem ($ this ->_pagination ->getPage ()-1 );
147163 $ footer ->setValues ($ menu );
164+ $ this ->_associatePaginationBehavior ($ menu ,$ js );
165+ }
166+
167+ protected function _associatePaginationBehavior ($ menu ,$ js =NULL ){
148168 if (isset ($ this ->_urls ["refresh " ]))
149169 $ menu ->postOnClick ($ this ->_urls ["refresh " ],"{'p':$(this).attr('data-page')} " ,$ this ->getRefreshSelector (),["preventDefault " =>false ,"jqueryDone " =>"replaceWith " ]);
150170 }
@@ -314,4 +334,22 @@ public function setRefreshSelector($_refreshSelector) {
314334 return $ this ;
315335 }
316336
337+ public function show ($ modelInstance ){
338+ if (\is_array ($ modelInstance )){
339+ if (\is_array (array_values ($ modelInstance )[0 ]))
340+ $ modelInstance =\json_decode (\json_encode ($ modelInstance ), FALSE );
341+ }
342+ $ this ->_modelInstance =$ modelInstance ;
343+ }
344+
345+ public function getRowClass () {
346+ return $ this ->_rowClass ;
347+ }
348+
349+ public function setRowClass ($ _rowClass ) {
350+ $ this ->_rowClass =$ _rowClass ;
351+ return $ this ;
352+ }
353+
354+
317355}
0 commit comments