2017 © Pedro Peláez
 

yii2-extension yii2-china-region

Yii2中国省市区三级联动

image

yiier/yii2-china-region

Yii2中国省市区三级联动

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

china region for Yii2

Yii2中国省市区街道四级联动, (*1)

Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

php composer.phar require --prefer-dist yiier/yii2-china-region "*"

or add, (*5)

"yiier/yii2-china-region": "*"

to the require section of your composer.json file., (*6)

Migrations

Run the following command, (*7)

php yii migrate --migrationPath=@yiier/region/migrations/

配置

在 controller 中添加以下 action, (*8)

public function actions()
{
    $actions = parent::actions();
    $actions['get-region'] = [
        'class' => \yiier\region\RegionAction::className(),
    ];
    return $actions;
}

使用

view 页面, (*9)

<?= 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 删除。, (*10)

, (*11)

, (*12)

可选功能:使用 behaviors 轻松获取省、市、区、街道的名称, (*13)

你需要的 Model 主题 里添加 behaviors(注意:Model 主表字段不要命名为 province,会冲突,会导致此功能用不了,建议字段命名为 province_id;city 、district 和 street 字段同理), (*14)

/**
* @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
        ],
    ];
}

然后可以通过下面方法获取到省、市、区、街道以及省市区街道的名称:, (*15)

$model = Model::findOne($id);
$model->province['name'];
$model->city['name'];
$model->district['name'];
$model->street['name'];
$model->fullRegion;

Credits

chenkby/yii2-region, (*16)

与之最大的区别是,数据源不同, (*17)

The Versions

11/06 2018

dev-master

9999999-dev

Yii2中国省市区三级联动

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2 region

08/06 2018

v1.1

1.1.0.0

Yii2中国省市区三级联动

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2 region

08/06 2018

v1.0

1.0.0.0

Yii2中国省市区三级联动

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2 region

08/06 2018

v0.1

0.1.0.0

Yii2中国省市区三级联动

  Sources   Download

BSD-4-Clause

The Requires

 

extension yii2 region