@@ -29,25 +29,38 @@ class DataTable extends Widget {
29
29
protected $ _pagination ;
30
30
protected $ _hasCheckboxes ;
31
31
protected $ _compileParts ;
32
+ protected $ _hasDelete =false ;
33
+ protected $ _hasEdit =false ;
32
34
protected $ _visibleHover =false ;
35
+ protected $ _targetSelector ;
36
+
37
+ public function __construct ($ identifier ,$ model ,$ modelInstance =NULL ) {
38
+ parent ::__construct ($ identifier , $ model ,$ modelInstance );
39
+ $ this ->_init (new InstanceViewer ($ identifier ), "table " , new HtmlTable ($ identifier , 0 ,0 ), false );
40
+ $ this ->_urls =[];
41
+ }
33
42
34
43
public function run (JsUtils $ js ){
35
44
if ($ this ->_hasCheckboxes && isset ($ js )){
36
45
$ js ->execOn ("change " , "# " .$ this ->identifier ." [name='selection[]'] " , "
37
- var \$parentCheckbox= \$('#ck-main-ck- {$ this ->identifier }'), \$checkbox= \$('# {$ this ->identifier } [name= \"selection[] \"]'),allChecked=true,allUnchecked=true;
38
- \$checkbox.each(function() {if($(this).prop('checked')){allUnchecked = false;}else{allChecked = false;}});
39
- if(allChecked) { \$parentCheckbox.checkbox('set checked');}else if(allUnchecked){ \$parentCheckbox.checkbox('set unchecked');}else{ \$parentCheckbox.checkbox('set indeterminate');} " );
46
+ var \$parentCheckbox= \$('#ck-main-ck- {$ this ->identifier }'), \$checkbox= \$('# {$ this ->identifier } [name= \"selection[] \"]'),allChecked=true,allUnchecked=true;
47
+ \$checkbox.each(function() {if($(this).prop('checked')){allUnchecked = false;}else{allChecked = false;}});
48
+ if(allChecked) { \$parentCheckbox.checkbox('set checked');}else if(allUnchecked){ \$parentCheckbox.checkbox('set unchecked');}else{ \$parentCheckbox.checkbox('set indeterminate');} " );
40
49
}
41
50
if ($ this ->_visibleHover ){
42
51
$ js ->execOn ("mouseover " , "# " .$ this ->identifier ." tr " , "$(event.target).closest('tr').find('.visibleover').css('visibility', 'visible'); " ,["preventDefault " =>false ,"stopPropagation " =>true ]);
43
52
$ js ->execOn ("mouseout " , "# " .$ this ->identifier ." tr " , "$(event.target).closest('tr').find('.visibleover').css('visibility', 'hidden'); " ,["preventDefault " =>false ,"stopPropagation " =>true ]);
44
53
}
54
+ if ($ this ->_hasDelete )
55
+ $ this ->_generateBehavior ("delete " , $ js );
56
+ if ($ this ->_hasEdit )
57
+ $ this ->_generateBehavior ("edit " , $ js );
45
58
return parent ::run ($ js );
46
59
}
47
60
48
- public function __construct ( $ identifier , $ model , $ modelInstance = NULL ) {
49
- parent :: __construct ( $ identifier , $ model , $ modelInstance );
50
- $ this -> _init ( new InstanceViewer ( $ identifier), " table " , new HtmlTable ( $ identifier , 0 , 0 ), false );
61
+ protected function _generateBehavior ( $ op , JsUtils $ js ) {
62
+ if ( isset ( $ this -> _urls [ $ op ]))
63
+ $ js -> getOnClick ( " # " . $ this -> identifier . " . " . $ op , $ this -> _urls [ $ op ], $ this -> getTargetSelector (),[ " preventDefault " => false , " attr " => " data-ajax " ] );
51
64
}
52
65
53
66
/**
@@ -74,8 +87,10 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
74
87
$ table ->setHeaderValues ($ captions );
75
88
if (isset ($ this ->_compileParts ))
76
89
$ table ->setCompileParts ($ this ->_compileParts );
90
+
77
91
if (isset ($ this ->_searchField ) && isset ($ js )){
78
- $ this ->_searchField ->postOn ("change " , $ this ->_urls ,"{'s':$(this).val()} " ,"# " .$ this ->identifier ." tbody " ,["preventDefault " =>false ,"jqueryDone " =>"replaceWith " ]);
92
+ if (isset ($ this ->_urls ["refresh " ]))
93
+ $ this ->_searchField ->postOn ("change " , $ this ->_urls ["refresh " ],"{'s':$(this).val()} " ,"# " .$ this ->identifier ." tbody " ,["preventDefault " =>false ,"jqueryDone " =>"replaceWith " ]);
79
94
}
80
95
81
96
$ this ->_generateContent ($ table );
@@ -135,7 +150,8 @@ private function _generatePagination($table){
135
150
$ menu ->floatRight ();
136
151
$ menu ->setActiveItem ($ this ->_pagination ->getPage ()-1 );
137
152
$ footer ->setValues ($ menu );
138
- $ menu ->postOnClick ($ this ->_urls ,"{'p':$(this).attr('data-page')} " ,"# " .$ this ->identifier ." tbody " ,["preventDefault " =>false ,"jqueryDone " =>"replaceWith " ]);
153
+ if (isset ($ this ->_urls ["refresh " ]))
154
+ $ menu ->postOnClick ($ this ->_urls ["refresh " ],"{'p':$(this).attr('data-page')} " ,"# " .$ this ->identifier ." tbody " ,["preventDefault " =>false ,"jqueryDone " =>"replaceWith " ]);
139
155
}
140
156
141
157
protected function _getFieldName ($ index ){
@@ -193,8 +209,19 @@ public function getUrls() {
193
209
return $ this ->_urls ;
194
210
}
195
211
212
+ /**
213
+ * Sets the associative array of urls for refreshing, updating or deleting
214
+ * @param string|array $urls associative array with keys refresh: for refreshing with search field or pagination, edit : for updating a row, delete: for deleting a row
215
+ * @return \Ajax\semantic\widgets\datatable\DataTable
216
+ */
196
217
public function setUrls ($ urls ) {
197
- $ this ->_urls =$ urls ;
218
+ if (\is_array ($ urls )){
219
+ $ this ->_urls ["refresh " ]=JArray::getValue ($ urls , "refresh " ,0 );
220
+ $ this ->_urls ["edit " ]=JArray::getValue ($ urls , "edit " ,1 );
221
+ $ this ->_urls ["delete " ]=JArray::getValue ($ urls , "delete " ,2 );
222
+ }else {
223
+ $ this ->_urls =["refresh " =>$ urls ,"edit " =>$ urls ,"delete " =>$ urls ];
224
+ }
198
225
return $ this ;
199
226
}
200
227
@@ -316,26 +343,30 @@ private function getDefaultButton($icon,$class=null,$visibleHover=true){
316
343
return $ bt ;
317
344
}
318
345
319
- public function addDeleteButton ($ visibleHover =true ,$ callback =null ){
346
+ public function addDeleteButton ($ visibleHover =true ,$ generateBehavior =true ,$ callback =null ){
347
+ $ this ->_hasDelete =$ generateBehavior ;
320
348
return $ this ->addDefaultButton ("remove " ,"delete red basic " ,$ visibleHover ,$ callback );
321
349
}
322
350
323
- public function addEditButton ($ visibleHover =true ,$ callback =null ){
351
+ public function addEditButton ($ visibleHover =true ,$ generateBehavior =true ,$ callback =null ){
352
+ $ this ->_hasEdit =$ generateBehavior ;
324
353
return $ this ->addDefaultButton ("edit " ,"edit basic " ,$ visibleHover ,$ callback );
325
354
}
326
355
327
- public function addEditDeleteButtons ($ visibleHover =true ,$ callbackEdit =null ,$ callbackDelete =null ){
328
- $ this ->addEditButton ($ visibleHover ,$ callbackEdit );
356
+ public function addEditDeleteButtons ($ visibleHover =true ,$ generateBehavior = true , $ callbackEdit =null ,$ callbackDelete =null ){
357
+ $ this ->addEditButton ($ visibleHover ,$ generateBehavior , $ callbackEdit );
329
358
$ index =$ this ->_instanceViewer ->visiblePropertiesCount ()-1 ;
330
- $ this ->insertDeleteButtonIn ($ index ,$ visibleHover ,$ callbackDelete );
359
+ $ this ->insertDeleteButtonIn ($ index ,$ visibleHover ,$ generateBehavior , $ callbackDelete );
331
360
return $ this ;
332
361
}
333
362
334
- public function insertDeleteButtonIn ($ index ,$ visibleHover =true ,$ callback =null ){
363
+ public function insertDeleteButtonIn ($ index ,$ visibleHover =true ,$ generateBehavior =true ,$ callback =null ){
364
+ $ this ->_hasDelete =$ generateBehavior ;
335
365
return $ this ->insertDefaultButtonIn ($ index ,"remove " ,"delete red basic " ,$ visibleHover ,$ callback );
336
366
}
337
367
338
- public function insertEditButtonIn ($ index ,$ visibleHover =true ,$ callback =null ){
368
+ public function insertEditButtonIn ($ index ,$ visibleHover =true ,$ generateBehavior =true ,$ callback =null ){
369
+ $ this ->_hasEdit =$ generateBehavior ;
339
370
return $ this ->insertDefaultButtonIn ($ index ,"edit " ,"edit basic " ,$ visibleHover ,$ callback );
340
371
}
341
372
@@ -366,6 +397,15 @@ public function asForm(){
366
397
return $ this ->getForm ();
367
398
}
368
399
400
+ /**
401
+ * Creates a submit button at $index position
402
+ * @param int $index
403
+ * @param string $cssStyle
404
+ * @param string $url
405
+ * @param string $responseElement
406
+ * @param array $attributes
407
+ * @return \Ajax\semantic\widgets\datatable\DataTable
408
+ */
369
409
public function fieldAsSubmit ($ index ,$ cssStyle =NULL ,$ url =NULL ,$ responseElement =NULL ,$ attributes =NULL ){
370
410
return $ this ->_fieldAs (function ($ id ,$ name ,$ value ,$ caption ) use ($ url ,$ responseElement ,$ cssStyle ,$ index ,$ attributes ){
371
411
$ button =new HtmlButton ($ id ,$ value ,$ cssStyle );
@@ -380,4 +420,22 @@ protected function _visibleOver($element){
380
420
$ this ->_visibleHover =true ;
381
421
return $ element ->addToProperty ("class " , "visibleover " )->setProperty ("style " ,"visibility:hidden; " );
382
422
}
423
+
424
+ protected function getTargetSelector () {
425
+ $ result =$ this ->_targetSelector ;
426
+ if (!isset ($ result ))
427
+ $ result ="# " .$ this ->identifier ;
428
+ return $ result ;
429
+ }
430
+
431
+ /**
432
+ * Sets the response element selector for Edit and Delete request with ajax
433
+ * @param string $_targetSelector
434
+ * @return \Ajax\semantic\widgets\datatable\DataTable
435
+ */
436
+ public function setTargetSelector ($ _targetSelector ) {
437
+ $ this ->_targetSelector =$ _targetSelector ;
438
+ return $ this ;
439
+ }
440
+
383
441
}
0 commit comments