Skip to content

Commit ca057c3

Browse files
committed
DataTable checkboxes info
1 parent 0b37738 commit ca057c3

File tree

5 files changed

+78
-10
lines changed

5 files changed

+78
-10
lines changed

Ajax/common/Widget.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,13 @@ public function addDropdownInToolbar($value,$items,$callback=NULL){
235235
}
236236

237237
/**
238-
* @param unknown $caption
238+
* @param string $caption
239+
* @param string $cssStyle
239240
* @param callable $callback function($element)
240241
* @return \Ajax\common\html\HtmlDoubleElement
241242
*/
242-
public function addButtonInToolbar($caption,$callback=NULL){
243-
$bt=new HtmlButton("bt-".$caption,$caption);
243+
public function addButtonInToolbar($caption,$cssStyle=null,$callback=NULL){
244+
$bt=new HtmlButton("bt-".$caption,$caption,$cssStyle);
244245
return $this->addInToolbar($bt,$callback);
245246
}
246247

Ajax/common/html/HtmlSingleElement.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public function setClass($classNames) {
2020
return $this;
2121
}
2222

23+
public function addClass($classNames) {
24+
if(\is_array($classNames)){
25+
$classNames=implode(" ", $classNames);
26+
}
27+
$this->addToProperty("class", $classNames);
28+
return $this;
29+
}
30+
2331
public function setRole($value) {
2432
$this->setProperty("role", $value);
2533
return $this;

Ajax/semantic/html/collections/menus/HtmlMenu.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Ajax\semantic\html\content\HtmlMenuItem;
1818
use Ajax\JsUtils;
1919
use Ajax\semantic\html\elements\HtmlButtonGroups;
20+
use Ajax\semantic\html\elements\HtmlLabel;
2021

2122
/**
2223
* Semantic Menu component
@@ -51,7 +52,7 @@ public function setActiveItem($index) {
5152
}
5253

5354
private function getItemToInsert($item) {
54-
if ($item instanceof HtmlInput || $item instanceof HtmlImg || $item instanceof HtmlIcon || $item instanceof HtmlButtonGroups || $item instanceof HtmlButton ) {
55+
if ($item instanceof HtmlInput || $item instanceof HtmlImg || $item instanceof HtmlIcon || $item instanceof HtmlButtonGroups || $item instanceof HtmlButton || $item instanceof HtmlLabel) {
5556
$itemO=new HtmlMenuItem("item-" . $this->identifier . "-" . \sizeof($this->content) , $item);
5657
$item=$itemO;
5758
}

Ajax/semantic/html/elements/HtmlButtonGroups.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public function getElements() {
101101

102102
public function addClasses($classes=array()) {
103103
$i=0;
104+
if(!\is_array($classes)){
105+
$classes=array_fill (0,$this->count(),$classes);
106+
}
104107
foreach ( $this->content as $button ) {
105108
$button->addToProperty("class", $classes[$i++]);
106109
}

Ajax/semantic/widgets/datatable/DataTable.php

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Ajax\semantic\html\base\HtmlSemDoubleElement;
1515
use Ajax\semantic\widgets\base\InstanceViewer;
1616
use Ajax\semantic\html\collections\table\traits\TableTrait;
17+
use Ajax\semantic\html\elements\HtmlLabel;
1718

1819
/**
1920
* DataTable widget for displaying list of objects
@@ -32,7 +33,9 @@ class DataTable extends Widget {
3233
protected $_hasDelete=false;
3334
protected $_hasEdit=false;
3435
protected $_visibleHover=false;
36+
protected $_hasCheckedMessage=false;
3537
protected $_targetSelector;
38+
protected $_checkedMessage;
3639

3740
public function __construct($identifier,$model,$modelInstance=NULL) {
3841
parent::__construct($identifier, $model,$modelInstance);
@@ -42,10 +45,7 @@ public function __construct($identifier,$model,$modelInstance=NULL) {
4245

4346
public function run(JsUtils $js){
4447
if($this->_hasCheckboxes && isset($js)){
45-
$js->execOn("change", "#".$this->identifier." [name='selection[]']", "
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');}");
48+
$this->_runCheckboxes($js);
4949
}
5050
if($this->_visibleHover){
5151
$js->execOn("mouseover", "#".$this->identifier." tr", "$(event.target).closest('tr').find('.visibleover').css('visibility', 'visible');",["preventDefault"=>false,"stopPropagation"=>true]);
@@ -58,6 +58,24 @@ public function run(JsUtils $js){
5858
return parent::run($js);
5959
}
6060

61+
protected function _runCheckboxes(JsUtils $js){
62+
$checkedMessageCall="";
63+
if($this->_hasCheckedMessage){
64+
$msg=$this->getCheckedMessage();
65+
$checkedMessageFunction="function updateChecked(){var msg='".$msg[0]."',count=\$('#{$this->identifier} [name=\"selection[]\"]:checked').length,all=\$('#{$this->identifier} [name=\"selection[]\"]').length;
66+
if(count==1) msg='".$msg[1]."';
67+
else if(count>1) msg='".$msg["other"]."';
68+
\$('#checked-count-".$this->identifier."').contents().filter(function() {return this.nodeType == 3;}).each(function(){this.textContent = msg.replace('{count}',count);});
69+
\$('#toolbar-{$this->identifier} .visibleOnChecked').toggle(count>0);}\$('#toolbar-".$this->identifier." .visibleOnChecked').hide();";
70+
$checkedMessageCall="updateChecked();";
71+
$js->exec($checkedMessageFunction,true);
72+
}
73+
$js->execOn("change", "#".$this->identifier." [name='selection[]']", "
74+
var \$parentCheckbox=\$('#ck-main-ck-{$this->identifier}'),\$checkbox=\$('#{$this->identifier} [name=\"selection[]\"]'),allChecked=true,allUnchecked=true;
75+
\$checkbox.each(function() {if($(this).prop('checked')){allUnchecked = false;}else{allChecked = false;}});
76+
if(allChecked) {\$parentCheckbox.checkbox('set checked');}else if(allUnchecked){\$parentCheckbox.checkbox('set unchecked');}else{\$parentCheckbox.checkbox('set indeterminate');};".$checkedMessageCall);
77+
}
78+
6179
protected function _generateBehavior($op,JsUtils $js){
6280
if(isset($this->_urls[$op]))
6381
$js->getOnClick("#".$this->identifier." .".$op, $this->_urls[$op],$this->getTargetSelector(),["preventDefault"=>false,"attr"=>"data-ajax"]);
@@ -114,8 +132,11 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
114132

115133
private function _generateMainCheckbox(&$captions){
116134
$ck=new HtmlCheckbox("main-ck-".$this->identifier,"");
117-
$ck->setOnChecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',true);");
118-
$ck->setOnUnchecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',false);");
135+
$checkedMessageCall="";
136+
if($this->_hasCheckedMessage)
137+
$checkedMessageCall="updateChecked();";
138+
$ck->setOnChecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',true);".$checkedMessageCall);
139+
$ck->setOnUnchecked("$('#".$this->identifier." [name=%quote%selection[]%quote%]').prop('checked',false);".$checkedMessageCall);
119140
\array_unshift($captions, $ck);
120141
}
121142

@@ -438,4 +459,38 @@ public function setTargetSelector($_targetSelector) {
438459
return $this;
439460
}
440461

462+
protected function getCheckedMessage() {
463+
$result= $this->_checkedMessage;
464+
if(!isset($result)){
465+
$result=[0=>"none selected",1=>"one item selected","other"=>"{count} items selected"];
466+
}
467+
return $result;
468+
}
469+
470+
/**
471+
* Defines the message displayed when checkboxes are checked or unchecked
472+
* with an associative array 0=>no selection,1=>one item selected, other=>{count} items selected
473+
* @param array $_checkedMessage
474+
* @return \Ajax\semantic\widgets\datatable\DataTable
475+
*/
476+
public function setCheckedMessage(array $_checkedMessage) {
477+
$this->_checkedMessage=$_checkedMessage;
478+
return $this;
479+
}
480+
481+
/**
482+
* @param array $checkedMessage
483+
* @param callable $callback
484+
*/
485+
public function addCountCheckedInToolbar(array $checkedMessage=null,$callback=null){
486+
if(isset($checkedMessage))
487+
$this->_checkedMessage=$checkedMessage;
488+
$checkedMessage=$this->getCheckedMessage();
489+
$this->_hasCheckboxes=true;
490+
$this->_hasCheckedMessage=true;
491+
$element=new HtmlLabel("checked-count-".$this->identifier,$checkedMessage[0]);
492+
$this->addInToolbar($element,$callback);
493+
}
494+
495+
441496
}

0 commit comments

Comments
 (0)