This commit is contained in:
杰 朱
2021-10-20 19:42:28 +08:00
parent 9f2c936f8f
commit 8df04ff12e
2 changed files with 121 additions and 115 deletions

View File

@@ -1,53 +1,43 @@
<template> <template>
<div> <div>
<Table <Table border :data="filters" :columns="tableColumnsFilters"></Table>
border
:data="filters"
:columns="tableColumnsFilters"
>
</Table>
<Table <Table :show-header="false" border :data="data" :columns="columns"></Table>
:show-header=false
border
:data="data"
:columns="columns"
>
</Table>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
name: "FilterTable", name: "FilterTable",
props: { props: {
columns: Array, //列描述数据对象 columns: Array, //列描述数据对象
data: Array //表格数据 data: Array //表格数据
}, },
data() { data() {
return { return {
filters: [{ filters: [
title: '' {
}], title: ""
tableColumnsFilters: [],
search: {},//过滤条件保存的对象,就是保存Input框和Select中值
}
},
created() {
for (let index in this.columns) {
let filter = {};
/**
* 因为是采用的两个表的形式,过滤表中显示查询的Input,Select条件输组件,表头显示的是数据表的表头,渲染的数据是传入的columns中的filter字段
* 数据表隐藏表头,只显示数据,渲染的数据是传入的columns中的key值
*/
//将传入的列描述数据对象(columns) 的title和width 复制给 过滤表的列描述数据对象(tableColumnsFilters)
this.$set(filter, 'title', this.columns[index].title);
if (this.columns[index].width) {
this.$set(filter, 'width', this.columns[index].width);
} }
let render = (h) => {}; ],
/** tableColumnsFilters: [],
search: {} //过滤条件保存的对象,就是保存Input框和Select中值
};
},
created() {
for (let index in this.columns) {
let filter = {};
/**
* 因为是采用的两个表的形式,过滤表中显示查询的Input,Select条件输组件,表头显示的是数据表的表头,渲染的数据是传入的columns中的filter字段
* 数据表隐藏表头,只显示数据,渲染的数据是传入的columns中的key值
*/
//将传入的列描述数据对象(columns) 的title和width 复制给 过滤表的列描述数据对象(tableColumnsFilters)
this.$set(filter, "title", this.columns[index].title);
if (this.columns[index].width) {
this.$set(filter, "width", this.columns[index].width);
}
let render = h => {};
/**
* 需要有搜索的列, 只要在 列描述数据对象columns中配置 filter属性就行如下: * 需要有搜索的列, 只要在 列描述数据对象columns中配置 filter属性就行如下:
{ {
title: '名字', title: '名字',
@@ -65,18 +55,23 @@
} }
}, },
*/ */
//如果存在 过滤选项 //如果存在 过滤选项
if (this.columns[index].filter) { if (this.columns[index].filter) {
//过滤为 下拉选择框 //过滤为 下拉选择框
if (this.columns[index].filter.type && this.columns[index].filter.type === 'Select') { if (
render = (h) => { this.columns[index].filter.type &&
return h(this.columns[index].filter.type, { this.columns[index].filter.type != "Input"
) {
render = h => {
return h(
this.columns[index].filter.type,
{
props: { props: {
value: 0,//Select选项列表一般 0 为全部 value: 0, //Select选项列表一般 0 为全部
transfer: true, transfer: true
}, },
on: { on: {
'on-change': (val) => { "on-change": val => {
if (val === 0) { if (val === 0) {
//当选项是全部的时候删除 search中该字段的过滤条件 //当选项是全部的时候删除 search中该字段的过滤条件
this.$delete(this.search, this.columns[index].key); this.$delete(this.search, this.columns[index].key);
@@ -88,77 +83,87 @@
this.load(); this.load();
} }
} }
}, this.createOptionsRender(index, h)); },
} this.createOptionsRender(index, h)
} else { );
//如果是输入框 };
render = (h) => { }
//获取输入框的值,为了减少数据库的压力,我这里是 else {
//通过回车和点击搜索按钮才进行数据过滤查询,如果 //如果是输入框
//要输入框变化就进行过滤,把 this.load()放到 render = h => {
//input事件方法就行了 //获取输入框的值,为了减少数据库的压力,我这里是
let inputValue = {}; //通过回车和点击搜索按钮才进行数据过滤查询,如果
return h(this.columns[index].filter.type, { //要输入框变化就进行过滤,把 this.load()放到
props: { //input事件方法就行了
placeholder: '输入' + this.columns[index].title, let inputValue = {};
icon: 'ios-search-strong' return h(this.columns[index].filter.type, {
}, props: {
on: { placeholder: "输入" + this.columns[index].title,
input: val => { icon: "ios-search-strong",
inputValue = val; transfer: false
if (!inputValue) { },
this.validInputValue(index, inputValue); on: {
} input: val => {
}, inputValue = val;
//input框后面的搜索按钮的点击事件 if (!inputValue) {
'on-click': () => {
this.validInputValue(index, inputValue);
},
'on-enter': () => {
this.validInputValue(index, inputValue); this.validInputValue(index, inputValue);
} }
},
//input框后面的搜索按钮的点击事件
"on-click": () => {
this.validInputValue(index, inputValue);
},
"on-change": () => {
this.validInputValue(index, inputValue);
} }
})
};
}
}
this.$set(filter, 'render', render);
this.tableColumnsFilters.push(filter);
}
},
methods: {
createOptionsRender(index, h) {
//选项渲染
let optionRender = [];
if (this.columns[index].filter.option) {
let option = this.columns[index].filter.option;
for (let i in option) {
optionRender.push(h('Option', {
props: {
value: option[i].value
} }
}, option[i].name)) });
} };
} }
return optionRender;
},
//重新加载数据
load() {
//会执行一个load的事件
this.$emit('on-search',this.search);
},
//验证输入框的值
validInputValue(index, inputValue) {
if (!inputValue) {
this.$delete(this.search, this.columns[index].key);
this.load();
return;
}
this.$set(this.search, this.columns[index].key, inputValue);
this.load();
} }
this.$set(filter, "render", render);
this.tableColumnsFilters.push(filter);
}
},
methods: {
createOptionsRender(index, h) {
//选项渲染
let optionRender = [];
if (this.columns[index].filter.option) {
let option = this.columns[index].filter.option;
for (let i in option) {
optionRender.push(
h(
"Option",
{
props: {
value: option[i].value
}
},
option[i].name
)
);
}
}
return optionRender;
},
//重新加载数据
load() {
//会执行一个load的事件
this.$emit("on-search", this.search);
},
//验证输入框的值
validInputValue(index, inputValue) {
if (!inputValue) {
this.$delete(this.search, this.columns[index].key);
this.load();
return;
}
this.$set(this.search, this.columns[index].key, inputValue);
this.load();
} }
} }
};
</script> </script>
<style scoped> <style scoped>

View File

@@ -159,7 +159,7 @@ export default {
return <div> {dateFormat(params.row.dDate)} </div>; return <div> {dateFormat(params.row.dDate)} </div>;
}, },
filter: { filter: {
type: "Input" type: "DatePicker"
} }
}, },
{ {
@@ -386,6 +386,7 @@ export default {
console.log(search); console.log(search);
let _filert = { let _filert = {
abc:"123",
c_cus_abb_name: search.cCusAbbName, c_cus_abb_name: search.cCusAbbName,
d_date: search.dDate, d_date: search.dDate,
c_so_code: search.cSOCode, c_so_code: search.cSOCode,