avue-crud多級復雜的動態表頭的實現示例
Avue.js 是基於現有的element-ui庫進行的二次封裝,從而簡化一些繁瑣的操作,核心理念為數據驅動視圖,主要的組件庫針對table表格和form表單場景,同時衍生出更多企業常用的組件,達到高復用,容易維護和擴展的框架,同時內置瞭豐富瞭數據展示組件,讓開發變得更加容易.
前言
在實際開發過程,需要多級復雜的動態表頭,表頭中的內容是動態填充的。以下是avuejs官網提供的負責表頭樣式,因此我後臺返回的數據,需要拼接avue-crud中的json格式。
實際開發中需要拼接成,column這種格式。
column: [{ label: '姓名', prop: 'name', width:140, }, { label: '性別1', prop: 'sex', }, { label: '自定義圖標', prop: 'icon', type: "icon", iconList: [{ label: '基本圖表', list: ['el-icon-time', 'el-icon-bell', 'el-icon-star-on'] }] }, { label: '復雜表頭', children: [{ label: '信息', children: [{ label: '年齡', prop: 'age' }, { label: '手機號', prop: 'phone', }] }, { label: '地區', prop: 'address', type: 'select', props: { label: 'name', value: 'code' }, dicUrl:'https://cli.avuejs.com/api/area/getProvince' }] }, { label: '測試', prop: 'test', }, { label: '手機信息1', prop: 'phone1' }], }, data: [{ name: '張三', age: 14, address: '110000', phone1: '17547400800', phone: '17547400800', icon: 'el-icon-time', test: 1, sex: '男' }, { name: '王五', age: 10, address: '120000', test: 1, sex: '女', icon: 'el-icon-star-on', phone1: '17547400800', phone: '17547400800' }]
後臺數據拼接
@GetMapping("/getTableHeader") public R getTableHeaderJson(){ Map<String,Object> map = new HashMap<>(); StringBuilder sb = new StringBuilder(); sb.append("{label: '工序',children: ["); List<FactoryProcess> processList = factoryProcessService.list(); for (int i = 0; i < processList.size(); i++) { String pid = processList.get(i).getProcessId(); sb.append("{label:'" + processList.get(i).getProcessName() + "',prop:'" + pid + "',"); sb.append("children:[{label: '單價',prop: 'price" + pid + "'}, " + "{label: '合計',prop: 'sum" + pid + "'},{label: '完成數量',prop: 'completeNum" + pid + "'}, " + "{label: '質檢合格數量',prop: 'qualifiedNum" + pid + "'}," + " {label: '質檢不合格數量',prop: 'unqualifiedNum" + pid + "'}, " + "{label: '報廢數量',prop: 'scrapNum" + pid + "'}]").append("},"); }; sb.append("]}"); map.put("cols",sb.toString()); return R.data(map); }
前臺數據展示
在created函數中加載表頭信息展示
// created生命周期函數中加載 created(){ this.getHeaderList(); }, //method中加載頁面展示的信息 methods: { getHeaderList(){ getHeaderFun().then(res => { this.option.column.push( { label: "產品名稱", prop: "productName", color:'#eef1f6' }, { label: "產品代號", prop: "productCode", }, { label: "材質", prop: "productMaterialStr", }, { label: "數量", prop: "sumNum", }, { label: "外協數量", prop: "outNum", }, { label: "外協完成量", prop: "outCompleteNum" }, { label: "總完成量", prop: "totalCompleteNum" } ) // 得到後臺拼接的信息 let objs = eval("("+res.data.data.cols+")"); this.option.column.push(objs); this.onLoad(this.page); }) }, }
加載表格中的數據
// 頁面數據的展示,需要於後臺請求的prop屬性對應 onLoad(page, params = {}) { this.loading = true; findProjectFormList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => { const data = res.data.data; this.page.total = data.total; //this.data = data.records; let records = data.records; let dataList = []; let processList = this.option.column[7].children; for(let i = 0; i < records.length; i++ ) { let obj = { 'productName' : records[i].productName, 'productCode' : records[i].productCode, 'productMaterialStr' : records[i].productMaterialStr, 'sumNum' : records[i].sumNum, 'outNum' : records[i].outNum, 'outCompleteNum' : records[i].outCompleteNum, 'totalCompleteNum': records[i].totalCompleteNum, } let processNumList = records[i].processNumList; for(let j = 0; j < processNumList.length; j++) { for (let k = 0; k < processList.length; k++) { if (processList[k].prop === processNumList[j].processCode) { // $set向相應的屬性prop中賦值 this.$set(obj, 'price'+processNumList[j].processCode, processNumList[j].processPrice); this.$set(obj, 'sum'+processNumList[j].processCode, processNumList[j].processTotal); this.$set(obj, 'completeNum'+processNumList[j].processCode, processNumList[j].completeNum); this.$set(obj, 'qualifiedNum'+processNumList[j].processCode, processNumList[j].qualifiedNum); this.$set(obj, 'unqualifiedNum'+processNumList[j].processCode, processNumList[j].unqualifiedNum); this.$set(obj, 'scrapNum'+processNumList[j].processCode, processNumList[j].scrapNum); this.$set(obj, 'shift'+processNumList[j].processCode, processNumList[j].shiftName); } } } dataList.push(obj); } this.data = dataList; this.loading = false; this.selectionClear(); }) },
頁面效果展示
到此這篇關於avue-crud多級復雜的動態表頭的實現示例的文章就介紹到這瞭,更多相關avue-crud多級復雜的動態表頭 內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- avue實現自定義搜索欄及清空搜索事件的實踐
- Avue和Element-UI動態三級表頭的實現
- vue 項目頁面卡死原因排查分析
- Element UI中table單元格合並的解決過程
- elementui中樹形表格切換展開不同層級的示例代碼