Crud 增删改查
z-crud
组件插槽介绍
基础用法
筛选表达上下部分内容自定义可用
formTop
、formBottom
插槽。toolBar
组件上下左右内容自定义可用toolBarTop
、toolBarBottom
、toolBarLeft
、tableTitle
、toolBarBottom
插槽。pagination
组件上下左右内容自定义可用paginationTop
、paginationRight
、paginationBottom
、paginationLeft
插槽。
<script lang="ts" setup>
import { ref } from 'vue'
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',
},
detail: {
field: 'name',
label: '姓名',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
detail: {
field: 'gender',
label: '性别',
},
},
{
prop: 'age',
label: '年龄',
detail: {
field: 'time',
label: '出生日期',
},
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'date',
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,
date: '2020-01-01',
time: ['2020-01-01', '2020-01-02'],
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
date: '2012-01-01',
time: ['2020-01-01', '2020-01-02'],
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
date: '2018-01-01',
time: ['2020-01-01', '2020-01-02'],
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
date: '2028-01-01',
time: ['2020-01-01', '2020-01-02'],
},
]
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)
})
}
</script>
<template>
<z-crud
v-model:pagination="pagination"
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:options="options"
:columns="columns"
:request="request"
>
<template #formTop>
<div class="slot-demo">
formTop
</div>
</template>
<template #formBottom>
<div class="slot-demo">
formBottom
</div>
</template>
<template #toolBarTop>
<div class="slot-demo">
toolBarTop
</div>
</template>
<template #toolBarBottom>
<div class="slot-demo">
toolBarBottom
</div>
</template>
<template #toolBarRight>
<div class="slot-demo">
toolBarRight
</div>
</template>
<template #toolBarLeft>
<div class="slot-demo">
toolBarLeft
</div>
</template>
<template #tableTitle>
<div class="slot-demo">
tableTitle
</div>
</template>
<template #crudMiddle>
<div class="mb-4">
<el-card>crudMiddle</el-card>
</div>
</template>
<template #paginationTop>
<div class="mb-4">
<el-card>paginationTop</el-card>
</div>
</template>
<template #paginationBottom>
<div class="mb-4">
<el-card>paginationBottom</el-card>
</div>
</template>
<template #paginationLeft>
<div class="mb-4">
<el-card>paginationLeft</el-card>
</div>
</template>
<template #paginationRight>
<div class="mb-4">
<el-card>paginationRight</el-card>
</div>
</template>
</z-crud>
</template>
<style lang="scss">
.slot-demo {
padding: 10px;
border: 1px solid var(--el-card-border-color);
border-radius: var(--el-card-border-radius);
box-shadow: var(--el-box-shadow-light);
}
</style>
- 当
toolBar
和pagination
隐藏时,表格上下内容自定义可用tableTop
、tableBottom
插槽。
<script lang="ts" setup>
import { ref } from 'vue'
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',
},
detail: {
field: 'name',
label: '姓名',
},
},
{
prop: 'gender',
label: '性别',
search: {
component: 'select',
label: '性别',
field: 'gender',
},
detail: {
field: 'gender',
label: '性别',
},
},
{
prop: 'age',
label: '年龄',
detail: {
field: 'time',
label: '出生日期',
},
search: {
component: 'input',
label: '年龄',
field: 'age',
},
},
{
prop: 'date',
label: '出生日期',
},
])
const options = {
gender: [{ label: 'male', value: 'male' }, { label: 'female', value: 'female' }],
}
const request = ref({
searchApi: mockApi,
})
function mockApi() {
return new Promise((resolve) => {
setTimeout(() => {
const data = [
{
id: 1,
name: 'Steven',
gender: 'male',
age: 22,
date: '2020-01-01',
time: ['2020-01-01', '2020-01-02'],
},
{
id: 2,
name: 'Helen',
gender: 'male',
age: 12,
date: '2012-01-01',
time: ['2020-01-01', '2020-01-02'],
},
{
id: 3,
name: 'Nancy',
gender: 'female',
age: 18,
date: '2018-01-01',
time: ['2020-01-01', '2020-01-02'],
},
{
id: 4,
name: 'Jack',
gender: 'male',
age: 28,
date: '2028-01-01',
time: ['2020-01-01', '2020-01-02'],
},
]
resolve({
data: {
page: 1,
pageSize: 10,
total: 4,
list: data,
},
})
}, 100)
})
}
</script>
<template>
<z-crud
v-model:data="tableData"
v-model:formData="formData"
v-model:loading="loading"
:options="options"
:columns="columns"
:request="request"
:tool-bar="false"
>
<template #tableTop>
<div class="slot-demo">
tableTop
</div>
</template>
<template #tableBottom>
<div class="slot-demo">
tableBottom
</div>
</template>
</z-crud>
</template>
<style lang="scss">
.slot-demo {
padding: 10px;
border: 1px solid var(--el-card-border-color);
border-radius: var(--el-card-border-radius);
box-shadow: var(--el-box-shadow-light);
}
</style>
z-crud插槽
插槽名 | 说明 |
---|---|
append | 插入至表格最后一行之后的内容, 如果需要对表格的内容进行无限滚动操作,可能需要用到这个 slot。 若表格有合计行,该 slot 会位于合计行之上。 |
empty | 当数据为空时自定义的内容 |
tableTop | 顶部插槽 |
tableBottom | 底部插槽 |
toolBarTop | 工具栏顶部插槽 |
toolBarBottom | 工具栏底部插槽 |
toolBarRight | 工具栏右部插槽 |
toolBarLeft | 工具栏左侧左部插槽 |
tableTitle | 表格标题插槽 |
paginationTop | 分页顶部插槽 |
paginationBottom | 分页底部插槽 |
paginationLeft | 分页左侧插槽 |
paginationRight | 分页右侧插槽 |
formTop | 表单顶部插槽 |
formBottom | 表单底部插槽 |
crudMiddle | 中间内容插槽 |