fastadmin列表通用搜索如何加入城市选择器
发表于:2024-09-21 11:13:27浏览:225次
引言
fastadmin列表通用搜索如何加入城市选择器
效果
代码
前端
表格字段添加
{
field: '', title: __('地区'),
visible:false,
searchList: function () {
return '<div style="width:100%;">\n'+
'<div class="form-inline" data-toggle="cxselect" data-selects="province,city">\n' +
'<select class="province form-control" name="province" data-url="ajax/area_by_code"></select>\n'+
'<select class="city form-control" name="city" data-url="ajax/area_by_code"></select>\n'+
// '<select class="area form-control" name="area" data-url="ajax/area"></select>\n'+
// '<select class="town form-control" name="town" data-url="ajax/area"></select>\n'+
// '<select class="village form-control" name="village" data-url="ajax/area"></select>\n' +
'</div>\n'+
'</div>'
}
},
表格搜索参数自定义
queryParams:function(params){
var filter = params.filter ? JSON.parse(params.filter) : {}
var op = params.op ? JSON.parse(params.op) : {}
var province = $("select[name='province']").val()
var city_id = $("select[name='city']").val()
if (city) {
filter.city = city
op.city = '='
}
else if (province) {
filter.province = province
op.province = '='
}
// 重新赋值
params.filter = JSON.stringify(filter)
params.op = JSON.stringify(op)
console.log(params)
return params;
},
说明:
① province=省,city=城市,area=县,town=城镇,village=村
② 如果你数据库保存的是id,则调用data-url="ajax/area"
③ 如果你数据库保存的是code,则需自定义area_by_code
方法,调用data-url="ajax/area_by_code"
后端
路径application\admin\controller\Ajax.php
/**
* 读取省市区数据,联动列表
*/
public function area_by_code()
{
$params = $this->request->get("row/a");
if (!empty($params)) {
$province = isset($params['province']) ? $params['province'] : '';
$city = isset($params['city']) ? $params['city'] : null;
} else {
$province = $this->request->get('province');
$city = $this->request->get('city');
}
$where = ['level' => 1];
$provincelist = null;
if ($province !== '') {
if ($province) {
$where['provinceCode'] = $province;
$where['level'] = 2;
}
if ($city !== '') {
if ($city) {
$where['cityCode'] = $city;
$where['level'] = 3;
}
$provincelist = Db::name('area')->where($where)->field('code as value,name')->select();
}
}
$this->success('', null, $provincelist);
}
栏目分类全部>