-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArea.php
75 lines (64 loc) · 2.6 KB
/
Area.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Description of Yii2.0 area selecter
*
* @author Axlrose <hp , *@qq.com>
* @link http://www.github.com
* @date 2016-12-20
* @warning beta 1.0
*/
namespace echotrue\area;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\helpers\Html;
use yii\widgets\InputWidget;
/**
* Class Area
*
* @package echotrue\area
*/
class Area extends InputWidget
{
public $template = "<div class='row'>{hideInput}<div class='col-sm-2'>{province}</div><div class='col-sm-2'>{city}</div><div class='col-sm-2'>{regional}</div></div>";
public function init()
{
if (!isset($this->options['url'])) {
throw new InvalidConfigException('Params `url` not found !');
}
isset($this->options['template']) && $this->template = $this->options['template'];
parent::init();
}
public function run()
{
$view = $this->getView();
AreaAsset::register($view);
echo $this->renderDom();
}
private function renderDom()
{
if ($this->hasModel()) {
if (!isset($this->attribute)) {
throw new InvalidParamException('参数错误');
}
return strtr($this->template, [
'{hideInput}' => Html::hiddenInput('url', $this->options['url'], ['id' => 'url']),
'{province}' => Html::activeDropDownList($this->model, $this->attribute . '[province]', ['0' => '请选择'],
['class' => 'form-control', 'id' => 'province']),
'{city}' => Html::activeDropDownList($this->model, $this->attribute . '[city]', ['0' => '请选择'],
['class' => 'form-control', 'id' => 'city']),
'{regional}' => Html::activeDropDownList($this->model, $this->attribute . '[regional]', ['0' => '请选择'],
['class' => 'form-control', 'id' => 'regional']),
]);
} else {
return strtr($this->template, [
'{hideInput}' => Html::hiddenInput('url', $this->options['url'], ['id' => 'url']),
'{province}' => Html::dropDownList($this->name . '[province]', null, ['0' => '请选择'],
['id' => 'province', 'class' => 'form-control']),
'{city}' => Html::dropDownList($this->name . '[city]', null, ['0' => '请选择'],
['id' => 'city', 'class' => 'form-control']),
'{regional}' => Html::dropDownList($this->name . '[regional]', null, ['0' => '请选择'],
['id' => 'regional', 'class' => 'form-control']),
]);
}
}
}