feat: 表格选择器-多选添加form查询示例

This commit is contained in:
xiaoxian521 2024-01-24 15:48:22 +08:00
parent 44065a8978
commit 3b66ba9fa5
4 changed files with 111 additions and 20 deletions

View File

@ -1,8 +1,8 @@
// 完整版菜单比较多,将 rank 抽离出来,在此方便维护 // 完整版菜单比较多,将 rank 抽离出来,在此方便维护
const home = 0, // 平台规定只有 home 路由的 rank 才能为 0 ,所以后端在返回 rank 的时候需要从 1 开始哦 const home = 0, // 平台规定只有 home 路由的 rank 才能为 0 ,所以后端在返回 rank 的时候需要从 1 开始哦
able = 1, components = 1,
components = 2, able = 2,
table = 3, table = 3,
list = 4, list = 4,
result = 5, result = 5,
@ -23,8 +23,8 @@ const home = 0, // 平台规定只有 home 路由的 rank 才能为 0 ,所以
export { export {
home, home,
able,
components, components,
able,
table, table,
list, list,
result, result,

View File

@ -91,7 +91,8 @@ const tableDataEdit = clone(tableData, true).map((item, index) => {
return Object.assign(item, { return Object.assign(item, {
id: index + 1, id: index + 1,
date: `${dayjs(new Date()).format("YYYY-MM")}-${index + 1}`, date: `${dayjs(new Date()).format("YYYY-MM")}-${index + 1}`,
address: "China" address: "China",
sex: index % 2 === 0 ? "男" : "女"
}); });
}); });

View File

@ -1,10 +1,27 @@
import { message } from "@/utils/message"; import { message } from "@/utils/message";
import { tableDataEdit } from "../../data"; import { tableDataEdit } from "../../data";
import { ref, reactive, type Ref } from "vue"; import { type Ref, ref, reactive } from "vue";
import type { PaginationProps } from "@pureadmin/table"; import type { PaginationProps } from "@pureadmin/table";
import { cloneDeep, isAllEmpty } from "@pureadmin/utils";
export function useColumns(selectRef: Ref, tableRef: Ref) { export function useColumns(selectRef: Ref, formRef: Ref, tableRef: Ref) {
const tableData = ref(tableDataEdit);
const cloneTableData = cloneDeep(tableData.value);
const selectValue = ref([]); const selectValue = ref([]);
const searchForm = reactive({
sexValue: "",
searchDate: ""
});
const sexOptions = [
{
value: 0,
label: "男"
},
{
value: 1,
label: "女"
}
];
const columns: TableColumnList = [ const columns: TableColumnList = [
{ {
type: "selection", type: "selection",
@ -14,20 +31,24 @@ export function useColumns(selectRef: Ref, tableRef: Ref) {
{ {
label: "ID", label: "ID",
prop: "id", prop: "id",
width: 80 width: 50
},
{
label: "日期",
prop: "date",
minWidth: 120
}, },
{ {
label: "姓名", label: "姓名",
prop: "name" prop: "name"
}, },
{
label: "性别",
prop: "sex"
},
{ {
label: "地址", label: "地址",
prop: "address" prop: "address"
},
{
label: "日期",
prop: "date",
minWidth: 120
} }
]; ];
@ -36,7 +57,7 @@ export function useColumns(selectRef: Ref, tableRef: Ref) {
pageSize: 5, pageSize: 5,
currentPage: 1, currentPage: 1,
layout: "prev, pager, next", layout: "prev, pager, next",
total: tableDataEdit.length, total: tableData.value.length,
background: true, background: true,
small: true small: true
}); });
@ -49,9 +70,32 @@ export function useColumns(selectRef: Ref, tableRef: Ref) {
selectValue.value = arr; selectValue.value = arr;
}; };
const onSearch = () => {
tableData.value = cloneTableData;
if (!isAllEmpty(searchForm.sexValue)) {
let sex = sexOptions
.map(sex => sex.value === Number(searchForm.sexValue) && sex.label)
.filter(Boolean)[0];
tableData.value = tableData.value.filter(data => data.sex === sex);
}
if (!isAllEmpty(searchForm.searchDate)) {
tableData.value = tableData.value.filter(
data => data.date === searchForm.searchDate
);
}
pagination.total = tableData.value.length;
};
const onReset = () => {
formRef.value.resetFields();
onClear();
tableData.value = cloneTableData;
pagination.total = tableData.value.length;
};
const removeTag = ({ id }) => { const removeTag = ({ id }) => {
const { toggleRowSelection } = tableRef.value.getTableRef(); const { toggleRowSelection } = tableRef.value.getTableRef();
toggleRowSelection(tableDataEdit.filter(v => v.id == id)?.[0], false); toggleRowSelection(tableData.value.filter(v => v.id == id)?.[0], false);
}; };
const onClear = () => { const onClear = () => {
@ -67,12 +111,16 @@ export function useColumns(selectRef: Ref, tableRef: Ref) {
}; };
return { return {
searchForm,
sexOptions,
columns, columns,
pagination, pagination,
selectValue, selectValue,
tableDataEdit, tableData,
onSure, onSure,
onClear, onClear,
onReset,
onSearch,
removeTag, removeTag,
handleSelectionChange handleSelectionChange
}; };

View File

@ -2,18 +2,23 @@
import { ref } from "vue"; import { ref } from "vue";
import { useColumns } from "./columns"; import { useColumns } from "./columns";
const selectRef = ref(); const formRef = ref();
const tableRef = ref(); const tableRef = ref();
const selectRef = ref();
const { const {
searchForm,
sexOptions,
columns, columns,
pagination, pagination,
selectValue, selectValue,
tableDataEdit, tableData,
onClear,
onSure, onSure,
onClear,
onReset,
onSearch,
removeTag, removeTag,
handleSelectionChange handleSelectionChange
} = useColumns(selectRef, tableRef); } = useColumns(selectRef, formRef, tableRef);
</script> </script>
<template> <template>
@ -31,6 +36,42 @@ const {
> >
<template #empty> <template #empty>
<div class="m-4"> <div class="m-4">
<!-- <el-config-provider size="small"> -->
<el-form ref="formRef" :inline="true" :model="searchForm">
<el-form-item prop="sexValue">
<el-select
v-model="searchForm.sexValue"
class="!w-[120px]"
placeholder="请选择性别"
:teleported="false"
clearable
>
<el-option
v-for="item in sexOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item>
<el-form-item prop="searchDate">
<el-date-picker
v-model="searchForm.searchDate"
class="!w-[150px]"
type="date"
placeholder="请选择日期"
format="YYYY/MM/DD"
value-format="YYYY-MM-D"
/>
</el-form-item>
<el-form-item class="float-right !mr-0">
<el-button type="primary" text bg @click="onSearch">
查询
</el-button>
<el-button text bg @click="onReset"> 重置 </el-button>
</el-form-item>
</el-form>
<pure-table <pure-table
ref="tableRef" ref="tableRef"
row-key="id" row-key="id"
@ -40,7 +81,7 @@ const {
color: '#303133' color: '#303133'
}" }"
:data=" :data="
tableDataEdit.slice( tableData.slice(
(pagination.currentPage - 1) * pagination.pageSize, (pagination.currentPage - 1) * pagination.pageSize,
pagination.currentPage * pagination.pageSize pagination.currentPage * pagination.pageSize
) )
@ -59,6 +100,7 @@ const {
> >
确定 确定
</el-button> </el-button>
<!-- </el-config-provider> -->
</div> </div>
</template> </template>
</el-select> </el-select>