记录:fastadmin 列表页表格实现动态列
后端代码
/**
* 商品库存余额表
*/
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代码
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);
},