fastadmin selectpage 编辑默认选中项

原因: 因为 selectpage 默认使用的 data-primary-key 为id, 上级默认使用pid
 
我的字段使用的不是id 和pid
 
所以导致问题出现(编辑页面不出现默认选中)

解决

 完整代码

 

<div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('Parent_name')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-inv_pcode" data-rule="required" data-source="user/user/indexSelectPage" data-field="nickname" class="form-control selectpage" name="row[inv_pcode]" data-primary-key="inv_code" type="text" value="{$row.inv_pcode|htmlentities}">
        </div>
</div>
  1. 第一步

  2. 重写 data-source 方法

  3. data-source="user/user/indexSelectPage"

重写indexSelectPage 将搜索指向更改 

public function indexSelectPage()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            $keyField = $this->request->request('keyField');
            if ($keyField) {
                return $this->selectpageSelect();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['group','invite','level'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
 
            return json($result);
        }
        return $this->view->fetch();
    }

 

第二步
将data-primary-key 限制成你搜索的字段
 
我的是 inv_code
 
data-primary-key="inv_code"
 
第三步
更改selectpageSelect方法  
方法来自  application\common\controller\Backend.php\selectpage
将pid更改成 inv_pcode
 

只需要修改下面两个地方就可以

protected function selectpageSelect()
    {
        //设置过滤方法
        $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
 
        //搜索关键词,客户端输入以空格分开,这里接收为数组
        $word = (array)$this->request->request("q_word/a");
        //当前页
        $page = $this->request->request("pageNumber");
        //分页大小
        $pagesize = $this->request->request("pageSize");
        //搜索条件
        $andor = $this->request->request("andOr", "and", "strtoupper");
        //排序方式
        $orderby = (array)$this->request->request("orderBy/a");
        //显示的字段
        $field = $this->request->request("showField");
        //主键
        $primarykey = $this->request->request("keyField");
        //主键值
        $primaryvalue = $this->request->request("keyValue");
        //搜索字段
        $searchfield = (array)$this->request->request("searchField/a");
        //自定义搜索条件
        $custom = (array)$this->request->request("custom/a");
        //是否返回树形结构
        $istree = $this->request->request("isTree", 0);
        $ishtml = $this->request->request("isHtml", 0);
        if ($istree) {
            $word = [];
            $pagesize = 999999;
        }
        $order = [];
        foreach ($orderby as $k => $v) {
            $order[$v[0]] = $v[1];
        }
        $field = $field ? $field : 'name';
 
        //如果有primaryvalue,说明当前是初始化传值
        if ($primaryvalue !== null) {
            $where = [$primarykey => ['in', $primaryvalue]];
            $pagesize = 999999;
        } else {
            $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
                $logic = $andor == 'AND' ? '&' : '|';
                $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
                $searchfield = str_replace(',', $logic, $searchfield);
                $word = array_filter(array_unique($word));
                if (count($word) == 1) {
                    $query->where($searchfield, "like", "%" . reset($word) . "%");
                } else {
                    $query->where(function ($query) use ($word, $searchfield) {
                        foreach ($word as $index => $item) {
                            $query->whereOr(function ($query) use ($item, $searchfield) {
                                $query->where($searchfield, "like", "%{$item}%");
                            });
                        }
                    });
                }
                if ($custom && is_array($custom)) {
                    foreach ($custom as $k => $v) {
                        if (is_array($v) && 2 == count($v)) {
                            $query->where($k, trim($v[0]), $v[1]);
                        } else {
                            $query->where($k, '=', $v);
                        }
                    }
                }
            };
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        $list = [];
        $total = $this->model->where($where)->count();
        if ($total > 0) {
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
 
            $fields = is_array($this->selectpageFields) ? $this->selectpageFields : ($this->selectpageFields && $this->selectpageFields != '*' ? explode(',', $this->selectpageFields) : []);
 
            //如果有primaryvalue,说明当前是初始化传值,按照选择顺序排序
            if ($primaryvalue !== null && preg_match("/^[a-z0-9_\-]+$/i", $primarykey)) {
                $primaryvalue = array_unique(is_array($primaryvalue) ? $primaryvalue : explode(',', $primaryvalue));
                //修复自定义data-primary-key为字符串内容时,给排序字段添加上引号
                $primaryvalue = array_map(function ($value) {
                    return '\'' . $value . '\'';
                }, $primaryvalue);
 
                $primaryvalue = implode(',', $primaryvalue);
 
                $this->model->orderRaw("FIELD(`{$primarykey}`, {$primaryvalue})");
            } else {
                $this->model->order($order);
            }
 
            $datalist = $this->model->where($where)
                ->page($page, $pagesize)
                ->select();
 
            foreach ($datalist as $index => $item) {
                unset($item['password'], $item['salt']);
                if ($this->selectpageFields == '*') {
                    $result = [
                        $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
                        $field      => isset($item[$field]) ? $item[$field] : '',
                    ];
                } else {
                    $result = array_intersect_key(($item instanceof Model ? $item->toArray() : (array)$item), array_flip($fields));
                }
                $result['inv_pcode'] = isset($item['inv_pcode']) ? $item['inv_pcode'] : (isset($item['parent_id']) ? $item['parent_id'] : 0);
                $list[] = $result;
            }
            if ($istree && !$primaryvalue) {
                $tree = Tree::instance();
                $tree->init(collection($list)->toArray(), 'inv_pcode');
                $list = $tree->getTreeList($tree->getTreeArray(0), $field);
                if (!$ishtml) {
                    foreach ($list as &$item) {
                        $item = str_replace('&nbsp;', ' ', $item);
                    }
                    unset($item);
                }
            }
        }
        //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
        return json(['list' => $list, 'total' => $total]);
    }

 最终效果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: b'fastadmin selectpage' 指的是 FastAdmin 框架中的一个功能,用于创建管理后台页面的选择器。该选择器可以让用户快速选择所需的内容,并显示相关的信息和操作选。要使用该功能,请打开 FastAdmin 后台,进入相关页面并按照指示操作即可。 ### 回答2: FastAdmin是一个基于ThinkPHP5和Bootstrap4的后台管理系统框架,它具有可扩展性和易用性。SelectPageFastAdmin中的一个插件,它提供了方便的下拉列表搜索功能。在FastAdmin中使用SelectPage可以让用户更快速地找到所需数据。 使用SelectPage插件的第一步是在FastAdmin后台菜单管理中添加一个搜索页面。在搜索页面中,可以定义搜索条件和搜索结果的展示方式。这里需要注意的是,搜索页面必须有一个关联到数据表的模型。 在搜索页面的模板中,可以添加SelectPage搜索框和相关代码。当用户在搜索框中输入关键字时,SelectPage将根据定义的搜索条件对数据表进行筛选,并在页面中展示搜索结果。此外,SelectPage还提供了可编辑的下拉列表,可以通过后台管理来配置下拉列表的选。 使用SelectPage可以让用户方便地在数据表中查找所需要的数据。同时,由于FastAdmin具有可扩展性,开发者也可以根据自己的需求对SelectPage进行自定义和扩展。 ### 回答3: FastAdmin是一个强大的后台开发框架,采用了前后端分离的设计思路,提供了一系列丰富的功能组件和插件,方便开发者快速构建高效稳定的后台管理系统。其中,selectpageFastAdmin框架中非常实用的一个组件之一,可以让用户在表单中快速选择相关数据,提高了数据操作的效率。 selectpage的主要特点包括: 1.支持数据异步加载,可以在输入框输入数据时,实时向后台请求并返回匹配的数据,极大地方便了用户的操作。 2.支持多选和单选模式,可以根据需要选择是否允许用户同时选择多个数据。 3.支持自定义显示字段,可以根据业务需求自由选择需要展示的字段内容。 4.支持分页查询,当数据量过大时,可以通过分页的方式快速定位到需要选择的数据,提高了用户的操作效率。 5.支持数据缓存,可以将查询到的数据缓存到本地,减少服务器端的查询压力。 在实际应用中,selectpage可以被广泛运用于需要选择关联数据的场景,例如选择所属部门、选择用户、选择商品等等。对于开发者来说,只需要简单地配置相应的参数即可,框架会自动完成数据加载、显示、缓存等工作,从而快速完成业务开发。总之,FastAdminselectpage组件极大地提高了后台管理系统的数据操作效率和用户体验,为开发人员带来了极大的便利。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值