You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.8 KiB

  1. <?php
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright 2019 Blobt.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. namespace blobt\widgets;
  26. use yii\widgets\InputWidget;
  27. use Yii;
  28. use yii\base\InvalidConfigException;
  29. use yii\helpers\ArrayHelper;
  30. use yii\helpers\Html;
  31. use blobt\web\Select2Asset;
  32. /**
  33. * Description of Select2
  34. *
  35. * @author Blobt
  36. * @email 380255922@qq.com
  37. * @created Aug 20, 2019
  38. */
  39. class Select2 extends InputWidget {
  40. /**
  41. * @var array select的选项
  42. */
  43. public $data = [];
  44. /**
  45. * @var array select 的html选择
  46. */
  47. public $options;
  48. /**
  49. * @inheritdoc
  50. * @throws \ReflectionException
  51. * @throws \yii\base\InvalidConfigException
  52. */
  53. public function run() {
  54. $this->registerAsset();
  55. $this->registerSelect2Js();
  56. return $this->renderSelect();
  57. }
  58. /**
  59. * 初始化
  60. */
  61. public function init() {
  62. parent::init();
  63. if ($this->hasModel() && !isset($options['name'])) {
  64. $this->name = $this->options['name'] = Html::getInputName($this->model, $this->attribute);
  65. }
  66. }
  67. /**
  68. * 注册js和css
  69. */
  70. protected function registerAsset() {
  71. $view = $this->getView();
  72. Select2Asset::register($view);
  73. }
  74. /**
  75. * 注册js
  76. */
  77. protected function registerSelect2Js() {
  78. $js = <<<SCRIPT
  79. $('#category-icon_type').select2();
  80. SCRIPT;
  81. $this->getView()->registerJs($js);
  82. }
  83. /**
  84. * 渲染一个select
  85. * @return string
  86. */
  87. protected function renderSelect() {
  88. $value = null;
  89. if ($this->hasModel()) {
  90. $value = ArrayHelper::getValue($this->model, $this->attribute);
  91. }
  92. return Html::dropDownList($this->name, $value, $this->data, $this->options);
  93. }
  94. }