引用方式
推荐main.js
里全局引用:
import ElTableColumnPro from './components/ElTableColumnPro.vue';
ElTableColumnPro.install = function(Vue) {
Vue.component(ElTableColumnPro.name, ElTableColumnPro);
};
Vue.use(ElTableColumnPro);
然后引用页面直接在
<el-table-column-pro
:width="160"
prop="AssetCode"
label="设备编号 "
align="center"
header-align="center"
placeholder="请输入设备编号"
:callback="getTableDatas"
renderType="input"
:param.sync="AssetCode"
>
<template #content="scope">
<div class="text-center">
<router-link
:to="{ name: '资产管理详情', query: { id: scope.row.Id }}"
>{{scope.row.AssetCode}}</router-link>
</div>
</template>
</el-table-column-pro>
ElTableColumnPro.vue
<template>
<el-table-column
v-if="visible"
:formatter="formatter"
:align="align"
:prop="prop"
:header-align="headerAlign"
:label="label"
:width="width"
>
<template v-slot:header="slotProps">
<!-- input -->
<div v-if="renderType==='input'">
<span>{{label}}</span>
<el-popover v-model="popover" placement="bottom" width="220" trigger="click">
<div class="d-flex">
<el-input size="mini" :placeholder="placeholder" v-model="value" :clearable="isClear"></el-input>
<div class="search-popver">
<el-button type="primary" size="mini" @click="emitChange" icon="el-icon-search"></el-button>
</div>
</div>
<i slot="reference" class="cursor-point" :class="param?'filtered-color '+icon:icon"></i>
</el-popover>
</div>
<!-- select -->
<div v-if="renderType==='select'">
<span>{{label}}</span>
<el-popover v-model="popover" placement="bottom" width="200" trigger="click">
<div class="d-flex">
<el-select
size="mini"
v-model="value"
placeholder="请选择"
filterable
:clearable="isClear"
>
<el-option
v-for="(item,index) in selectList"
:label="item.name"
:value="item.value"
:key="index"
></el-option>
</el-select>
<div class="search-popver">
<el-button type="primary" size="mini" class="el-icon-search" @click="emitChange"></el-button>
</div>
</div>
<i slot="reference" class="cursor-point" :class="param?'filtered-color '+icon:icon"></i>
</el-popover>
</div>
<!-- Cascader -->
<div v-if="renderType==='cascader'">
<span>{{label}}</span>
<el-popover v-model="popover" placement="bottom" width="200" trigger="click">
<div class="d-flex">
<el-cascader
v-model="value"
size="mini"
placeholder="请选择"
clearable
:options="selectList"
:props="defaultProps"
:show-all-levels="false"
></el-cascader>
<div class="search-popver">
<el-button type="primary" size="mini" class="el-icon-search" @click="emitChange"></el-button>
</div>
</div>
<i slot="reference" class="cursor-point" :class="param?'filtered-color '+icon:icon"></i>
</el-popover>
</div>
<!-- datetimerange -->
<div v-if="renderType==='datetimerange'">
<span>{{label}}</span>
<el-popover v-model="popover" placement="bottom" width="430" trigger="click">
<div class="d-flex">
<!-- <el-input size="mini" :placeholder="placeholder" v-model="value" :clearable="isClear"></el-input> -->
<el-date-picker
size="mini"
:placeholder="placeholder"
v-model="value"
type="datetimerange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期"
:clearable="isClear"
></el-date-picker>
<div class="search-popver">
<el-button type="primary" size="mini" @click="emitChange" icon="el-icon-search"></el-button>
</div>
</div>
<i slot="reference" class="cursor-point" :class="param?'filtered-color '+icon:icon"></i>
</el-popover>
</div>
</template>
<template v-slot="scope">
<slot name="content" :row="scope.row">{{scope.row[prop]}}</slot>
</template>
</el-table-column>
</template>
<script>
export default {
name: "el-table-column-pro",
props: {
prop: {
type: String
},
label: {
type: String
},
width: {
type: Number
},
renderType: {
type: String,
validator: value =>
["datetimerange", "input", "select", "cascader"].includes(value)
},
placeholder: {
type: String
},
rederWidth: {
type: String,
default: "230px"
},
param: {
type: [String, Number, Array],
default: ""
},
startDate: {
type: String
},
endDate: {
type: String
},
selectList: {
type: Array
},
defaultProps: {
type: Object
},
isClear: {
type: Boolean,
default: true
},
visible: {
type: Boolean,
default: true
},
filterIcon: {
type: String,
default: "el-icon-search"
},
callback: {
type: Function
},
formatter: {
type: Function,
default: (row, cellValue) => cellValue
},
align: {
type: String
},
headerAlign: {
type: String
}
},
data() {
return {
icon: this.filterIcon,
value: this.param,
popover: false
};
},
methods: {
emitChange() {
this.$emit("update:param", this.value);
this.callback();
this.popover = false;
},
fomatMethod(value) {
return this.formatter("", "", value);
}
}
};
</script>
<style lang="less" scoped>
// table表头筛选颜色
.filtered-color {
color: var(--thFilterColor);
}
</style>