Crud 增删改查
z-crud
组件删除功能介绍。
基础用法
配置request
的deleteApi
字段,可以调用接口删除。参数默认为id
,也可以通过rowKey
配置。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import type { DeleteRequestApiParams } from 'ideaz-element'
interface RowData {
id: number
name: string
gender: string
age: number
time: string
}
const loading = ref(false)
const formData = ref({
name: '',
gender: '',
age: '',
})
const tableData = ref([])
const columns = ref([
{
prop: 'name',
label: '姓名',
search: {
component: 'input',
label: '姓名',
field: 'name',
},
add: {
component: 'input',
label: '姓名',
field: 'name',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
},
{
prop: 'age',
label: '年龄',
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'time',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const pagination = ref({
page: 1,
pageSize: 2,
total: 4,
})
const request = ref({
searchApi: mockApi,
deleteApi: deleteMockApi,
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
time: '2020-01-01',
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
time: '2012-01-01',
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
time: '2018-01-01',
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
time: '2028-01-01',
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data.slice((pagination.value.page - 1) * pagination.value.pageSize, pagination.value.page * pagination.value.pageSize),
},
})
}, 100)
})
}
function deleteMockApi(params: DeleteRequestApiParams<RowData>) {
console.log(JSON.stringify(params), 'deleteMockApi params')
return new Promise((resolve) => {
setTimeout(() => {
resolve({
message: 'success',
code: 200,
})
}, 100)
})
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:columns="columns"
:options="options"
:request="request"
:detail="false"
:add="false"
:edit="false"
/>
</template>
自定义删除
配置operate-delete
事件,可以自定义删除逻辑。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import type { CrudDeleteDialogConfirmParams, DeleteRequestApiParams } from 'ideaz-element'
interface RowData {
id: number
name: string
gender: string
age: number
time: string
}
const loading = ref(false)
const formData = ref({
name: '',
gender: '',
age: '',
})
const tableData = ref([])
const columns = ref([
{
prop: 'name',
label: '姓名',
search: {
component: 'input',
label: '姓名',
field: 'name',
},
add: {
component: 'input',
label: '姓名',
field: 'name',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
},
{
prop: 'age',
label: '年龄',
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'time',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const pagination = ref({
page: 1,
pageSize: 2,
total: 4,
})
const request = ref({
searchApi: mockApi,
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
time: '2020-01-01',
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
time: '2012-01-01',
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
time: '2018-01-01',
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
time: '2028-01-01',
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data.slice((pagination.value.page - 1) * pagination.value.pageSize, pagination.value.page * pagination.value.pageSize),
},
})
}, 100)
})
}
function deleteMockApi(params: DeleteRequestApiParams<RowData>) {
console.log(params, 'params')
return new Promise((resolve) => {
setTimeout(() => {
resolve({
message: 'success',
code: 200,
})
}, 100)
})
}
function handleDelete({ row }: { row: RowData }) {
window.ZDialogTip({
type: 'warning',
title: '提示',
message: '确定删除吗?',
onConfirm: async ({ confirmButtonLoading, done }: CrudDeleteDialogConfirmParams<RowData>) => {
confirmButtonLoading.value = true
try {
await deleteMockApi({ id: row.id })
confirmButtonLoading.value = false
done()
}
catch {}
confirmButtonLoading.value = false
},
})
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:columns="columns"
:options="options"
:request="request"
:detail="false"
:add="false"
:edit="false"
@operate-delete="handleDelete"
/>
</template>
delete
属性传入函数,可以直接覆盖delete
点击后内置的逻辑。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import type { CrudDeleteDialogConfirmParams } from 'ideaz-element'
interface RowData {
id: number
name: string
gender: string
age: number
time: string
}
const loading = ref(false)
const formData = ref({
name: '',
gender: '',
age: '',
})
const tableData = ref([])
const columns = ref([
{
prop: 'name',
label: '姓名',
search: {
component: 'input',
label: '姓名',
field: 'name',
},
add: {
component: 'input',
label: '姓名',
field: 'name',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
},
{
prop: 'age',
label: '年龄',
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'time',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const pagination = ref({
page: 1,
pageSize: 2,
total: 4,
})
const request = ref({
searchApi: mockApi,
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
time: '2020-01-01',
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
time: '2012-01-01',
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
time: '2018-01-01',
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
time: '2028-01-01',
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data.slice((pagination.value.page - 1) * pagination.value.pageSize, pagination.value.page * pagination.value.pageSize),
},
})
}, 100)
})
}
function deleteMockApi(params: { id: number }) {
console.log(params, 'params')
return new Promise((resolve) => {
setTimeout(() => {
resolve({
message: 'success',
code: 200,
})
}, 100)
})
}
function handleDelete({ row, getTableData }: { row: RowData, getTableData: () => void }) {
window.ZDialogTip({
type: 'warning',
title: '提示',
message: '确定删除吗?',
onConfirm: async ({ confirmButtonLoading, done }: CrudDeleteDialogConfirmParams<RowData>) => {
confirmButtonLoading.value = true
try {
await deleteMockApi({ id: row.id })
confirmButtonLoading.value = false
done()
getTableData()
}
catch {}
confirmButtonLoading.value = false
},
})
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:columns="columns"
:options="options"
:request="request"
:detail="false"
:add="false"
:edit="false"
:delete="handleDelete"
/>
</template>
delete
属性传入对象,可以直接自定义删除弹窗属性(属性详情可查看弹窗文档)。
<!-- eslint-disable unused-imports/no-unused-vars -->
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { h, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { CrudDeleteDialogConfirmParams, DeleteRequestApiParams } from 'ideaz-element'
interface RowData {
id: number
name: string
gender: string
age: number
time: string
}
const loading = ref(false)
const formData = ref({
name: '',
gender: '',
age: '',
})
const tableData = ref([])
const columns = ref([
{
prop: 'name',
label: '姓名',
search: {
component: 'input',
label: '姓名',
field: 'name',
},
add: {
component: 'input',
label: '姓名',
field: 'name',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
},
{
prop: 'age',
label: '年龄',
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'time',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const pagination = ref({
page: 1,
pageSize: 2,
total: 4,
})
const request = ref({
searchApi: mockApi,
deleteApi: deleteMockApi,
})
const deleteConfig = reactive({
message: ({ row }: { row: RowData }) => h('span', {}, `确认删除${row.name}吗?`),
onConfirm: async ({ done, confirmButtonLoading, row, tableRef, getTableData }: CrudDeleteDialogConfirmParams<RowData>) => {
confirmButtonLoading.value = true
try {
await deleteMockApi({ id: row?.id })
ElMessage.success('删除成功')
confirmButtonLoading.value = false
done()
getTableData()
}
catch (error) {
}
confirmButtonLoading.value = false
},
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
time: '2020-01-01',
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
time: '2012-01-01',
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
time: '2018-01-01',
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
time: '2028-01-01',
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data.slice((pagination.value.page - 1) * pagination.value.pageSize, pagination.value.page * pagination.value.pageSize),
},
})
}, 100)
})
}
function deleteMockApi(params: DeleteRequestApiParams<RowData>) {
console.log(params, 'params')
return new Promise((resolve) => {
setTimeout(() => {
resolve({
message: 'success',
code: 200,
})
}, 100)
})
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:columns="columns"
:options="options"
:request="request"
:delete="deleteConfig"
:detail="false"
:add="false"
:edit="false"
/>
</template>
多选删除
多选框和删除功能结合,组件内部会有el-alert
和批量删除等内置功能。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import type { DeleteRequestApiParams } from 'ideaz-element'
interface RowData {
id: number
name: string
gender: string
age: number
time: string
}
const loading = ref(false)
const formData = ref({
name: '',
gender: '',
age: '',
})
const tableData = ref([])
const columns = ref([
{
type: 'selection',
reserveSelection: true,
},
{
prop: 'name',
label: '姓名',
search: {
component: 'input',
label: '姓名',
field: 'name',
},
add: {
component: 'input',
label: '姓名',
field: 'name',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
},
{
prop: 'age',
label: '年龄',
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'time',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const pagination = ref({
page: 1,
pageSize: 2,
total: 4,
})
const request = ref({
searchApi: mockApi,
deleteApi: deleteMockApi,
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
time: '2020-01-01',
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
time: '2012-01-01',
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
time: '2018-01-01',
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
time: '2028-01-01',
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data.slice((pagination.value.page - 1) * pagination.value.pageSize, pagination.value.page * pagination.value.pageSize),
},
})
}, 100)
})
}
function deleteMockApi(params: DeleteRequestApiParams<RowData>) {
console.log(JSON.stringify(params), 'deleteMockApi params')
return new Promise((resolve) => {
setTimeout(() => {
resolve({
message: 'success',
code: 200,
})
}, 100)
})
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:columns="columns"
:options="options"
:request="request"
:detail="false"
:add="false"
:edit="false"
row-key="id"
/>
</template>
自定义多选删除弹窗内容。
<!-- eslint-disable unused-imports/no-unused-vars -->
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { h, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { CrudDeleteDialogConfirmParams, DeleteRequestApiParams } from 'ideaz-element'
interface RowData {
id: number
name: string
gender: string
age: number
time: string
}
const loading = ref(false)
const formData = ref({
name: '',
gender: '',
age: '',
})
const tableData = ref([])
const columns = ref([
{
type: 'selection',
reserveSelection: true,
},
{
prop: 'name',
label: '姓名',
search: {
component: 'input',
label: '姓名',
field: 'name',
},
add: {
component: 'input',
label: '姓名',
field: 'name',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
},
{
prop: 'age',
label: '年龄',
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'time',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const pagination = ref({
page: 1,
pageSize: 2,
total: 4,
})
const request = ref({
searchApi: mockApi,
deleteApi: deleteMockApi,
})
const deleteConfig = reactive({
message: ({ selectionData }: { selectionData: RowData[] }) => h('span', {}, `确认删除这${selectionData.length}条数据吗?`),
onConfirm: async ({ done, confirmButtonLoading, selectionData, tableRef, getTableData }: CrudDeleteDialogConfirmParams<RowData>) => {
confirmButtonLoading.value = true
try {
await deleteMockApi({ id: selectionData?.map(item => item.id) })
ElMessage.success('删除成功')
confirmButtonLoading.value = false
tableRef.clearSelection()
done()
getTableData()
}
catch (error) {
}
confirmButtonLoading.value = false
},
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
time: '2020-01-01',
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
time: '2012-01-01',
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
time: '2018-01-01',
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
time: '2028-01-01',
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data.slice((pagination.value.page - 1) * pagination.value.pageSize, pagination.value.page * pagination.value.pageSize),
},
})
}, 100)
})
}
function deleteMockApi(params: DeleteRequestApiParams<RowData>) {
console.log(params, 'params')
return new Promise((resolve) => {
setTimeout(() => {
resolve({
message: 'success',
code: 200,
})
}, 100)
})
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:columns="columns"
:options="options"
:request="request"
:delete="deleteConfig"
:detail="false"
:add="false"
:edit="false"
row-key="id"
/>
</template>
Alert配置
传入alert
对象配置el-alert
组件属性。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import type { DeleteRequestApiParams } from 'ideaz-element'
interface RowData {
id: number
name: string
gender: string
age: number
time: string
}
const loading = ref(false)
const formData = ref({
name: '',
gender: '',
age: '',
})
const tableData = ref([])
const columns = ref([
{
type: 'selection',
reserveSelection: true,
},
{
prop: 'name',
label: '姓名',
search: {
component: 'input',
label: '姓名',
field: 'name',
},
add: {
component: 'input',
label: '姓名',
field: 'name',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
},
{
prop: 'age',
label: '年龄',
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'time',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const pagination = ref({
page: 1,
pageSize: 2,
total: 4,
})
const request = ref({
searchApi: mockApi,
deleteApi: deleteMockApi,
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
time: '2020-01-01',
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
time: '2012-01-01',
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
time: '2018-01-01',
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
time: '2028-01-01',
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data.slice((pagination.value.page - 1) * pagination.value.pageSize, pagination.value.page * pagination.value.pageSize),
},
})
}, 100)
})
}
function deleteMockApi(params: DeleteRequestApiParams<RowData>) {
console.log(JSON.stringify(params), 'deleteMockApi params')
return new Promise((resolve) => {
setTimeout(() => {
resolve({
message: 'success',
code: 200,
})
}, 100)
})
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:columns="columns"
:options="options"
:request="request"
:detail="false"
:add="false"
:edit="false"
:alert="{ type: 'warning', description: 'description' }"
row-key="id"
/>
</template>
自定义Alert
如果需要自定义提示内容,alert
可以直接传入render
函数。如果不需要提示,可以传入false
。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { h, ref } from 'vue'
import type { DeleteRequestApiParams } from 'ideaz-element'
interface RowData {
id: number
name: string
gender: string
age: number
time: string
}
const loading = ref(false)
const formData = ref({
name: '',
gender: '',
age: '',
})
const tableData = ref([])
const columns = ref([
{
type: 'selection',
reserveSelection: true,
},
{
prop: 'name',
label: '姓名',
search: {
component: 'input',
label: '姓名',
field: 'name',
},
add: {
component: 'input',
label: '姓名',
field: 'name',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
},
{
prop: 'age',
label: '年龄',
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'time',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const pagination = ref({
page: 1,
pageSize: 2,
total: 4,
})
const request = ref({
searchApi: mockApi,
deleteApi: deleteMockApi,
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
time: '2020-01-01',
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
time: '2012-01-01',
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
time: '2018-01-01',
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
time: '2028-01-01',
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data.slice((pagination.value.page - 1) * pagination.value.pageSize, pagination.value.page * pagination.value.pageSize),
},
})
}, 100)
})
}
function deleteMockApi(params: DeleteRequestApiParams<RowData>) {
console.log(JSON.stringify(params), 'deleteMockApi params')
return new Promise((resolve) => {
setTimeout(() => {
resolve({
message: 'success',
code: 200,
})
}, 100)
})
}
function renderAlert(selectionData: RowData[]) {
return h('span', `已选中${selectionData.length}项`)
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:columns="columns"
:options="options"
:request="request"
:detail="false"
:add="false"
:edit="false"
:alert="renderAlert"
row-key="id"
/>
</template>
自定义提示内容也可以使用alert
插槽。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { ref } from 'vue'
import type { DeleteRequestApiParams } from 'ideaz-element'
interface RowData {
id: number
name: string
gender: string
age: number
time: string
}
const loading = ref(false)
const formData = ref({
name: '',
gender: '',
age: '',
})
const tableData = ref([])
const columns = ref([
{
type: 'selection',
reserveSelection: true,
},
{
prop: 'name',
label: '姓名',
search: {
component: 'input',
label: '姓名',
field: 'name',
},
add: {
component: 'input',
label: '姓名',
field: 'name',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
},
{
prop: 'age',
label: '年龄',
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'time',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const pagination = ref({
page: 1,
pageSize: 2,
total: 4,
})
const request = ref({
searchApi: mockApi,
deleteApi: deleteMockApi,
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
time: '2020-01-01',
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
time: '2012-01-01',
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
time: '2018-01-01',
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
time: '2028-01-01',
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data.slice((pagination.value.page - 1) * pagination.value.pageSize, pagination.value.page * pagination.value.pageSize),
},
})
}, 100)
})
}
function deleteMockApi(params: DeleteRequestApiParams<RowData>) {
console.log(JSON.stringify(params), 'deleteMockApi params')
return new Promise((resolve) => {
setTimeout(() => {
resolve({
message: 'success',
code: 200,
})
}, 100)
})
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:columns="columns"
:options="options"
:request="request"
:detail="false"
:add="false"
:edit="false"
row-key="id"
>
<template #alert="{ selectionData }">
已选中{{ selectionData.length }}项
</template>
</z-crud>
</template>
alert.title
和alert.description
支持传入render
函数自定义。
<!-- eslint-disable no-console -->
<script lang="ts" setup>
import { h, ref } from 'vue'
import type { DeleteRequestApiParams } from 'ideaz-element'
interface RowData {
id: number
name: string
gender: string
age: number
time: string
}
const loading = ref(false)
const formData = ref({
name: '',
gender: '',
age: '',
})
const tableData = ref([])
const columns = ref([
{
type: 'selection',
reserveSelection: true,
},
{
prop: 'name',
label: '姓名',
search: {
component: 'input',
label: '姓名',
field: 'name',
},
add: {
component: 'input',
label: '姓名',
field: 'name',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
},
{
prop: 'age',
label: '年龄',
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'time',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const pagination = ref({
page: 1,
pageSize: 2,
total: 4,
})
const request = ref({
searchApi: mockApi,
deleteApi: deleteMockApi,
})
const alertConfig = ref({
title: (selectionData: RowData[]) => h('span', `选中${selectionData.length}项`),
description: () => h('span', 'description'),
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
time: '2020-01-01',
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
time: '2012-01-01',
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
time: '2018-01-01',
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
time: '2028-01-01',
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data.slice((pagination.value.page - 1) * pagination.value.pageSize, pagination.value.page * pagination.value.pageSize),
},
})
}, 100)
})
}
function deleteMockApi(params: DeleteRequestApiParams<RowData>) {
console.log(JSON.stringify(params), 'commonApi params')
return new Promise((resolve) => {
setTimeout(() => {
resolve({
message: 'success',
code: 200,
})
}, 100)
})
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:columns="columns"
:options="options"
:request="request"
:detail="false"
:add="false"
:edit="false"
:alert="alertConfig"
row-key="id"
/>
</template>
z-crud删除相关属性
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
delete | 删除配置 | boolean / ({ row, tableRef, getTableData }) => void / object | true |
action | 操作项是否展示(内置的删除、编辑等按钮) | boolean | true |
edit | 编辑配置 | boolean / object | true |
add | 新增配置 | boolean / object | true |
detail | 详情配置 | boolean / object / ({ row, tableRef }) => void | true |
search | 查询配置 | boolean / object | |
alert | 固定的选中数据提示 | object / boolean | true |
request | 接口配置 | object | — |
request属性
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
searchApi | 查询接口 | (params: any) => promise | — |
submitApi | 编辑新增确认 | ({ [key: string]: any, row: any, type: 'add' / 'edit' / 'view', formData: any }) => promise | — |
deleteApi | 删除接口 | ({ [key: string]?: any, row?: any, selectionData?: any }) => promise | — |
addApi | 新增接口 | ({ type: 'add' / 'edit' / 'view', formData: any }) => promise | — |
editApi | 编辑接口 | ({ [key: string]: any, row: any, type: 'add' / 'edit' / 'view', formData: any }) => promise | — |
detailApi | 详情接口 | ({ [key: string]: any, row: any }) => promise | — |
alias | 数据路径自定义 | object | — |
beforeData | 表格数据接口调用前的回调 | Function | — |
afterData | 表格数据接口调用后的回调 | (res) => void | — |
searchFunc | 查询方法重写 | ({ params }) => any | — |
tableData | 表格数据自定义返回 | (res) => any | — |
alert属性
名称 | 说明 | 类型 | 默认值 | 必填 |
---|---|---|---|---|
title | Alert 标题。 | string / (selectionData, tableRef) => VNode | — | 否 |
type | Alert 类型。 | success / warning / info / error | info | 否 |
description | 描述性文本 | string / () => VNode | — | 否 |
closable | 是否可以关闭 | boolean | true | 否 |
center | 文字是否居中 | boolean | false | 否 |
close-text | 自定义关闭按钮文本 | string | — | 否 |
effect | 主题样式 | light / dark | 'light' | 否 |
onClose | 关闭 Alert 事件 | Function | — | 否 |
z-crud删除相关事件
事件名 | 说明 | 类型 |
---|---|---|
operate-delete | 删除表格数据 | ({ selectionData, row, table, getTableData }) => void |
z-crud删除相关插槽
插槽名 | 说明 |
---|---|
alert | 选中数据时表格顶部固定提示内容 |