Bar.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-06-15 09:25
  7. */
  8. namespace backend\widgets;
  9. use Yii;
  10. use yii\base\Widget;
  11. use yii\helpers\Html;
  12. use yii\helpers\Url;
  13. class Bar extends Widget
  14. {
  15. public $buttons = [];
  16. public $options = [
  17. 'class' => 'mail-tools tooltip-demo m-t-md',
  18. ];
  19. public $template = "{refresh} {create} {delete}";
  20. /**
  21. * @inheritdoc
  22. */
  23. public function run()
  24. {
  25. $buttons = '';
  26. $this->initDefaultButtons();
  27. $buttons .= $this->renderDataCellContent();
  28. return "<div class='{$this->options['class']}'>{$buttons}</div>";
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. protected function renderDataCellContent()
  34. {
  35. return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) {
  36. $name = $matches[1];
  37. if (isset($this->buttons[$name])) {
  38. return $this->buttons[$name] instanceof \Closure ? call_user_func($this->buttons[$name]) : $this->buttons[$name];
  39. } else {
  40. return '';
  41. }
  42. }, $this->template);
  43. }
  44. /**
  45. * 生成默认按钮
  46. *
  47. */
  48. protected function initDefaultButtons()
  49. {
  50. if (! isset($this->buttons['refresh'])) {
  51. $this->buttons['refresh'] = function () {
  52. return Html::a('<i class="fa fa-refresh"></i> ' . Yii::t('app', 'Refresh'), Url::to(['refresh']), [
  53. 'title' => Yii::t('app', 'Refresh'),
  54. 'data-pjax' => '0',
  55. 'class' => 'btn btn-white btn-sm refresh',
  56. ]);
  57. };
  58. }
  59. if (! isset($this->buttons['create'])) {
  60. $this->buttons['create'] = function () {
  61. return Html::a('<i class="fa fa-plus"></i> ' . Yii::t('app', 'Create'), Url::to(['create']), [
  62. 'title' => Yii::t('app', 'Create'),
  63. 'data-pjax' => '0',
  64. 'class' => 'btn btn-white btn-sm',
  65. ]);
  66. };
  67. }
  68. if (! isset($this->buttons['delete'])) {
  69. $this->buttons['delete'] = function () {
  70. return Html::a('<i class="fa fa-trash-o"></i> ' . Yii::t('app', 'Delete'), Url::to(['delete']), [
  71. 'title' => Yii::t('app', 'Delete'),
  72. 'data-pjax' => '0',
  73. 'data-confirm' => Yii::t('app', 'Really to delete?'),
  74. 'class' => 'btn btn-white btn-sm multi-operate',
  75. ]);
  76. };
  77. }
  78. }
  79. }