ActionColumn.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-03-21 19:55
  7. */
  8. namespace backend\grid;
  9. use Yii;
  10. use Closure;
  11. use yii\db\ActiveRecord;
  12. use yii\helpers\Html;
  13. use yii\helpers\Url;
  14. /**
  15. * @inheritdoc
  16. */
  17. class ActionColumn extends \yii\grid\ActionColumn
  18. {
  19. public $header = 'Action';
  20. public $queryParams = [];
  21. public $width = '100px';
  22. public $template = '{view-layer} {update} {delete}';
  23. /**
  24. * @inheritdoc
  25. */
  26. public function init()
  27. {
  28. parent::init();
  29. $this->header = Yii::t('app', $this->header);
  30. if (! isset($this->headerOptions['width'])) {
  31. $this->headerOptions['width'] = $this->width;
  32. }
  33. $this->contentOptions = array_merge(['class' => 'da-icon-column', 'style' => 'width:' . $this->width . ';'], $this->contentOptions);
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. protected function initDefaultButtons()
  39. {
  40. if (! isset($this->buttons['view'])) {
  41. $this->buttons['view'] = function ($url, $model, $key, $index, $gridView) {
  42. return Html::a('<i class="fa fa-folder"></i> ', $url, [
  43. 'title' => Yii::t('app', 'View'),
  44. 'data-pjax' => '0',
  45. 'class' => 'btn-sm',
  46. ]);
  47. };
  48. }
  49. if (! isset($this->buttons['view-layer'])) {
  50. $this->buttons['view-layer'] = function ($url, $model, $key, $index, $gridView) {
  51. //$url = str_replace('viewLayer', 'view', $url);
  52. return Html::a('<i class="fa fa-folder"></i> ', 'javascript:void(0)', [
  53. 'title' => Yii::t('yii', 'View'),
  54. 'url' => $url,
  55. 'onclick' => "viewLayer('" . $url . "',$(this))",
  56. 'data-pjax' => '0',
  57. 'class' => 'btn-sm',
  58. ]);
  59. };
  60. }
  61. if (! isset($this->buttons['update'])) {
  62. $this->buttons['update'] = function ($url, $model, $key, $index, $gridView) {
  63. return Html::a('<i class="fa fa-edit"></i> ', $url, [
  64. 'title' => Yii::t('app', 'Update'),
  65. 'data-pjax' => '0',
  66. 'class' => 'btn-sm',
  67. ]);
  68. };
  69. }
  70. if (! isset($this->buttons['delete'])) {
  71. $this->buttons['delete'] = function ($url, $model, $key, $index, $gridView) {
  72. $data = [];
  73. if($model instanceof ActiveRecord) {
  74. $primaryKeys = $model->getPrimaryKey(true);
  75. $data = [];
  76. foreach ($primaryKeys as $key => $abandon) {
  77. $data[$key] = $model->$key;
  78. }
  79. }
  80. return Html::a('<i class="glyphicon glyphicon-trash" aria-hidden="true"></i> ', $url, [
  81. 'title' => Yii::t('app', 'Delete'),
  82. 'data-confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
  83. 'data-method' => 'post',
  84. 'data-params' => json_encode($data),
  85. 'data-pjax' => '0',
  86. 'class' => 'btn-sm',
  87. ]);
  88. };
  89. }
  90. }
  91. /**
  92. * @inheritdoc
  93. */
  94. public function createUrl($action, $model, $key, $index)
  95. {
  96. if ($this->urlCreator instanceof Closure) {
  97. return call_user_func($this->urlCreator, $action, $model, $key, $index, $this);
  98. } else {
  99. $params = \Yii::$app->request->queryParams;
  100. if (is_array($key)) {
  101. $params = array_merge($params, $key);
  102. } else {
  103. $params['id'] = (string)$key;
  104. }
  105. if (isset($this->queryParams[$action])) {
  106. $params = array_merge($params, $this->queryParams[$action]);
  107. }
  108. $params[0] = $this->controller ? $this->controller . '/' . $action : $action;
  109. return Url::toRoute($params);
  110. }
  111. }
  112. /**
  113. * @inheritdoc
  114. */
  115. protected function renderDataCellContent($model, $key, $index)
  116. {
  117. if ($this->content !== null) {
  118. return call_user_func($this->content, $model, $key, $index, $this);
  119. }
  120. return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
  121. $name = $matches[1];
  122. if (isset($this->buttons[$name])) {
  123. $url = $this->createUrl($name, $model, $key, $index);
  124. return call_user_func($this->buttons[$name], $url, $model, $key, $index, $this);
  125. } else {
  126. return '';
  127. }
  128. }, $this->template);
  129. }
  130. }