CheckboxColumn.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-04-14 21:53
  7. */
  8. namespace backend\grid;
  9. use Closure;
  10. use yii\helpers\Html;
  11. use yii\helpers\Json;
  12. /**
  13. * @inheritdoc
  14. */
  15. class CheckboxColumn extends \yii\grid\CheckboxColumn
  16. {
  17. public $width = '10px';
  18. /**
  19. * @inheritdoc
  20. */
  21. public function init()
  22. {
  23. parent::init();
  24. if (! isset($this->headerOptions['width'])) {
  25. $this->headerOptions['width'] = $this->width;
  26. }
  27. }
  28. /**
  29. * @inheritdoc
  30. */
  31. protected function renderHeaderCellContent()
  32. {
  33. if ($this->header !== null || !$this->multiple) {
  34. return parent::renderHeaderCellContent();
  35. } else {
  36. static $i = 1;
  37. $unique = uniqid() . $i;
  38. $i++;
  39. $for = 'inlineCheckbox' . $unique;
  40. $options['id'] = $for;
  41. $options['class'] = 'select-on-check-all';
  42. return "<span class=\"checkbox checkbox-success checkbox-inline\">" . Html::checkbox($this->getHeaderCheckBoxName(), false, $options) . "<label for='{$for}'></label></span>";
  43. }
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. protected function renderDataCellContent($model, $key, $index)
  49. {
  50. if ($this->checkboxOptions instanceof Closure) {
  51. $options = call_user_func($this->checkboxOptions, $model, $key, $index, $this);
  52. } else {
  53. $options = $this->checkboxOptions;
  54. }
  55. if (!isset($options['value'])) {
  56. $options['value'] = is_array($key) ? Json::encode($key) : $key;
  57. }
  58. if ($this->cssClass !== null) {
  59. Html::addCssClass($options, $this->cssClass);
  60. }
  61. static $i = 1;
  62. $unique = uniqid() . $i;
  63. $i++;
  64. $for = 'inlineCheckbox' . $unique;
  65. $options['id'] = $for;
  66. return "<span class=\"checkbox checkbox-success checkbox-inline\">" . Html::checkbox($this->name, !empty($options['checked']), $options) . "<label for='{$for}'></label></span>";
  67. }
  68. }