详解VUE对element-ui中的ElTableColumn扩展
公司有⼀个新的需求,点击ElTableColumn的头部可以进⾏搜索,这个功能同事已经做出来了,但是使⽤有些不⽅便,⾃⼰就打算封装成⼀个组件,学习⼀下。ElTableColumn本来是这个样⼦的:
要做成的是这个样⼦:
我直接就放代码了,挨着挨着说明太多了。
代码的结构:
组件
<!-- ElTableColumnPro.vue -->
<template>
<el-table-column v-if="visible" :formatter="formatter" :align='align' :prop="prop" :header-align="headerAlign" :label="label" :width="width" :render-header="renderHeader" >
<template slot-scope="scope">
<slot :row="w" :$index="scope.$index" >
<span>{{w[prop])}}</span>
</slot>
</template>
</el-table-column>
</template>
<script>
import moment from "moment";
export default {
name: "el-table-column-pro",
props: {
prop: {
type: String
},
label: {
type: String
},
width: {
type: Number
},
renderType: {
type: String,
validator: value => ["date", "input", "select"].includes(value)
},
placeholder: {
type: String
},
rederWidth: {
type: String,
default: "230px"
},
param: {
type: String,
default: ""
},
startDate: {
type: String
},
endDate: {
type: String
},
selectList: {
type: Array
},
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, column, cellValue)=>cellValue
},
align:{
},
headerAlign:{
type:String
}
},
data() {
return {
formatD:this.filterIcon
};
},
methods: {
fomatMethod(value){
return this.formatter('','',value)
},
renderHeader(createElement, { column, $index }) {
switch (derType) {
case "date":
derDate(createElement, { column, $index });    case "input":
derInput(createElement, { column, $index });    case "select":
derSelect(createElement, { column, $index });    default:
return column.label;
}
},
rederInput(createElement, { column, $index }) {
return createElement(
"div",
{
class: "filters",
style: {
color: lor
}
},
[
createElement(
"el-popover",
{
props: {
placement: "bottom",
width: "200",
trigger: "click"
}
},
[
createElement("el-input", {
props: {
placeholder: this.placeholder,
value: this.param
},
nativeOn: {
keyup: event => {
if (event.keyCode === 13) {
this.$emit("update:param", event.target.value);
this.callback && this.callback();
}
}
},
on: {
blur: event => {
this.$emit("update:param", event.target.value);
this.callback && this.callback();
}
}
}),
createElement(
"span",
{
slot: "reference"
},
[
column.label,
createElement("i", {
class: this.filterIcon,
style: {
marginLeft: "4px"
}
})
]
)
]
)
]
);
},
rederSelect(createElement, { column, $index }) {
return createElement(
"div",
{
class: "filters",
style: {
color: lor
}
},
[
createElement(
"el-popover",
{
props: {
placement: "bottom",
width: "200",
trigger: "click"
}
},
[
createElement(
"el-select",
{
props: {
placeholder: this.placeholder,
value: this.param,
clearable: this.isClear
},
input: value => {
this.$emit("update:param", value);
this.callback && this.callback();
}
}
},
[
this.selectList.map(item => {
return createElement("el-option", {
props: {
value: item.value,
label: item.label
}
});
})
]
),
createElement(
"span",
{
slot: "reference"
},
[
column.label,
createElement("i", {
class: this.filterIcon,
style: {
marginLeft: "4px"
}
})
]
)
]
)
]
);
},
renderDate(createElement, { column, $index }) {
return createElement(
"div",
{
class: "filters"
},
[
createElement(
columnspan是什么意思
"el-popover",
{
props: {
placement: "bottom",
width: derWidth,
trigger: "click"
}
},
[
createElement("el-date-picker", {
props: {
placeholder: this.placeholder,
value: this.value,
type: "daterange",
rangeSeparator:"⾄",
startPlaceholder:"开始⽇期",
endPlaceholder:"结束⽇期",
},
style: {
width: derWidth
},
on: {
input: value => {
if (value) {
const startDate = moment(value[0]).format("YYYY-MM-DD");          const endDate = moment(value[1]).format("YYYY-MM-DD");          this.$emit("update:startDate", startDate);
this.$emit("update:endDate", endDate);
this.callback && this.callback();
}
}
}
}),
createElement(
"span",
{
slot: "reference"
},
[
column.label,
createElement("i", {
class: this.filterIcon,
style: {
marginLeft: "4px"
}
})
]
)
]
)
]
);
}
}
};
</script>
<!-- index.js -->
import ElTableColumnPro from './ElTableColumnPro' ElTableColumnPro.install = function(Vue) {
Vueponent(ElTableColumnPro.name, ElTableColumnPro);
};
export default ElTableColumnPro;
安装
import ElTableColumnPro from 'components/ElTableColumnPro/index' ...
.
..
...
Vue.use(ElTableColumnPro)
使⽤
<el-table :data="bankFlow" stripe>
<el-table-column-pro :visible="showMore" prop="transactionId" label="流⽔号" :width="120"> </el-table-column-pro>
<el-table-column-pro prop="clientCode" label="客户代码 " :width="120" placeholder="请输⼊客户代码" :callback="requestTransactionLogs" renderType="input" :param.sync="request_params.clientCode"> </el-table-column-pro>
<el-table-column-pro prop="eventTypeName" label="事件 " placeholder="请选择事件"  :selectList="listEventEnum" :callback="requestTransactionLogs" renderType="select" :param.sync="request_params.event" :width="100"> </el-table-column-pro>
<el-table-column-pro prop="createTime" :callback="requestTransactionLogs" :startDate.sync="request_params.startDate" :endDate.sync="dDate" :formatter="$timeformat" label="时间" renderType="date" :width="180" ></el-table-column-p  </el-table>
<el-table :data="t" v-loading="loading" @row-dblclick="detail" >
<el-table-column-pro :width="120" prop="clientCode" label="客户代码 " align="center" header-align="center" placeholder="请输⼊客户代码" :callback="getLists" renderType="input" :param.sync="params.clientCode"></el-table-column-pro>
<el-table-column-pro label="内容 " placeholder="请输⼊内容" :callback="getLists" renderType="input" :param.sync="t">
<template slot-scope="scope">
<pre>{{t}}</pre>
</template>
</el-table-column-pro>
<el-table-column-pro prop="userName" :width="100" label="记录⼈ " align="center" header-align="center" placeholder="请输⼊记录⼈" :callback="getLists" renderType="input" :param.sync="params.userName"></el-table-column-pro>
<el-table-column prop="createTime" width="150" label="记录时间" align="center" header-align="center" :formatter="$dateTimeFormat"></el-table-column>
</el-table>
注释就不挨着打了....
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。