fastadmin 列表页表格实现动态列

记录:fastadmin 列表页表格实现动态列

后端代码

php 复制代码
    /**
     * 商品库存余额表
     */
    public function kucunbalance()
    {
        $houseList = (new House)->where(['shop_id'=>SHOP_ID])->order('id desc')->field('name,id')->select();

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $filter = json_decode($this->request->get('filter'), true);          
            $whereparam = ['a.shop_id'=>SHOP_ID];
            if($filter){
                foreach($filter as $k=>$item){
                    if($k == 'goods_name'){
                        $whereparam['b.goods_name'] = ['like', '%'.$item.'%'];
                    }
                    if($k == 'code'){
                        $whereparam['b.code'] = ['like', '%'.$item.'%'];
                    }
                    if($k == 'category_name'){
                        $categoryIds = array_values((new Litestorecategory)->where(['shop_id'=>SHOP_ID,'name'=>$item])->column('id'));
                        $whereparam['b.category_id'] = ['in', $categoryIds];
                    }
                    if($k == 'unit_name'){
                        $unitIds = array_values((new Unit)->where(['shop_id'=>SHOP_ID,'name'=>$item])->column('id'));
                        $whereparam['b.unit_id'] = ['in', $unitIds];
                    }
                    if($k == 'goods_no'){
                        $whereparam['c.goods_no'] = ['like', '%'.$item.'%'];
                    }
                }
            }
            if($this->request->get('goods_name')){
                $whereparam['b.goods_name'] = ['like', '%'.$this->request->get('goods_name').'%'];
            }

            $list = $this->model->alias('a')
                ->join('litestore_goods b','a.goods_id = b.goods_id','left')
                ->join('litestore_goods_spec c','a.spec_sku_id = c.spec_sku_id AND a.goods_id = c.goods_id','left')
                ->where($whereparam)
                ->field('any_value(a.id) as id, a.goods_id, a.spec_sku_id, b.goods_name, b.code, b.category_id, b.unit_id, any_value(c.goods_no) as goods_no')
                ->group('a.goods_id, a.spec_sku_id')
                ->order(['a.goods_id'=>'asc', 'a.spec_sku_id'=>'asc', 'id'=>'asc'])
                ->paginate($limit);

            $erp = \think\Config::get("erp");
            $number_decimal = $erp['set']['number_decimal']['value'];
            $price_decimal = $erp['set']['price_decimal']['value'];

            $categoryList = (new Litestorecategory)->where(['shop_id'=>SHOP_ID])->order('id desc')->column('name', 'id');
            $unitList = (new Unit)->where(['shop_id'=>SHOP_ID])->order('id desc')->column('name', 'id');
            foreach ($list as &$row) {
                $row['category_name'] = $categoryList[$row['category_id']] ?? '';
                $row['unit_name'] = $unitList[$row['unit_id']] ?? '';
                $row['goods_attr'] = $row['spec_sku_id'] ? $this->goodspecmodel->spec_sku($row['spec_sku_id']) : '';
                $params = [
                    'goods_id'=>$row['goods_id'],
                    'spec_sku_id'=>$row['spec_sku_id'],
                ];
                $junjia = floatval($this->goodspecmodel->where(['goods_id'=>$row['goods_id'], 'spec_sku_id'=>$row['spec_sku_id']])->value('caigou_price'));
                // 全部仓库
                $total_ruku = $this->model->where($params)->sum('ruku_number');
                $total_chuku = $this->model->where($params)->sum('chuku_number');
                $row['all_number'] = floatval(bcsub($total_ruku.'', $total_chuku.'', $number_decimal));                
                $row['junjia'] = $junjia;
                $row['chengben'] = floatval(bcmul($row['junjia'].'', $row['all_number'].'', $price_decimal));
                // 仓库列表
                foreach ($houseList as $house) {
                    $ruku_number = $this->model->where($params)->where(['house_id'=>$house['id']])->sum('ruku_number');
                    $chuku_number = $this->model->where($params)->where(['house_id'=>$house['id']])->sum('chuku_number');
                    $number = floatval(bcsub($ruku_number.'', $chuku_number.'', $number_decimal)); 
                    $chengben = floatval(bcmul($junjia.'', $number.'', $price_decimal));
                    $row['number'.$house['id']] = $number;
                    $row['junjia'.$house['id']] = $junjia;
                    $row['chengben'.$house['id']] = $chengben;
                } 
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $rowspan = [
            ['field'=> 'category_name', 'title'=> __('Category_name'), 'rowspan'=> 2],
            ['field'=> 'code', 'title'=> __('Code'), 'operate'=> 'LIKE', 'rowspan'=> 2],
            ['field'=> 'goods_name', 'width'=>100, 'title'=> __('Goods_id'), 'operate'=> 'LIKE', 'rowspan'=> 2],
            ['field'=> 'goods_attr', 'title'=> __('Spec_sku_id'), 'searchable'=> false, 'rowspan'=> 2],
            ['field'=> 'unit_name', 'title'=> __('Unit_name'), 'rowspan'=> 2],
            ['field'=> 'all_cangku', 'title'=> '全部仓库', 'colspan'=> 3],        
        ];
        $rowlist = [
            ['field'=> 'all_number', 'title'=> '数量', 'searchable'=> false],        
            ['field'=> 'junjia', 'title'=> '单位成本', 'searchable'=> false],        
            ['field'=> 'chengben', 'title'=> '成本', 'searchable'=> false],        
        ];
        foreach ($houseList as $house) {
            $rowspan[] = ['field'=> $house['id'], 'title'=> $house['name'], 'colspan'=> 2];
            $rowlist[] = ['field'=> 'number'.$house['id'], 'title'=> '数量', 'searchable'=> false];
            $rowlist[] = ['field'=> 'chengben'.$house['id'], 'title'=> '成本', 'searchable'=> false];
        }        
        $this->assignconfig('rowspan',json_encode($rowspan));
        $this->assignconfig('rowlist',json_encode($rowlist));

        return $this->view->fetch();
    }

js代码

js 复制代码
        kucunbalance: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ck/baobiao/kucunbalance',
                    table: 'ck_churukulog',
                }
            });

            var table = $("#table");

            var rowspanstr = Config.rowspan;
            var jsObject = JSON.parse(rowspanstr);    //转换为json对象

            var rowspanstr2 = Config.rowlist;
            var jsObjects = JSON.parse(rowspanstr2);    //转换为json对象

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                // fixedColumns: true,
                // fixedRightNumber: 1,
                search:false,
                columns: [
                    jsObject,jsObjects
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
相关推荐
NotesChapter1 小时前
Android吸顶效果,并有着ViewPager左右切换
android
_祝你今天愉快2 小时前
分析android :The binary version of its metadata is 1.8.0, expected version is 1.5.
android
暮志未晚Webgl2 小时前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5
麦田里的守望者江3 小时前
KMP 中的 expect 和 actual 声明
android·ios·kotlin
Dnelic-3 小时前
解决 Android 单元测试 No tests found for given includes:
android·junit·单元测试·问题记录·自学笔记
佛系小嘟嘟3 小时前
Android Studio不显示需要的tag日志解决办法《All logs entries are hidden by the filter》
android·ide·android studio
mariokkm3 小时前
Django一分钟:django中收集关联对象关联数据的方法
android·django·sqlite
长亭外的少年4 小时前
如何查看 Android 项目的依赖结构树
android
深海呐6 小时前
Android 从本地选择视频,用APP播放或进行其他处理
android·音视频·从本地选择视频,用app播放·从本地选择视频,并拿到信息·跳转到本地视频列表
深海呐6 小时前
Android Google登录接入
android·google登录接入·android 谷歌登录接入·google登录·android google