PHP建站技术分享-从入门到精通PHP建站技术分享-从入门到精通PHP建站技术分享-从入门到精通

QQ:420220301 微信/手机:150-3210-7690
当前位置:首页 > CMS教程 > Fastadmin

数据关联并树状显示选项,列表页替换显示(非一键crud关联)

管理员 2024-12-14
Fastadmin
7

涉及表:
1、系统表fa_category
TIM截图20190517165439.png

2、自建表fa_oa_member

DROP TABLE IF EXISTS `fa_oa_member`;CREATE TABLE `fa_oa_member` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL COMMENT '姓名', `depart_id` varchar(100) NOT NULL COMMENT '所在部门', `status` enum('hidden','normal') DEFAULT 'normal' COMMENT '状态', `zhiwu` varchar(100) DEFAULT NULL COMMENT '职务', `kpi_id` varchar(50) NOT NULL DEFAULT '' COMMENT '绩效制度', PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='公司成员';INSERT INTO `fa_oa_member` VALUES ('1', '小邓', '18', 'normal', '技术员', '22');INSERT INTO `fa_oa_member` VALUES ('2', '小张', '20', 'normal', '职员', '22');INSERT INTO `fa_oa_member` VALUES ('3', '小周', '26', 'normal', '', '22');INSERT INTO `fa_oa_member` VALUES ('4', '测试员1号', '16', 'normal', '总监', '21');INSERT INTO `fa_oa_member` VALUES ('5', '测试', '16', 'normal', '1', '21');

fa_category 建立
部门分类 type=depart
制度分类 type=zhidu
fa_oa_member一键crud (注意类似表名模块须自定义,去掉_,如oamember,否则自动生成model/oa/下的member.php,无法正常调用)
kpi_id,depart_id根据特性,将自动引入selectpage模块,数据源须手动修改add.html和edit.html
但此此模块无树状结构,用户体验极低,所以一步到位:引用fa的tree使待选数据树状化。
参考https://ask.fastadmin.net/article/8568.html
须修改controller=>修改内容为在此转换为code方便各位阅读
member.php

use appcommoncontrollerBackend;use fasttree;

.
.
.

public function _initialize(){    parent::_initialize();    $this->model = new appadminmodelOamember;    $this->view->assign("statusList", $this->model->getStatusList());

.
.
.

    $this->model_cata = model('appcommonmodelCategory');    $tree = Tree::instance();    $tree->init(collection($this->model_cata        ->order('weigh desc,id desc')        ->where(array("type"=>"depart","status"=>"normal"))        ->field(["id","name","type","flag","pid"])        ->select())        ->toArray(), 'pid');    $this->catalist = $tree->getTreeList($tree->getTreeArray(0), 'name');    $catadata =[];    foreach ($this->catalist as $k => $v)    {        $catadata[$v['id']] = $v;    }    $this->view->assign("dpList", $catadata);    $this->model_catb = model('appcommonmodelCategory');    $tree = Tree::instance();    $tree->init(collection($this->model_catb->order('weigh desc,id desc')->where(array("type"=>"zhidu","status"=>"normal"))->field(["id","name","type","flag","pid"])->select())->toArray(), 'pid');    $this->catblist = $tree->getTreeList($tree->getTreeArray(0), 'name');    $catbdata =[];    foreach ($this->catblist as $k => $v)    {        $catbdata[$v['id']] = $v;    }    $this->view->assign("zdList", $catbdata);}

.
.
.
view修改=>add.html

<div class="form-group">    <label class="control-label col-xs-12 col-sm-2">{:__('Depart_id')}:</label>    <div class="col-xs-12 col-sm-8">           <select id="c-depart_id" data-rule="required" class="form-control selectpicker" name="row[depart_id]">            {foreach name="dpList" item="vo"}            <option value="{$key}" {in name="key" value=""}selected{/in}>{$vo.name}</option>            {/foreach}       </select>    </div></div>

.
.
.

<div class="form-group">    <label class="control-label col-xs-12 col-sm-2">{:__('Kpi_id')}:</label>    <div class="col-xs-12 col-sm-8">        <select id="c-kpi_id" data-rule="required" class="form-control selectpicker" name="row[kpi_id]">            {foreach name="zdList" item="vo"}            <option value="{$key}" {in name="key" value=""}selected{/in}>{$vo.name}</option>            {/foreach}        </select>    </div></div>

view修改=>edit.html

    <div class="col-xs-12 col-sm-8">        <select id="c-depart_id" data-rule="required" class="form-control selectpicker" name="row[depart_id]">            {foreach name="dpList" item="vo"}            <option value="{$key}" {in name="key" value="$row.depart_id"}selected{/in}>{$vo.name}</option>            {/foreach}        </select>    </div>

.
.
.
.

    <div class="col-xs-12 col-sm-8">        <select id="c-kpi_id" data-rule="required" class="form-control selectpicker" name="row[kpi_id]">            {foreach name="zdList" item="vo"}            <option value="{$key}" {in name="key" value="$row.kpi_id"}selected{/in}>{$vo.name}</option>            {/foreach}        </select>    </div>

至此表格增改部门和绩效制度均关联category并树状显示如图:
image.png
image.png
但此时列表页结果显示的部门与绩效制度为数值,不便查阅,
image.png

要像增改时一样显示对应名字须进行如下操作:
1、修改controller=>member.php
引入查看方法index()并修改如下

 public function index(){    //设置过滤方法    $this->request->filter(['strip_tags']);    if ($this->request->isAjax()) {        //如果发送的来源是Selectpage,则转发到Selectpage        if ($this->request->request('keyField')) {            return $this->selectpage();        }        list($where, $sort, $order, $offset, $limit) = $this->buildparams();        $total = $this->model            ->where($where)            ->order($sort, $order)            ->count();        $list = $this->model            ->where($where)            ->order($sort, $order)            ->limit($offset, $limit)            ->select();         $rp_a = $this->model_cata                         ->where(array("type"=>"depart","status"=>"normal"))                  ->select();         $rp_atotal = $this->model_cata                 ->where(array("type"=>"depart","status"=>"normal"))                          ->count();         $rp_b = $this->model_cata                         ->where(array("type"=>"zhidu","status"=>"normal"))             ->select();         $rp_btotal = $this->model_cata             ->where(array("type"=>"zhidu","status"=>"normal"))                             ->count();         $rp_a = collection($rp_a)->toArray();         $rp_b = collection($rp_b)->toArray();         $list = collection($list)->toArray();                  for ($rpid =0; $rpid < $total; $rpid++) {                          for ($rpa =0; $rpa < $rp_atotal; $rpa++) {                 if ($list[$rpid]['depart_id'] == $rp_a[$rpa]['id']) {                      $list[$rpid]['depart_id'] = $rp_a[$rpa]['name'];                 }             }//替换部门显示             for ($rpb =0; $rpb < $rp_btotal; $rpb++) {                 if ($list[$rpid]['kpi_id'] == $rp_b[$rpb]['id']) {                     $list[$rpid]['kpi_id'] = $rp_b[$rpb]['name'];                 }            }//替换制度显示         }               $result = array("total" => $total, "rows" => $list);        return json($result);    }    return $this->view->fetch();}

至此替换显示完成如图:
image.png

没怎么发过分享,此次分享也是加强自己理解的机会,格式很乱,见谅。


希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。

相关推荐

扫码关注

qrcode

QQ交谈

回顶部