vue 使用 sortable 實(shí)現(xiàn) el-table 拖拽排序功能
本文給大家介紹vue 使用 sortable 實(shí)現(xiàn) el-table 拖拽排序功能,具體內(nèi)容如下所示:
npm 下載:
npm install sortablejs --save
引入:
import Sortable from 'sortablejs';
代碼:
<template> <div class='table'> <el-table ref='dragTable' :data='tableData' border :row-class-name='tableRowClassName'> <el-table-column prop='date' label='日期'></el-table-column> <el-table-column prop='name' label='姓名'></el-table-column> <el-table-column prop='address' label='地址'></el-table-column> <el-table-column label='操作'> <template> <el-button type='text' size='small'>拖 拽</el-button> </template> </el-table-column> </el-table> </div></template><script>import Sortable from 'sortablejs';export default { data() { return { tableData: [ { id: '1', name: 'text_1', date: '1111-11-11', address: '測(cè)試_1', }, { id: '2', name: 'text_2_不可拖拽', date: '2222-22-22', address: '測(cè)試_2_不可拖拽', disabled: true, }, { id: '3', name: 'text_3', date: '3333-33-33', address: '測(cè)試_3', }, { id: '4', name: 'text_4', date: '4444-44-44', address: '測(cè)試_4', }, { id: '5', name: 'text_5', date: '5555-55-55', address: '測(cè)試_5', }, ], }; }, methods: { // 創(chuàng)建sortable實(shí)例 initSortable() { // 獲取表格row的父節(jié)點(diǎn) const ele = this.$refs.dragTable.$el.querySelector( '.el-table__body > tbody' ); // 創(chuàng)建拖拽實(shí)例 let dragTable = Sortable.create(ele, { animation: 150, //動(dòng)畫(huà) handle: '.move', //指定拖拽目標(biāo),點(diǎn)擊此目標(biāo)才可拖拽元素(此例中設(shè)置操作按鈕拖拽) filter: '.disabled', //指定不可拖動(dòng)的類名(el-table中可通過(guò)row-class-name設(shè)置行的class) dragClass: 'dragClass', //設(shè)置拖拽樣式類名 ghostClass: 'ghostClass', //設(shè)置拖拽??繕邮筋惷?chosenClass: 'chosenClass', //設(shè)置選中樣式類名 // 開(kāi)始拖動(dòng)事件 onStart: () => { console.log('開(kāi)始拖動(dòng)'); }, // 結(jié)束拖動(dòng)事件 onEnd: (e) => { console.log( '結(jié)束拖動(dòng)', `拖動(dòng)前索引${e.oldIndex}---拖動(dòng)后索引${e.newIndex}` ); }, }); }, // 設(shè)置表格row的class tableRowClassName({ row }) { if (row.disabled) { return 'disabled'; } return ''; }, }, mounted() { this.initSortable(); },};</script><style lang=’scss’>// 拖拽.dragClass { background: rgba($color: #41c21a, $alpha: 0.5) !important;}// ???ghostClass { background: rgba($color: #6cacf5, $alpha: 0.5) !important;}// 選擇.chosenClass:hover > td { background: rgba($color: #f56c6c, $alpha: 0.5) !important;}</style>
到此這篇關(guān)于vue 使用 sortable 實(shí)現(xiàn) el-table 拖拽排序功能的文章就介紹到這了,更多相關(guān)vue實(shí)現(xiàn) el-table 拖拽排序內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python excel和yaml文件的讀取封裝2. 如何理解PHP核心特性命名空間3. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問(wèn)題及解決4. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊5. python操作數(shù)據(jù)庫(kù)獲取結(jié)果之fetchone和fetchall的區(qū)別說(shuō)明6. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫(huà)水平條形圖案例7. python中PyQuery庫(kù)用法分享8. python 讀取二進(jìn)制 顯示圖片案例9. .net6 在中標(biāo)麒麟下的安裝和部署過(guò)程10. 開(kāi)發(fā)效率翻倍的Web API使用技巧
