yiier / yii2-china-region
Yii2中国省市区三级联动
Installs: 151
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 4
Forks: 1
Open Issues: 1
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-10-26 00:50:02 UTC
README
Yii2中国省市区街道四级联动
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yiier/yii2-china-region "*"
or add
"yiier/yii2-china-region": "*"
to the require section of your composer.json
file.
Migrations
Run the following command
php yii migrate --migrationPath=@yiier/region/migrations/
配置
在 controller 中添加以下 action
public function actions() { $actions = parent::actions(); $actions['get-region'] = [ 'class' => \yiier\region\RegionAction::className(), ]; return $actions; }
使用
view 页面
<?= Html::label('地址') ?> <?= $form->field($model, 'province_id')->widget(\yiier\region\RegionWidget::className(), [ 'model' => $model, 'url' => \yii\helpers\Url::toRoute(['get-region']), 'showOutOfRange'=> true, // 默认是 false,表示只展示 out_of_range 为 0 的数据, true 表示展示所有数据 'province' => [ 'attribute' => 'province_id', 'items' => Region::getRegion(), 'options' => ['class' => 'form-control form-control-inline', 'prompt' => '选择省份'] ], 'city' => [ 'attribute' => 'city_id', 'items' => Region::getRegion($model->province_id), 'options' => ['class' => 'form-control form-control-inline', 'prompt' => '选择城市'] ], 'district' => [ 'attribute' => 'district_id', 'items' => Region::getRegion($model->city_id), 'options' => ['class' => 'form-control form-control-inline', 'prompt' => '选择县/区'] ], 'street' => [ 'attribute' => 'street_id', 'items' => Region::getRegion($model->district_id), 'options' => ['class' => 'form-control form-control-inline', 'prompt' => '选择街道'] ] ])->label(false); ?>
province 为省份配置,可用的选项可以查看 Html::dropdownList。如果不需要县/区或者街道,可以把 district 或者 street 删除。
可选功能:使用 behaviors 轻松获取省、市、区、街道的名称
你需要的 Model 主题 里添加 behaviors(注意:Model 主表字段不要命名为 province,会冲突,会导致此功能用不了,建议字段命名为 province_id;city 、district 和 street 字段同理)
/** * @inheritdoc */ public function behaviors() { return [ 'region' => [ 'class' => \yiier\region\RegionBehavior::className(), 'provinceAttribute' => 'provinceId', // 可选参数 默认 province_id 'cityAttribute' => 'cityId', // 可选参数 默认 city_id 'districtAttribute' => 'districtId', // 可选参数 默认 district_id 'streetAttribute' => 'streetId' // 可选参数 默认 street_id ], ]; }
然后可以通过下面方法获取到省、市、区、街道以及省市区街道的名称:
$model = Model::findOne($id); $model->province['name']; $model->city['name']; $model->district['name']; $model->street['name']; $model->fullRegion;
Credits
与之最大的区别是,数据源不同