当前位置:网站首页>Design of tablewithpage
Design of tablewithpage
2022-06-12 11:36:00 【dralexsanderl】
TableWithPage The design of the
The component is TTable、TPage The combination of , The specific documents refer to the respective components .
We found that for businesses that want to use forms , We usually need to use table and pagination. But in elementUI Document introduction , We can only manually import table and pagination Two components and merge them , Based on this idea , We decided to write a component that would table and pagination Integration , Write a form component with pagination .
Because a component can only have one root node , So the two components are one section The parcel , If you need to change the style, please add... To the component by yourself class、style( Will be set to the root element of the component )
render(h) {
return h(
'section',
null,
this.resolveChildren(h)
);
}
First, let's think about merging the two components , So how to define props Well , How will it be different props Values are passed to different components ?
props: {
data: {
type: Array,
required: true
},
// Used to add... To a table loading
loading: {
type: Boolean,
default: false
},
// Whether in current-page Reset table body when changing scrollTop
resetToTop: {
type: Boolean,
default: false
},
pageProps: {
type: Object,
default: () => ({
})
},
hasDelete: {
type: Boolean,
default: false
},
resetIndex: {
type: Boolean,
default: false
}
},
Of course, the most important data Yes ,loading For display loading style . Define one separately pageProps Used to receive incoming pagination Value . Considering that the drop-down box will appear when the form height is less than the content height , Whether to change the number of pages scrollTop Also reset , So an extra one is added resetToTop.
Next, let's look at how to generate two components , Because I defined one pageProps receive pagination Of props, So the other props By default, they are passed to table Of , So it's generating ttable Use of components attrs Receive other unused props
methods: {
resolveChildren(h) {
return [this.resolveTableChild(h), this.resolvePageChild(h)];
},
resolveTableChild(h) {
return h(
'TTable',
{
ref: 'table',
props: {
data: this.data },
attrs: {
...this.$attrs },
on: {
...this.$listeners },
scopedSlots: {
...this.$scopedSlots },
directives: [
{
name: 'loading',
expression: 'loading',
value: this.loading
}
]
}
);
},
}
about pagination nothing more , The required props It's all through pageProps Wear it , So we have to deal with the listener . Of course , Also handle incoming slot, The practice here is fixed slot For the name of the pagination. At the same time, you should also listen for changes in the number of pages .
resolvePageChild(h) {
const pageDefaultSlot = this.$scopedSlots.pagination;
const children = pageDefaultSlot ? pageDefaultSlot() : [];
const listeners = {
'current-change': this.handleCurrentChange
};
this.$listeners['size-change'] && (listeners['size-change'] = this.$listeners['size-change']);
this.$listeners['page-change'] && (listeners['page-change'] = this.$listeners['page-change']);
this.$listeners['prev-click'] && (listeners['prev-click'] = this.$listeners['prev-click']);
this.$listeners['next-click'] && (listeners['next-click'] = this.$listeners['next-click']);
return h(
'TPage',
{
ref: 'page',
props: {
...this.pageProps,
resetIndex: this.resetIndex
},
attrs: {
...this.pageProps },
on: {
'update:reset-index': this.updateResetIndex,
...listeners
}
},
children
);
},
handleCurrentChange(currentPage) {
if (this.resetToTop) {
// Find the corresponding element and scrollTop Value is set to 0
this.$refs.table.$el.querySelector('.el-table__body-wrapper').scrollTop = 0;
}
// Because tables also have current-change event , So change it pagination Of current-change The name of the event as a distinction
this.$emit('page-current-change', currentPage);
},
There is an extra one exposed here page-current-change Method to get a change in the number of pages .
slots
Table slots
Default slot 、 stay columns Slots declared in 、append Slots are passed to TTable Components
Paging slot
A named slot pagination, Will pass to TPage Components
events
$listeners Will be passed on to TTable Components
size-change、page-change,prev-click,next-click Will be passed on to TPage
because ElTable Also have current-change event , therefore TPage Of current-change Event changed to listen page-current-change
methods
Support all ElTable Methods
Example
A simple example ( With request )
<template>
<el-button type="primary" @click="handleSearch"> Search for </el-button>
<!--
first style Is to set the style of the component root element
the second height The attribute is Appoint Table Component's height prop
-->
<table-with-page
style="height: 100%;"
height="94%"
:data="tableData"
:columns="tableColumns"
:loading="tableLoading"
:page-props="pageProps"
@page-change="handlePageInfoChange"
>
<template v-slot:operate="{ row }">
<el-button type="text" @click="handleDelete(row)"> Delete </el-button>
</template>
</table-with-page>
</template>
<script>
export default {
data() {
return {
tableData: [],
tableColumns: [
{ type: 'index', label: ' Serial number ' },
{ prop: 'name', label: ' full name ' },
{ prop: 'age', label: ' Age ' },
{ slotName: 'operate', label: ' operation ' }
],
tableLoading: false,
total: 0,
hasDelete: false,
shouldResetIndex: false
}
},
computed: {
pageProps() {
const { total } = this;
return {
total,
align: 'center'
};
}
},
watch: {
// When paging information changes , Auto search
pageInfo(newVal) {
if (!newVal) {
return;
}
this.handleSearch();
}
},
methods: {
async handleDelete(row) {
try {
await SomeService.delete(row.id);
// It will trigger once page-change event , And then automatically get the data
this.hasDelete = true;
} catch (e) {
console.error(e);
}
}
handleSearch(e) {
if (e) {
// It will trigger once page-change event , And then automatically get the data , So you can just go back here
this.shouldResetIndex = true;
return;
}
const params = this.getParams();
this.search(params);
},
getParams() {
return { ...this.pageInfo };
},
async search(params) {
this.tableLoading = true;
try {
const { total, records } = await SomeService.search(params);
this.total = total;
this.tableData = records;
} catch(e) {
console.error(e);
}
this.tableLoading = false;
},
handlePageInfoChange(pageInfo) {
this.pageInfo = pageInfo;
}
}
}
</script>
Last
Source check :https://github.com/leopord-lau/Twpage
npm install
npm i twpage --save-dev
边栏推荐
- go基于阿里云实现发送短信
- 字节序 - 如何判断大端小端
- logrotate日志轮转方式create和copytruncate原理
- C# 36. DataGridView line number
- [cf1392d] D. Omkar and bed Wars
- UML series articles (30) architecture modeling -- product diagram
- Basic principle of Doppler effect
- Reentrantlock source code analysis
- manuscript手稿格式准备
- Shardingjdbc-5.1.0 monthly horizontal table splitting + read-write separation, automatic table creation and node table refresh
猜你喜欢

