網(wǎng)站首頁 編程語言 正文
iview的表格實現(xiàn)單元格行編輯功能
截圖:
實現(xiàn)功能:
-
可分頁批量編輯并保存
-
輸入框支持上下鍵換行編輯
實現(xiàn)方式
使用插槽
<template>
<layout :footerHeight="0">
<template #Opration>
<div
style="
width: 100%;
height: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
"
>
<Button size="small" @click="cancelBatchEdit()" style="margin: 0 5px" v-if="age_is_edit"
>取消批量編輯</Button
>
<Button type="primary" size="small" @click="batchEdit()" style="margin: 0 5px" v-else
>批量編輯</Button
>
<Button type="primary" size="small" @click="batchSave()" style="margin: 0 5px"
>批量保存</Button
>
</div>
</template>
<template #main>
<div
style="
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
"
>
<div style="height: 92%; overflow: auto">
<Table
:data="results"
style="display: flex;height: 100%;"
:columns="[
{
title: '序號',
name: '序號',
key: 'index',
width: 90,
},
{
title: '年齡',
name: '年齡',
key: 'age',
isShow: 1,
width: 200,
resizable: 1,
sortable: 1,
slot: 'edit',
},
{
title: '備注',
name: '備注',
key: 'remark',
isShow: 1,
width: 200,
resizable: 1,
sortable: 1,
},
]"
>
<template #edit="{ row, column }">
<div
:class="column.key"
style="
cursor: pointer;
min-width: 120px;
min-height: 48px;
display: flex;
align-items: center;
"
>
<template v-if="age_is_edit">
<YsInput
@keyup.native="handleKeyup($event, row, column.key)"
v-model="row[column.key]"
@on-blur="changeValue(row, column, row[column.key], row._index)"
/>
</template>
<template v-else>
<span>{{ row[column.key] }}</span>
</template>
</div>
</template>
</Table>
</div>
<div
style="
width: 100%;
height: 8%;
padding: 15px;
display: flex;
justify-content: space-between;
align-items: center;
"
>
<span>共 {{ page.total }} 條</span>
<Page
:current.sync="args.page_para.page_index"
:total="page.total"
:page-size-opts="pageSizeOpts"
show-sizer
@on-page-size-change="handlePageSizeChange"
@on-change="handlePageChange"
/>
</div>
</div>
</template>
</layout>
</template>
<script>
import { differenceWith, isEqual, cloneDeep } from "lodash";
export default {
data() {
return {
age_is_edit: false,
pageSizeOpts: [10, 20, 30, 40],
args: {
conditions: {
},
page_para: {
page_index: 1,
page_size: 10,
},
},
page: {
total: 0,
currnt: 1,
pageSize: 10,
},
results: [],
originalResults: [],
editResults: [],
};
},
created() {
this.getData();
},
methods: {
cancelBatchEdit() {
this.age_is_edit = false;
this.args.page_para.page_index = 1;
this.getData(1);
},
batchEdit() {
this.age_is_edit = true;
},
async batchSave() {
let data = differenceWith(this.editResults, this.originalResults, isEqual);
data = cloneDeep(data);
let reg = /^[0-9]*[1-9][0-9]*$/;
this.batchSaveService.args.t_list = [];
if (data?.length) {
for (let i in data) {
if (data[i].age && !reg.test(data[i].age)) {
this.$Message.warning(`第${data[i].index}行的年齡請輸入數(shù)字!`);
return false;
}
}
this.age_is_edit = false;
await this.$http(data);
this.$Nessage.success("操作成功!");
this.args.page_para.page_index = 1;
this.getData(1);
} else {
this.$Message.info("無操作數(shù)據(jù)!");
}
},
handleKeyup(event, row, key) {
// ↑
if (event.keyCode === 38) {
if (row._index > 0) {
this.changeCurrentRow(row, key, "-");
}
}
// ↓
if (event.keyCode === 40) {
if (row._index + 2 <= this.page.pageSize) {
this.changeCurrentRow(row, key, "+");
}
}
},
changeCurrentRow(row, key, type) {
let curIndex = row._index;
if (type === "+") {
curIndex = row._index + 1;
} else {
curIndex = row._index - 1;
}
this.$set(this.results, row._index, row);
let changeRow = this.results[curIndex];
this.$set(this.results, curIndex, changeRow);
this.$nextTick(() => {
let e = document.querySelectorAll(`.${key}`)[curIndex];
e.querySelector('input').focus();
});
},
async getData(flag = 0) {
if (flag === 1) {
this.editResults = [];
this.originalResults = [];
}
let res = await this.$http({});
this.page.total = res.total;
let [pageIndex, pageSize] = [this.args.page_para.page_index, this.args.page_para.page_size];
if (res.total > 0) {
res.result.forEach((item, index) => {
// 序號
this.$set(item, "index", ((pageIndex - 1) * pageSize) + index + 1);
this.$set(item, "age_is_edit", false);
});
}
if (this.editResults.length < pageIndex * pageSize) {
// 當(dāng)前表格顯示數(shù)據(jù)
this.results = res.result;
// 編輯數(shù)據(jù)
this.editResults.push(...res.result);
// 原始數(shù)據(jù)
this.originalResults.push(...cloneDeep(res.result));
} else {
this.results = this.editResults.slice((pageIndex - 1) * pageSize, pageIndex * pageSize);
}
},
changeValue(row, column, val, index) {
if (val) {
if (!/^[0-9]*[1-9][0-9]*$/.test(val)) {
this.$Message.warning("請輸入數(shù)字!");
this.results[index][column.key] = val;
} else {
this.results[index][column.key] = parseFloat(val);
}
}
},
},
};
</script>
使用render函數(shù)
<template>
<layout>
<template #main>
<div
style="
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
"
>
<div style="height: 92%; overflow: auto">
<Table
:data="results"
style="display: flex;height: 100%;"
:columns="[
{
title: '序號',
name: '序號',
key: 'index',
width: 90,
},
{
title: '年齡',
name: '年齡',
key: 'age',
isShow: 1,
width: 200,
resizable: 1,
sortable: 1,
render: render,
},
{
title: '備注',
name: '備注',
key: 'remark',
isShow: 1,
width: 200,
resizable: 1,
sortable: 1,
},
]"
>
</Table>
</div>
<div
style="
width: 100%;
height: 8%;
padding: 15px;
display: flex;
justify-content: space-between;
align-items: center;
"
>
<span>共 {{ page.total }} 條</span>
<Page
:current.sync="args.page_para.page_index"
:total="page.total"
:page-size-opts="pageSizeOpts"
show-sizer
@on-page-size-change="handlePageSizeChange"
@on-change="handlePageChange"
/>
</div>
</div>
</template>
</layout>
</template>
<script>
export default {
data() {
return {
age_is_edit: false,
pageSizeOpts: [10, 20, 30, 40],
args: {
conditions: {
},
page_para: {
page_index: 1,
page_size: 10,
},
},
page: {
total: 0,
currnt: 1,
pageSize: 10,
},
results: [],
};
},
created() {
this.getData();
},
methods: {
render(h, { row, column, index }) {
return (
<div
style="cursor: pointer"
vOn:click={(e) => {
this.changeIndex(e, index, row)
}}>
{!row.nameIsEdit ? (
<span>{row[column.key]}</span>
) : (
<Input vModel={row[column.key]} vOn:on-blur={() => this.changeValue(row, column, row[column.key], index)} />
)}
</div>
)
},
async getData(flag = 0) {
let res = await this.$http({});
if (res.total > 0) {
res.result.forEach((item) => {
this.$set(item, 'nameIsEdit', false);
});
}
this.results = res.result;
},
changeValue(row, column, val, index) {
this.$set(row, 'nameIsEdit', false);
results[index][column.key] = val
},
changeIndex(e, index, row) {
this.flag = false;
this.$set(row, 'nameIsEdit', true);
//實現(xiàn)點擊后輸入框自動聚焦
this.$nextTick(() => {
e.currentTarget.getElementsByTagName("input")[0].focus();
});
},
},
};
</script>
原文鏈接:https://blog.csdn.net/qq_38157825/article/details/126931484
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-12-04 WxPython界面利用pubsub如何實現(xiàn)多線程控制_python
- 2022-06-30 go-micro集成RabbitMQ實戰(zhàn)和原理詳解_Golang
- 2022-04-25 C#使用Npoi導(dǎo)出Excel并合并行列_C#教程
- 2023-06-21 Python中sorted()用法案例代碼_python
- 2022-09-16 深度學(xué)習(xí)中shape[0]、shape[1]、shape[2]的區(qū)別詳解_python
- 2022-12-29 解決React?hook?'useState'?cannot?be?called?in?a?clas
- 2022-09-15 C++中的整形字節(jié)數(shù)_C 語言
- 2022-11-04 ASP.NET?MVC實現(xiàn)登錄后跳轉(zhuǎn)到原界面_實用技巧
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支