在使用fastadmin的过程中,可能出现多选需要排序的问题
比如在类型选择的时候选择的是如下顺序
但是保存后,去修改页面发现排序变成如下
如果需要改成按照开始的排序修改
在controller中重写selectpage方法即可
代码如下
/** * Selectpage搜索 * * @internal */ public function selectpage() { //设置过滤方法 $this->request->filter(['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 = 99999; } if($primaryvalue==""||$primaryvalue==null){ $order = "field(`{$primarykey}`, 0)"; } else{ $order = "field(`{$primarykey}`, {$primaryvalue})"; } $field = $field ? $field : 'name'; //如果有primaryvalue,说明当前是初始化传值 if ($primaryvalue !== null) { $where = [$primarykey => ['in', $primaryvalue]]; } else { $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) { $logic = $andor == 'AND' ? '&' : '|'; $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield; foreach ($word as $k => $v) { $query->where(str_replace(',', $logic, $searchfield), "like", "%{$v}%"); } 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); } $datalist = $this->model->where($where) ->orderRaw($order) ->page($page, $pagesize) ->field($this->selectpageFields) ->select(); foreach ($datalist as $index => $item) { unset($item['password'], $item['salt']); $list[] = [ $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '', $field => isset($item[$field]) ? $item[$field] : '', 'pid' => isset($item['pid']) ? $item['pid'] : 0 ]; } if ($istree) { $tree = Tree::instance(); $tree->init(collection($list)->toArray(), 'pid'); $list = $tree->getTreeList($tree->getTreeArray(0), $field); if (!$ishtml) { foreach ($list as &$item) { $item = str_replace(' ', ' ', $item); } unset($item); } } } //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮 return json(['list' => $list, 'total' => $total]); }
另外如果需要在index页面展示数据
需要在Model中添加
public function getTypenameAttr($value, $data){ $typenameArr = thinkDb::name("booktype") ->where("id", "in", $data["booktype_ids"]) ->orderRaw('field(id,'.$data["booktype_ids"].')') ->column("typename"); return implode(",", $typenameArr); }
在controller中添加
foreach ($list as $row) { $row->typename = $row->typename; }
在前台直接调用即可
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。