Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

Element-ui table splice移除数组元素 页面只会删除最后一行 视图和数据不对等(√已解决)

搭建Vue后台,用了vue-element-admin开源项目,里面的Element-ui table表格删除元素视图更新有误
<el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
    <el-table-column align="center" label="Actions" width="120">
        <template slot-scope="scope">
          <el-button-group>
            <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteList(scope.row.id, scope.$index)" />
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
</el-table>
这里我要删除操作,移除一行数组,于是获得了id和数组index
deleteList(id, index) {
  console.log(index)
  this.list.splice(index, 1)
}
操作完后,list数组是移除正确的,但是视图上把表格最后一行移除了,我很奇怪,难道element-ui的table遍历的数组对象不是我传递的list?不管我删除哪一个,视图上照样删除最后一行,甚至我重新调用接口赋值list也不行,视图和数据不对等

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

没毛病

<el-table v-loading="listLoading" :data="arr" border fit highlight-current-row style="width: 100%">
            <el-table-column align="center" label="Actions" width="120" prop="name">
            </el-table-column>
            <el-table-column align="center" label="Actions" width="120" prop="name">
                <template slot-scope="scope">
                <el-button-group>
                    <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteList(scope.row, scope.$index)" />
                </el-button-group>
                </template>
            </el-table-column>
        </el-table>
<script>
export default {
    data(){
        return{
            arr:[
                {
                    name:'fgf'
                },
                {
                    name:'fgfd'
                },
                {
                    name:'fgft'
                },
                {
                    name:'fgfb'
                },
                {
                    name:'fgfw'
                },
                {
                    name:'fgfy'
                }
            ]
        }
    },
    mounted(){

    },
    methods:{
        deleteList(item,i){
            this.arr.splice(i,1)
        }
    }
}
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...