CCheckBoxColumn.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * CCheckBoxColumn class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. Yii::import('zii.widgets.grid.CGridColumn');
  11. /**
  12. * CCheckBoxColumn represents a grid view column of checkboxes.
  13. *
  14. * CCheckBoxColumn supports no checking (read-only), single check and multiple checking.
  15. * The mode is determined according to {@link selectableRows}. When in multiple checking mode, the header cell will display
  16. * an additional checkbox, clicking on which will check or uncheck all of the checkboxes in the data cells.
  17. * The header cell can be customized by {@link headerTemplate}.
  18. *
  19. * Additionally selecting a checkbox can select a grid view row (depending on {@link CGridView::selectableRows} value) if
  20. * {@link selectableRows} is null (default).
  21. *
  22. * By default, the checkboxes rendered in data cells will have the values that are the same as
  23. * the key values of the data model. One may change this by setting either {@link name} or
  24. * {@link value}.
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @package zii.widgets.grid
  28. * @since 1.1
  29. */
  30. class CCheckBoxColumn extends CGridColumn
  31. {
  32. /**
  33. * @var string the attribute name of the data model. The corresponding attribute value will be rendered
  34. * in each data cell as the checkbox value. Note that if {@link value} is specified, this property will be ignored.
  35. * @see value
  36. */
  37. public $name;
  38. /**
  39. * @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
  40. * in each data cell as the checkbox value. In this expression, you can use the following variables:
  41. * <ul>
  42. * <li><code>$row</code> the row number (zero-based)</li>
  43. * <li><code>$data</code> the data model for the row</li>
  44. * <li><code>$this</code> the column object</li>
  45. * </ul>
  46. * The PHP expression will be evaluated using {@link evaluateExpression}.
  47. *
  48. * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
  49. * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
  50. */
  51. public $value;
  52. /**
  53. * @var string a PHP expression that will be evaluated for every data cell and whose result will
  54. * determine if checkbox for each data cell is checked. In this expression, you can use the following variables:
  55. * <ul>
  56. * <li><code>$row</code> the row number (zero-based)</li>
  57. * <li><code>$data</code> the data model for the row</li>
  58. * <li><code>$this</code> the column object</li>
  59. * </ul>
  60. * The PHP expression will be evaluated using {@link evaluateExpression}.
  61. *
  62. * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
  63. * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
  64. * @since 1.1.4
  65. */
  66. public $checked;
  67. /**
  68. * @var string a PHP expression that will be evaluated for every data cell and whose result will
  69. * determine if checkbox for each data cell is disabled. In this expression, you can use the following variables:
  70. * <ul>
  71. * <li><code>$row</code> the row number (zero-based)</li>
  72. * <li><code>$data</code> the data model for the row</li>
  73. * <li><code>$this</code> the column object</li>
  74. * </ul>
  75. * The PHP expression will be evaluated using {@link evaluateExpression}.
  76. *
  77. * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
  78. * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
  79. *
  80. * Note that expression result will overwrite value set with <code>checkBoxHtmlOptions['disabled']</code>.
  81. * @since 1.1.13
  82. */
  83. public $disabled;
  84. /**
  85. * @var array the HTML options for the data cell tags.
  86. */
  87. public $htmlOptions=array('class'=>'checkbox-column');
  88. /**
  89. * @var array the HTML options for the header cell tag.
  90. */
  91. public $headerHtmlOptions=array('class'=>'checkbox-column');
  92. /**
  93. * @var array the HTML options for the footer cell tag.
  94. */
  95. public $footerHtmlOptions=array('class'=>'checkbox-column');
  96. /**
  97. * @var array the HTML options for the checkboxes.
  98. */
  99. public $checkBoxHtmlOptions=array();
  100. /**
  101. * @var integer the number of rows that can be checked.
  102. * Possible values:
  103. * <ul>
  104. * <li>0 - the state of the checkbox cannot be changed (read-only mode)</li>
  105. * <li>1 - only one row can be checked. Checking a checkbox has nothing to do with selecting the row</li>
  106. * <li>2 or more - multiple checkboxes can be checked. Checking a checkbox has nothing to do with selecting the row</li>
  107. * <li>null - {@link CGridView::selectableRows} is used to control how many checkboxes can be checked.
  108. * Checking a checkbox will also select the row.</li>
  109. * </ul>
  110. * You may also call the JavaScript function <code>$(gridID).yiiGridView('getChecked', columnID)</code>
  111. * to retrieve the key values of the checked rows.
  112. * @since 1.1.6
  113. */
  114. public $selectableRows=null;
  115. /**
  116. * @var string the template to be used to control the layout of the header cell.
  117. * The token "{item}" is recognized and it will be replaced with a "check all" checkbox.
  118. * By default if in multiple checking mode, the header cell will display an additional checkbox,
  119. * clicking on which will check or uncheck all of the checkboxes in the data cells.
  120. * See {@link selectableRows} for more details.
  121. * @since 1.1.11
  122. */
  123. public $headerTemplate='{item}';
  124. /**
  125. * Initializes the column.
  126. * This method registers necessary client script for the checkbox column.
  127. */
  128. public function init()
  129. {
  130. if(isset($this->checkBoxHtmlOptions['name']))
  131. $name=$this->checkBoxHtmlOptions['name'];
  132. else
  133. {
  134. $name=$this->id;
  135. if(substr($name,-2)!=='[]')
  136. $name.='[]';
  137. $this->checkBoxHtmlOptions['name']=$name;
  138. }
  139. $name=strtr($name,array('['=>"\\[",']'=>"\\]"));
  140. if($this->selectableRows===null)
  141. {
  142. if(isset($this->checkBoxHtmlOptions['class']))
  143. $this->checkBoxHtmlOptions['class'].=' select-on-check';
  144. else
  145. $this->checkBoxHtmlOptions['class']='select-on-check';
  146. return;
  147. }
  148. $cball=$cbcode='';
  149. if($this->selectableRows==0)
  150. {
  151. //.. read only
  152. $cbcode="return false;";
  153. }
  154. elseif($this->selectableRows==1)
  155. {
  156. //.. only one can be checked, uncheck all other
  157. $cbcode="jQuery(\"input:not(#\"+this.id+\")[name='$name']\").prop('checked',false);";
  158. }
  159. elseif(strpos($this->headerTemplate,'{item}')!==false)
  160. {
  161. //.. process check/uncheck all
  162. $cball=<<<CBALL
  163. jQuery(document).on('click','#{$this->id}_all',function() {
  164. var checked=this.checked;
  165. jQuery("input[name='$name']:enabled").each(function() {this.checked=checked;});
  166. });
  167. CBALL;
  168. $cbcode="jQuery('#{$this->id}_all').prop('checked', jQuery(\"input[name='$name']\").length==jQuery(\"input[name='$name']:checked\").length);";
  169. }
  170. if($cbcode!=='')
  171. {
  172. $js=$cball;
  173. $js.=<<<EOD
  174. jQuery(document).on('click', "input[name='$name']", function() {
  175. $cbcode
  176. });
  177. EOD;
  178. Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id,$js);
  179. }
  180. }
  181. /**
  182. * Returns the header cell content.
  183. * This method will render a checkbox in the header when {@link selectableRows} is greater than 1
  184. * or in case {@link selectableRows} is null when {@link CGridView::selectableRows} is greater than 1.
  185. * @return string the header cell content.
  186. * @since 1.1.16
  187. */
  188. public function getHeaderCellContent()
  189. {
  190. if(trim($this->headerTemplate)==='')
  191. return $this->grid->blankDisplay;
  192. if($this->selectableRows===null && $this->grid->selectableRows>1)
  193. $item=CHtml::checkBox($this->id.'_all',false,array('class'=>'select-on-check-all'));
  194. elseif($this->selectableRows>1)
  195. $item=CHtml::checkBox($this->id.'_all',false);
  196. else
  197. $item=parent::getHeaderCellContent();
  198. return strtr($this->headerTemplate,array(
  199. '{item}'=>$item,
  200. ));
  201. }
  202. /**
  203. * Returns the data cell content.
  204. * This method renders a checkbox in the data cell.
  205. * @param integer $row the row number (zero-based)
  206. * @return string the data cell content.
  207. * @since 1.1.16
  208. */
  209. public function getDataCellContent($row)
  210. {
  211. $data=$this->grid->dataProvider->data[$row];
  212. if($this->value!==null)
  213. $value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
  214. elseif($this->name!==null)
  215. $value=CHtml::value($data,$this->name);
  216. else
  217. $value=$this->grid->dataProvider->keys[$row];
  218. $checked = false;
  219. if($this->checked!==null)
  220. $checked=$this->evaluateExpression($this->checked,array('data'=>$data,'row'=>$row));
  221. $options=$this->checkBoxHtmlOptions;
  222. if($this->disabled!==null)
  223. $options['disabled']=$this->evaluateExpression($this->disabled,array('data'=>$data,'row'=>$row));
  224. $name=$options['name'];
  225. unset($options['name']);
  226. $options['value']=$value;
  227. $options['id']=$this->id.'_'.$row;
  228. return CHtml::checkBox($name,$checked,$options);
  229. }
  230. }