Distpicker是一个中国省市区三级联动选择组件,这个包是基于Distpicker的laravel-admin扩展,用来将Distpicker集成进laravel-admin的表单中
安装
composer require laravel-admin-ext/china-distpicker
然后
php artisan vendor:publish --tag=laravel-admin-china-distpicker
配置
在config/admin.php文件的extensions配置部分,加上属于这个扩展的配置
'extensions' => [ 'china-distpicker' => [ // 如果要关掉这个扩展,设置为false 'enable' => true, ] ]
使用
表单中使用
比如在表中有三个字段province_id, city_id, district_id, 在form表单中使用它:
$form->distpicker(['province_id', 'city_id', 'district_id']);
设置默认值
$form->distpicker([ 'province_id' => '省份', 'city_id' => '市', 'district_id' => '区' ], '地域选择')->default([ 'province' => 130000, 'city' => 130200, 'district' => 130203, ]);
可以设置每个字段的placeholder
$form->distpicker([ 'province_id' => '省', 'city_id' => '市', 'district_id' => '区' ]);
参考网站:https://github.com/laravel-admin-extensions/china-distpicker
默认存入数据库的是省市区的行政代码,而不是省市区的名字,Distpicker所使用的地域编码是基于国家统计局发布的数据, 数据字典为china_area.sql文件.
Grid 列表展示需要转换为省市区名字,代码如下:
//省名 $grid->column('province', __('Province')) ->display(function($code) { $name = DB::table('china_area')->where('code', $code)->value('name'); return $name; }); //市名 $grid->column('city', __('City')) ->display(function($code) { $name = DB::table('china_area')->where('code', $code)->value('name'); return $name; }); //区名 $grid->column('district', __('District')) ->display(function($code) { $name = DB::table('china_area')->where('code', $code)->value('name'); return $name; });
Detail 详情展示,代码如下:
$show->field('province', __('Province')) ->unescape()->as(function ($code) { $name = DB::table('china_area')->where('code', $code)->value('name'); return $name; }); $show->field('city', __('City')) ->unescape()->as(function ($code) { $name = DB::table('china_area')->where('code', $code)->value('name'); return $name; }); $show->field('district', __('District')) ->unescape()->as(function ($code) { $name = DB::table('china_area')->where('code', $code)->value('name'); return $name; });