M-arch (fanwai 10) gd32l233 evaluation -spi drive DS1302

InfoQ geek media's 15th anniversary solicitation |position:fixed virtual button cannot take effect after being triggered. Problem analysis and Solution Exploration

UML series articles (30) architecture modeling -- product diagram

Selenium uses proxy IP

MATLAB中stairs函数使用

Using stairs function in MATLAB

套接字编程Udp篇

The reason why scanf return value is ignored and its solution

conda环境下pip install 无法安装到指定conda环境中(conda环境的默认pip安装位置)

【clickhouse专栏】基础数据类型说明
随机推荐
Simple solution of regular expression
Differences among various cross compiling tools of arm
rosbridge使用案例心得总结之_第26篇在同一个服务器上打开多个rosbridge服务监听端口
记录一下使用JPA时遇到的坑
Reading mysql45 lecture - self summary (part)
Using stairs function in MATLAB
[the 11th national competition of Blue Bridge Cup single chip microcomputer]
套接字编程Udp篇
VirtualBox virtual machine shut down due to abnormal system. The virtual machine startup item is missing
CLJ3-100ALH30剩余电流继电器
进程的创建和回收
selenium使用代理IP
Construction and construction of meta Universe System
Deep learning and CV tutorial (14) | image segmentation (FCN, segnet, u-net, pspnet, deeplab, refinenet)
十折交叉验证代码中的问题
架构训练模块 7
go基于腾讯云实现发送短信
Socket programming UDP
Problems in cross validation code of 10% discount
^33 variable promotion and function promotion interview questions