使用ant design vue做一个选择不同选项展示不同表格的小例子(带筛选功能)
效果如下图所示。通过点击左侧的选项可以在右侧的表格展示对应选项包含的数据。页面代码如下:<template><div style="display: flex;"><a-card style="width: 20%;"><a-table:data-source="data":columns="columns1":customRow="Rowclick"&g
·
效果如下图所示。
通过点击左侧的选项可以在右侧的表格展示对应选项包含的数据。
页面代码如下:
<template>
<div style="display: flex;">
<a-card style="width: 20%;">
<a-table
:data-source="data"
:columns="columns1"
:customRow="Rowclick"
>
<template
slot="{title, filter}"
slot-scope="{ setSelectedKeys, selectedKeys, confirm, column }"
>
<a-input
v-ant-ref="c => (searchInput = c)"
:placeholder="`Search ${column.dataIndex}`"
:value="selectedKeys[0]"
style="width: 188px; margin-bottom: 8px;"
@change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
@pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
></a-input>
<a-button
type="primary"
icon="search"
size="small"
style="width: 90px; margin-right: 8px"
@click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
>
Search
</a-button>
</template>
<div
slot="filterDropdown"
slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
style="padding: 8px"
>
<a-input
v-ant-ref="c => (searchInput = c)"
:placeholder="`Search ${column.dataIndex}`"
:value="selectedKeys[0]"
style="width: 188px; margin-bottom: 8px; display: block;"
@change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
@pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
/>
<a-button
type="primary"
icon="search"
size="small"
style="width: 90px; margin-right: 8px"
@click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
>
Search
</a-button>
<a-button
size="small"
style="width: 90px"
@click="() => handleReset(clearFilters)"
>
Reset
</a-button>
</div>
<a-icon
slot="filterIcon"
slot-scope="filtered"
type="search"
:style="{ color: filtered ? '#108ee9' : undefined }"
/>
<template
slot="customRender"
slot-scope="text, record, index, column"
>
<span v-if="searchText && searchedColumn === column.dataIndex">
<template v-for="(fragment, i) in text
.toString()
.split(new RegExp(`(?<=${searchText})|(?=${searchText})`, 'i'))">
<mark
v-if="fragment.toLowerCase() === searchText.toLowerCase()"
:key="i"
class="highlight"
>{{ fragment }}</mark>
<template v-else>{{ fragment }}</template>
</template>
</span>
<template v-else>
{{ text }}
</template>
</template>
</a-table>
</a-card>
<a-card style="width: 80%;">
<a-table
:columns="columns2"
:data-source="selectData"
>
</a-table>
</a-card>
</div>
</template>
<script>
const data = [
{
key: "1",
name: "John Brown",
age: 32,
address: "New York No. 1 Lake Park",
},
{
key: "2",
name: "Joe Black",
age: 42,
address: "London No. 1 Lake Park",
},
{
key: "3",
name: "Jim Green",
age: 32,
address: "Sidney No. 1 Lake Park",
},
{
key: "4",
name: "Jim Red",
age: 32,
address: "London No. 2 Lake Park",
},
];
export default {
data() {
return {
data,
selectData: [],
searchText: "",
searchInput: null,
searchedColumn: "",
selectedKeys: [1, 2, 3, 4, 5],
columns1: [
{
title: "Name",
dataIndex: "name",
key: "name",
scopedSlots: {
filterDropdown: "filterDropdown",
filterIcon: "filterIcon",
customRender: "customRender",
},
onFilter: (value, record) =>
record.name.toString().toLowerCase().includes(value.toLowerCase()),
onFilterDropdownVisibleChange: (visible) => {
if (visible) {
setTimeout(() => {
this.searchInput.focus();
}, 0);
}
},
},
],
columns2: [
{
title: "Name",
dataIndex: "name",
},
{
title: "Age",
dataIndex: "age",
},
{
title: "Address",
dataIndex: "address",
},
],
};
},
methods: {
handleSearch(selectedKeys, confirm, dataIndex) {
confirm();
this.searchText = selectedKeys[0];
this.searchedColumn = dataIndex;
},
handleReset(clearFilters) {
clearFilters();
this.searchText = "";
},
Rowclick(record, index) {
return {
on: {
click: () => {
//此处需要注意$set的使用,直接替换的话是无法做到响应式的。
this.$set(this.selectData,0,record);
},
},
};
},
},
};
</script>
<style scoped>
</style>
更多推荐
已为社区贡献11条内容
所有评论(0)