Element: table表格如何正确获取当前row值

Created on 24 Oct 2016  ·  14Comments  ·  Source: ElemeFE/element

非常感谢饿了么团队开源这么好的基于vue的组件库,极大的提高了我们开发的效率。 :rose: :rose::rose:

我在学习的时候遇到一个问题,就是table实现查删增改功能,通过inline-template添加操作按钮,但是如何正确把当前行的值传过来呢?

        <el-table-column inline-template label="操作">
           <el-button-group>
             <el-button type="info" icon="edit" size="small"></el-button>
             <el-button type="danger" icon="delete" size="small" @click.native='deleteSelectTable(row)'></el-button>
           </el-button-group>
       </el-table-column>

  methods: {
      deleteSelectTable(row){
        this.tableData.splice(row,1)
        // console.log(row);
      }

console.log(row)能打印出选中当前row的对象,但是无论点哪个都是删除第一行

javascript· methods: { deleteSelectTable(row){ let index = this.tableData.indexOf(row) console.log(index); } }

点哪个按钮都是打印出-1,也就是按钮无法索引到当前行。(┬_┬)

认真看了文档和其它issue #408 #562 没有找到答案

另外,能否在https://github.com/ElemeFE/element/blob/master/examples/docs/zh-cn/table.md
文档中写一个类似的小demo演示呢,毕竟表格查删增改功能是很常见的需求。

question

Most helpful comment

@OYsun 之前版本有个设计失误,把用户返回的数据改了。 master 里面的代码应该已经没这个问题了,rc8 我们应该会在周五发布。

All 14 comments

哪个版本?

ElementUI version

"element-ui": "^1.0.0-rc.7",

OS/Browers version

macOS/Chrome 53

Vue version

"vue": "^2.0.1"

@furybean

@OYsun 之前版本有个设计失误,把用户返回的数据改了。 master 里面的代码应该已经没这个问题了,rc8 我们应该会在周五发布。

@furybean 刚才我把代码改成

 methods: {
      deleteSelectTable(row){
        this.tableData.splice(row.id,1)
        // console.log(row);
      }

发现console可以打印出选中的row.id,但是点击按钮有时候可以删除当前row,有时候却失效了。这是为什么呢◔ ‸◔?

Reproduction Link

http://web-oysun.cn/Bug-repro/

Reproduction code

https://github.com/OYsun/Bug-repro/blob/gh-pages/index.html

+1

@OYsun 这个例子是因为你漏了一个 id 为 2 的记录,我试一下 indexOf 的问题。

额,复制json数据粗心了,数据修正了,问题依然存在。

image
我是根据自定义的name属性删除的,还是跟pagination组件一起用,可以成功删除

版本是image

@OYsun 这个是之前这一行导致的:https://github.com/ElemeFE/element/pull/363/files#diff-2464594c163d1fc8302357129290d3d2L378

这个问题已经修复,会在 rc8 发出。

@furybean :+1: :rose:

估计 rc8之前只能给row中的obj做一个唯一表示,然后删除的时候
遍历

handleDel(row) {
  tableData.map((item, index) => {
    if(item.id == row.id) {
       tableData.splice(index, 1)
    }
  }) 
}

onRowClick 能不能获取当前行的index

scope.row.$index

Was this page helpful?
0 / 5 - 0 ratings