Skip to content

Commit 24138a2

Browse files
committed
Edit and delete buttons behavior (DataTable)
1 parent 772d83b commit 24138a2

File tree

1 file changed

+74
-16
lines changed

1 file changed

+74
-16
lines changed

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,38 @@ class DataTable extends Widget {
2929
protected $_pagination;
3030
protected $_hasCheckboxes;
3131
protected $_compileParts;
32+
protected $_hasDelete=false;
33+
protected $_hasEdit=false;
3234
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+
}
3342

3443
public function run(JsUtils $js){
3544
if($this->_hasCheckboxes && isset($js)){
3645
$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');}");
4049
}
4150
if($this->_visibleHover){
4251
$js->execOn("mouseover", "#".$this->identifier." tr", "$(event.target).closest('tr').find('.visibleover').css('visibility', 'visible');",["preventDefault"=>false,"stopPropagation"=>true]);
4352
$js->execOn("mouseout", "#".$this->identifier." tr", "$(event.target).closest('tr').find('.visibleover').css('visibility', 'hidden');",["preventDefault"=>false,"stopPropagation"=>true]);
4453
}
54+
if($this->_hasDelete)
55+
$this->_generateBehavior("delete", $js);
56+
if($this->_hasEdit)
57+
$this->_generateBehavior("edit", $js);
4558
return parent::run($js);
4659
}
4760

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"]);
5164
}
5265

5366
/**
@@ -74,8 +87,10 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
7487
$table->setHeaderValues($captions);
7588
if(isset($this->_compileParts))
7689
$table->setCompileParts($this->_compileParts);
90+
7791
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"]);
7994
}
8095

8196
$this->_generateContent($table);
@@ -135,7 +150,8 @@ private function _generatePagination($table){
135150
$menu->floatRight();
136151
$menu->setActiveItem($this->_pagination->getPage()-1);
137152
$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"]);
139155
}
140156

141157
protected function _getFieldName($index){
@@ -193,8 +209,19 @@ public function getUrls() {
193209
return $this->_urls;
194210
}
195211

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+
*/
196217
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+
}
198225
return $this;
199226
}
200227

@@ -316,26 +343,30 @@ private function getDefaultButton($icon,$class=null,$visibleHover=true){
316343
return $bt;
317344
}
318345

319-
public function addDeleteButton($visibleHover=true,$callback=null){
346+
public function addDeleteButton($visibleHover=true,$generateBehavior=true,$callback=null){
347+
$this->_hasDelete=$generateBehavior;
320348
return $this->addDefaultButton("remove","delete red basic",$visibleHover,$callback);
321349
}
322350

323-
public function addEditButton($visibleHover=true,$callback=null){
351+
public function addEditButton($visibleHover=true,$generateBehavior=true,$callback=null){
352+
$this->_hasEdit=$generateBehavior;
324353
return $this->addDefaultButton("edit","edit basic",$visibleHover,$callback);
325354
}
326355

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);
329358
$index=$this->_instanceViewer->visiblePropertiesCount()-1;
330-
$this->insertDeleteButtonIn($index,$visibleHover,$callbackDelete);
359+
$this->insertDeleteButtonIn($index,$visibleHover,$generateBehavior,$callbackDelete);
331360
return $this;
332361
}
333362

334-
public function insertDeleteButtonIn($index,$visibleHover=true,$callback=null){
363+
public function insertDeleteButtonIn($index,$visibleHover=true,$generateBehavior=true,$callback=null){
364+
$this->_hasDelete=$generateBehavior;
335365
return $this->insertDefaultButtonIn($index,"remove","delete red basic",$visibleHover,$callback);
336366
}
337367

338-
public function insertEditButtonIn($index,$visibleHover=true,$callback=null){
368+
public function insertEditButtonIn($index,$visibleHover=true,$generateBehavior=true,$callback=null){
369+
$this->_hasEdit=$generateBehavior;
339370
return $this->insertDefaultButtonIn($index,"edit","edit basic",$visibleHover,$callback);
340371
}
341372

@@ -366,6 +397,15 @@ public function asForm(){
366397
return $this->getForm();
367398
}
368399

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+
*/
369409
public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
370410
return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle,$index,$attributes){
371411
$button=new HtmlButton($id,$value,$cssStyle);
@@ -380,4 +420,22 @@ protected function _visibleOver($element){
380420
$this->_visibleHover=true;
381421
return $element->addToProperty("class", "visibleover")->setProperty("style","visibility:hidden;");
382422
}
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+
383441
}

0 commit comments

Comments
 (0)