iview 拖拽Table 表格实现手动排序

最近有个对指定列表手动排序的需求,项目用的是iview组件,看看了看官方 API 好像没有这样的例子,本以为要凉凉,结果一百度,发现 iview 其实提供了相应的方法,只是没写出一个可以直接用的DEMO而已,以后看文档开始要多想想哪个和哪个可以组合到一起,不然就受限于官方API提供的DEMO,这样很不好!

看看官方文档说了啥 ?!

  • 首先是 Table 的 draggable 属性,官方API 里给出的解释是:是否开启拖拽调整行顺序,需配合 @on-drag-drop 事件使用
  • 然后是 @on-drag-drop 事件 ,官方API的解释:拖拽排序松开时触发,返回置换的两行数据索引(index1,index2)

这其实就可以了嘛!

上菜 !

HTML

 <Table
     :columns="columns"
     :data="dataList"
     draggable
     @on-drag-drop="changeOrder"
     >
</Table>

JS

 changeOrder(oldIndex, newIndex) {
      oldIndex = parseInt(oldIndex)
      newIndex = parseInt(newIndex) 
      let oldData = this.dataList[oldIndex]
      this.dataList.splice(oldIndex, 1, this.dataList[newIndex])
      this.dataList.splice(newIndex, 1, oldData)
    }

顺序号直接使用索引就行了呗

下课!!!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下步骤来删除 Vue + iView 表格中的某一行数据: 1. 在表格中添加一个“删除”列,并在该列中添加一个“删除”按钮,例如,可以使用如下代码: ```html <template> <Table :columns="columns" :data="tableData"> <template slot="operations" slot-scope="{row}"> <Button type="error" @click="deleteRow(row)">删除</Button> </template> </Table> </template> <script> export default { data() { return { columns: [ { title: '姓名', key: 'name' }, { title: '年龄', key: 'age' }, { title: '性别', key: 'gender' }, { title: '操作', slot: 'operations', align: 'center' }, ], tableData: [ { name: '张三', age: 18, gender: '男' }, { name: '李四', age: 20, gender: '女' }, { name: '王五', age: 22, gender: '男' }, ], }; }, methods: { deleteRow(row) { const index = this.tableData.indexOf(row); this.tableData.splice(index, 1); }, }, }; </script> ``` 在上面的代码中,我们在表格中添加了一个名为“operations”的插槽,该插槽对应了一个“删除”按钮,当用户点击该按钮时,会触发 `deleteRow()` 方法,该方法会从表格数据源中删除指定的行。 2. 最后,如果你想要删除某一行数据时,弹出一个确认框,让用户确认是否删除,可以使用 iView 的 `Modal` 对话框组件,例如,可以使用如下代码: ```html <template> <Table :columns="columns" :data="tableData"> <template slot="operations" slot-scope="{row}"> <Button type="error" @click="showDeleteConfirm(row)">删除</Button> </template> </Table> </template> <script> import { Modal } from 'iview'; export default { data() { return { columns: [ { title: '姓名', key: 'name' }, { title: '年龄', key: 'age' }, { title: '性别', key: 'gender' }, { title: '操作', slot: 'operations', align: 'center' }, ], tableData: [ { name: '张三', age: 18, gender: '男' }, { name: '李四', age: 20, gender: '女' }, { name: '王五', age: 22, gender: '男' }, ], }; }, methods: { showDeleteConfirm(row) { Modal.confirm({ title: '确认删除', content: `是否删除 ${row.name}?`, onOk: () => { const index = this.tableData.indexOf(row); this.tableData.splice(index, 1); }, }); }, }, }; </script> ``` 在上面的代码中,我们使用了 iView 的 `Modal` 对话框组件,当用户点击“删除”按钮时,会弹出一个确认框,让用户确认是否删除该行数据。如果用户点击“确定”按钮,则会从表格数据源中删除该行数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值