vue3 elementPlus 表格實現(xiàn)行列拖拽及列檢索功能(完整代碼)
更新時間:2023年10月30日 14:35:44 作者:二進制旅者
本文通過實例代碼給大家介紹vue3 elementPlus 表格實現(xiàn)行列拖拽及列檢索功能,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
vue3 elementPlus 表格實現(xiàn)行列拖拽及列檢索功能

1、安裝vuedraggable
npm i -S vuedraggable@next
2、完整代碼
<template>
<div class='container'>
<div class="dragbox">
<el-table row-key="id" :data="tableData" :border="true">
<el-table-column
v-for="item in columnList"
:key="item.prop"
:prop="item.prop"
:label="item.label"
sortable
>
<template #header>
{{item.label}}
<el-popover
:visible="item.visible"
placement="bottom"
:width="200"
trigger="click"
>
<template #reference>
<el-button :type="item.input===''?'info':'primary'" link :icon="Search" @click.stop="item.visible = !item.visible"></el-button>
</template>
<div>
<el-input v-model="item.input" placeholder="請輸入" size="small" />
<div style="margin-top: 5px; display: flex; justify-content: space-between;">
<el-button size="small" type="primary" @click="searchItem(item)">查詢</el-button>
<el-button size="small" @click="resetItem(item)">重置</el-button>
</div>
</div>
</el-popover>
</template>
</el-table-column>
</el-table>
</div>
</div>
</template>
<script setup lang='ts'>
import { Search } from '@element-plus/icons-vue'
import Sortable from "sortablejs"
import { ref } from 'vue'
const tableData = ref([
{id: 1, name: '紙巾', type: '百貨', price: 30},
{id: 2, name: '抽紙', type: '百貨', price: 18},
{id: 3, name: '水杯', type: '百貨', price: 50},
])
const columnList = ref([
{label: '名稱', prop: 'name', input: '', visible: false},
{label: '類型', prop: 'type', input: '', visible: false},
{label: '價格', prop: 'price', input: '', visible: false},
])
onMounted(() => {
// 頁面加載完成執(zhí)行拖拽函數(shù)
rowDrop()
columnDrop()
})
// 列查詢
const searchItem = (item: any) => {
item.visible = false
console.log(item);
}
// 列查詢重置
const resetItem = (item: any) => {
item.visible = false
item.input = ''
console.log(item);
}
// 行拖拽
const rowDrop = () => {
const tbody = document.querySelector('.dragbox .el-table__body-wrapper tbody');
Sortable.create(tbody, {
draggable: ".dragbox .el-table__row",
onEnd(evt: any) {
const currRow = tableData.value.splice(evt.oldIndex, 1)[0];
tableData.value.splice(evt.newIndex, 0, currRow);
console.log(tableData.value);
}
});
}
// 列拖拽
const columnDrop = () => {
const tr = document.querySelector('.dragbox .el-table__header-wrapper tr');
Sortable.create(tr, {
animation: 150,
delay: 0,
onEnd: (evt: any) => {
const oldItem = columnList.value[evt.oldIndex];
columnList.value.splice(evt.oldIndex, 1);
columnList.value.splice(evt.newIndex, 0, oldItem);
console.log(columnList.value);
}
});
}
</script>
<style lang='scss' scoped>
.container {
height: 100vh;
.dragbox {
height: 100%;
}
}
</style> 到此這篇關(guān)于vue3 elementPlus 表格實現(xiàn)行列拖拽及列檢索功能的文章就介紹到這了,更多相關(guān)vue3 elementPlus表格行列拖拽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vuex state中的數(shù)組變化監(jiān)聽實例
今天小編就為大家分享一篇vuex state中的數(shù)組變化監(jiān)聽實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue之保留小數(shù)點兩位小數(shù) 使用filters(過濾器)
這篇文章主要介紹了vue之保留小數(shù)點兩位小數(shù) 使用filters(過濾器),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
vue中v-for通過動態(tài)綁定class實現(xiàn)觸發(fā)效果
這篇文章主要介紹了vue中v-for通過動態(tài)綁定class實現(xiàn)觸發(fā)效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-12-12

