CityWidget.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/12
  6. * Time: 上午11:18
  7. */
  8. namespace common\modules\city\widgets;
  9. use common\models\City;
  10. use yii\base\InvalidConfigException;
  11. use yii\helpers\Html;
  12. use yii\helpers\Json;
  13. use yii\helpers\Url;
  14. use yii\widgets\InputWidget;
  15. class CityWidget extends InputWidget
  16. {
  17. public $provinceAttribute;
  18. public $provinceName;
  19. public $provinceValue;
  20. public $provincePrompt = '请选择';
  21. public $cityAttribute;
  22. public $cityName;
  23. public $cityValue;
  24. public $cityPrompt = '请选择';
  25. public $areaAttribute;
  26. public $areaName;
  27. public $areaValue;
  28. public $areaPrompt = '请选择';
  29. public $required = false;
  30. public $route = ['/city/default/children'];
  31. /**
  32. * @var $fullArea string|array 地区合集 eg. 北京 北京市 东城区
  33. */
  34. public $fullArea;
  35. public $defaultOptions = ['id' => 'city-three-level-link', 'class' => 'form-group form-inline'];
  36. public $selectClass = 'form-control';
  37. public $clientOptions = [];
  38. public function init()
  39. {
  40. $this->options = array_merge($this->defaultOptions, $this->options);
  41. if (!isset($this->options['id'])) {
  42. throw new InvalidConfigException('{options}.id必须设置');
  43. }
  44. parent::init();
  45. if (!empty($this->fullArea)) {
  46. list($this->provinceValue, $this->cityValue, $this->areaValue) = City::parseFullArea($this->fullArea);
  47. }
  48. }
  49. public function run()
  50. {
  51. $this->registerClientJs();
  52. if ($this->hasModel()) {
  53. $provinceValue = Html::getAttributeValue($this->model, $this->provinceAttribute);
  54. $cityValue = Html::getAttributeValue($this->model, $this->cityAttribute);
  55. $select = Html::activeDropDownList($this->model, $this->provinceAttribute, City::getChildren(0), [
  56. 'class' => $this->selectClass,
  57. 'prompt' => $this->provincePrompt
  58. ]) . ' ' . Html::activeDropDownList($this->model, $this->cityAttribute, City::getChildren($provinceValue), [
  59. 'class' => $this->selectClass,
  60. 'prompt' => $this->cityPrompt
  61. ]) . ' ' . Html::activeDropDownList($this->model, $this->areaAttribute, City::getChildren($cityValue), [
  62. 'class' => $this->selectClass,
  63. 'prompt' => $this->areaPrompt
  64. ]);
  65. } else {
  66. $select = Html::dropDownList($this->provinceName, $this->provinceValue, City::getChildren(0), [
  67. 'class' => $this->selectClass,
  68. 'prompt' => $this->provincePrompt
  69. ]) . ' ' . Html::dropDownList($this->cityName, $this->cityValue, City::getChildren($this->provinceValue), [
  70. 'class' => $this->selectClass,
  71. 'prompt' => $this->cityPrompt
  72. ]) . ' ' . Html::dropDownList($this->areaName, $this->areaValue, City::getChildren($this->cityValue), [
  73. 'class' => $this->selectClass,
  74. 'prompt' => $this->areaPrompt
  75. ]);
  76. }
  77. return Html::tag('div', $select, $this->options);
  78. }
  79. public function registerClientJs()
  80. {
  81. CityAsset::register($this->view);
  82. $this->clientOptions['url'] = Url::to($this->route);
  83. $clientOptions = Json::htmlEncode($this->clientOptions);
  84. $this->view->registerJs("$('#{$this->options['id']} select').getArea($clientOptions)");
  85. }
  86. }