<template> <!-- 組件屬性: selection:表格是否可勾選 querys: [搜索配置 { label: "所屬網關",//標題 name: 'gatewayId',//v-model的參數名 value: '',//v-model的參數值 type: "select",//控制項 ...
<template> <!-- 組件屬性: selection:表格是否可勾選 querys: [搜索配置 { label: "所屬網關",//標題 name: 'gatewayId',//v-model的參數名 value: '',//v-model的參數值 type: "select",//控制項類型 list: [//選項], attr: { multiple: false, disabled: false, clearable: false, }//綁定自定義屬性 }, ], columnlist: [//表格配置 { label: "是否確認",//表格的label prop: 'isConfirm',//表格的prop slot: true,//是否啟用插槽 filter: (item) => {//過濾函數,item是當前row return item == 0 ? '未確認' : '已確認' }, attr: {//自定義屬性 align: 'center', width: '100px' }, btns:[{ }]//按鈕 }, { label: "操作", prop: '', slot: true, attr: { align: 'center' }, btns: [{ label: '確認',//按鈕label fn: (row) => { //按鈕點擊事件回調 console.log(row, this.$refs.Tablez) this.$refs.Tablez.$emit('confirm', row); }, attr: { //自定義屬性 type: "primary", size: "mini", plain: true, } } ] }], serchbtns: [//搜索行操作按鈕 { label: '一鍵確認',//按鈕label fn: (rows) => {//按鈕點擊回調 console.log(rows, this.$refs.Tablez) this.allCheck(); }, attr: { //自定義屬性 type: "primary", size: "mini", plain: true, icon: "el-icon-check" } } ], --> <div> <el-row> <el-col :span="24"> <slot name="header"></slot> <slot name="querys"> <el-form ref="queryForm" size="small" :inline="true" label-width="auto" style="margin-top: 40px"> <el-form-item v-for="item, index in querys" :key="index" :label="item.label"> <template v-if="item.type == 'select'"> <el-select v-model="item.value" placeholder="請選擇" v-bind={...item.attr} @change="changed(item)"> <el-option v-for="i, index in item.list" :key="index" :label="i.label" :value="i.value"> </el-option> </el-select> </template> <template v-if="item.type == 'DatePicker'"> <el-date-picker v-model="item.value" type="datetime" placeholder="選擇日期時間"> </el-date-picker> </template> </el-form-item> <span> <el-button v-for="s,index in serchbtns" :key="index" v-bind="{...s.attr}" @click="s.fn()">{{ s.label }}</el-button> </span> </el-form> </slot> </el-col> <el-col :span="24"> <slot name="table"> <el-table v-loading="loading" :data="tablelist" stripe ref="tablelist" @selection-change="checkRow"> <el-table-column v-if="selection" type="selection" width="55" align="center" /> <el-table-column v-for="item, index in columnlist" :key="index" :label=item.label :prop="item.prop" v-bind={...item.attr}> <template slot-scope="{row}"> <div v-if="item.slot == false"> {{ row[item.prop] }} </div> <div v-if="item.slot && typeof (item.filter) == 'function'">{{ item.filter(row[item.prop]) }}</div> <div v-if="item.slot && item.btns && item.btns.length > 0"> <el-button v-for="i, index in item.btns" :key="index" v-bind="{ ...i.attr }" @click="i.fn(row)">{{ i.label }}</el-button> </div> </template> </el-table-column> </el-table> <pagination v-show="total > 0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" @pagination="getList(pageNum)" /> </slot> </el-col> </el-row> </div> </template> <script> export default { name: 'Tablez', props: { total:{ type:Number, default:0 }, pageNum:{ type:Number, default:1 }, pageSize:{ type:Number, default:10 }, selection: { //列表是否可勾選 type: Boolean, default: false }, querys: { //搜索表單配置 type: Array, default: () => { return [ { label: "所屬網關",//名字 name: 'getwayid',//v-model的key value: '',//v-model的value type: "select",//控制項類型 list: [//options { value: '選項1', label: '黃金糕' } ] }, { label: "所屬設備", name: 'deviceid', value: "", type: "select", list: [ { value: '選項2', label: '黃金糕2' } ] }, { label: '日期', name: 'date', value: "", type: 'DatePicker' } ] } }, serchbtns:{ type:Array, default:()=>{ return[ ] } }, loading: { type: Boolean, default: false }, tablelist: { type: Array, default: () => { return [] } }, columnlist: { type: Array, default: () => [] } }, data() { return { }; }, mounted() { }, methods: { changed(e) { console.log(e) this.$emit('changed', { name: e.name, value: e.value }) }, checkRow(e) { console.log(e) this.$emit('getRows',e) }, getList(e){ thie.$emit('pagination',e) } }, }; </script> <style scoped></style>
使用時按照註釋進行配置即可生成對應的表格和搜索框。
也可以使用內置的插槽進行自定義
serchbtns: [//搜索行操作按鈕 { label: '一鍵確認', fn: (rows) => { console.log(rows, this.$refs.Tablez) this.allCheck(); }, attr: { type: "primary", size: "mini", plain: true, icon: "el-icon-check" } } ], selection: true,//表格是否可勾選 columnlist: [ { label: '設備名稱', prop: 'deviceName', slot: false, attr: { align: 'center' } }, { label: "發生時間", prop: 'lastAlert', slot: false, attr: { align: 'center' } }, { label: "告警類型", prop: 'levelName', slot: false, attr: { align: 'center' } }, { label: "閾值", prop: 'threshold', slot: false, attr: { align: 'center' } }, { label: "觸發值", prop: 'value', slot: false, attr: { align: 'center' } }, { label: "物理量名稱", prop: 'displayName', slot: false, attr: { align: 'center' } }, { label: "是否確認", prop: 'isConfirm', slot: true, filter: (item) => { return item == 0 ? '未確認' : '已確認' }, attr: { align: 'center', width: '100px' } }, { label: "確認操作人", prop: 'nickName', slot: false, attr: { align: 'center', width: '100px' } }, { label: "操作", prop: '', slot: true, attr: { align: 'center' }, btns: [{ label: '確認', fn: (row) => { console.log(row, this.$refs.Tablez) this.$refs.Tablez.$emit('confirm', row); }, attr: { type: "primary", size: "mini", plain: true, } } ] }], querys: [ { label: "所屬網關",//標題 name: 'gatewayId',//v-model的參數名 value: '',//v-model的參數值 type: "select",//控制項類型 list: [ ], attr: { multiple: false, disabled: false, clearable: false, }//綁定自定義屬性 }, { label: "所屬設備", name: 'deviceId', value: "", type: "select", list: [ ] }, ],
比如這樣配置就可以生成
當然各位也可以在裡面進行自己的一些其他配置,也可以增加一些控制項。
如果覺得有用的話就拿去用把,別忘了點個贊哦
<template> <!-- 組件屬性: selection:表格是否可勾選 querys: [搜索配置 { label: "所屬網關",//標題 name: 'gatewayId',//v-model的參數名 value: '',//v-model的參數值 type: "select",//控制項類型 list: [//選項], attr: { multiple: false, disabled: false, clearable: false, }//綁定自定義屬性 }, ], columnlist: [//表格配置 { label: "是否確認",//表格的label prop: 'isConfirm',//表格的prop slot: true,//是否啟用插槽 filter: (item) => {//過濾函數,item是當前row return item == 0 ? '未確認' : '已確認' }, attr: {//自定義屬性 align: 'center', width: '100px'}, btns:[{
}]//按鈕 }, { label: "操作", prop: '', slot: true, attr: { align: 'center'
}, btns: [{ label: '確認',//按鈕label fn: (row) => { //按鈕點擊事件回調 console.log(row, this.$refs.Tablez) this.$refs.Tablez.$emit('confirm', row); }, attr: { //自定義屬性 type: "primary", size: "mini", plain: true, } } ] }], serchbtns: [//搜索行操作按鈕 { label: '一鍵確認',//按鈕label fn: (rows) => {//按鈕點擊回調 console.log(rows, this.$refs.Tablez) this.allCheck(); }, attr: { //自定義屬性 type: "primary", size: "mini", plain: true, icon: "el-icon-check" } } ], --> <div> <el-row> <el-col :span="24"> <slot name="header"></slot> <slot name="querys"> <el-form ref="queryForm" size="small" :inline="true" label-width="auto" style="margin-top: 40px"> <el-form-item v-for="item, index in querys" :key="index" :label="item.label"> <template v-if="item.type == 'select'"> <el-select v-model="item.value" placeholder="請選擇" v-bind={...item.attr} @change="changed(item)"> <el-option v-for="i, index in item.list" :key="index" :label="i.label" :value="i.value"> </el-option> </el-select> </template> <template v-if="item.type == 'DatePicker'"> <el-date-picker v-model="item.value" type="datetime" placeholder="選擇日期時間"> </el-date-picker> </template> </el-form-item> <span> <el-button v-for="s,index in serchbtns" :key="index" v-bind="{...s.attr}" @click="s.fn()">{{ s.label }}</el-button> </span> </el-form> </slot> </el-col> <el-col :span="24"> <slot name="table"> <el-table v-loading="loading" :data="tablelist" stripe ref="tablelist" @selection-change="checkRow"> <el-table-column v-if="selection" type="selection" width="55" align="center" /> <el-table-column v-for="item, index in columnlist" :key="index" :label=item.label :prop="item.prop" v-bind={...item.attr}> <template slot-scope="{row}"> <div v-if="item.slot == false"> {{ row[item.prop] }} </div> <div v-if="item.slot && typeof (item.filter) == 'function'">{{ item.filter(row[item.prop]) }}</div> <div v-if="item.slot && item.btns && item.btns.length > 0"> <el-button v-for="i, index in item.btns" :key="index" v-bind="{ ...i.attr }" @click="i.fn(row)">{{ i.label }}</el-button> </div> </template> </el-table-column> </el-table> <pagination v-show="total > 0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" @pagination="getList(pageNum)" /> </slot> </el-col> </el-row> </div> </template> <script> export default { name: 'Tablez', props: { total:{ type:Number, default:0 }, pageNum:{ type:Number, default:1 }, pageSize:{ type:Number, default:10 }, selection: { //列表是否可勾選 type: Boolean, default: false }, querys: { //搜索表單配置 type: Array, default: () => { return [ { label: "所屬網關",//名字 name: 'getwayid',//v-model的key value: '',//v-model的value type: "select",//控制項類型 list: [//options { value: '選項1', label: '黃金糕' } ] }, { label: "所屬設備", name: 'deviceid', value: "", type: "select", list: [ { value: '選項2', label: '黃金糕2' } ] }, { label: '日期', name: 'date', value: "", type: 'DatePicker' } ] } }, serchbtns:{ type:Array, default:()=>{ return[ ] } }, loading: { type: Boolean, default: false }, tablelist: {