美文网首页
es5中的去重复数据以及el-table默认选中

es5中的去重复数据以及el-table默认选中

作者: 简小咖 | 来源:发表于2018-09-13 13:35 被阅读0次

    去重复数据

    初始化有一组数据,添加第二组数据时要判断数据是否存在,如果存在不插入,不存在的话插入这条新数据

    image.png
    • 方法一(推荐)
      初始化的数组为this.updateList
      查询的另一组数据为this.list
       this.list.forEach(item => {
            if (item.isSelect) {
              var hasItem = this.updateList.some(k => {
                return k.id === item.id
              })
              hasItem || this.updateList.push(item)
            }
          })
    
    • 方法二:reduce
      注意:这种方法也可以实现,但是看需求,可能最总的数据顺序有所打乱
    const list = response.data
    let hash = {};
    let config =this.updateList
    list.forEach(item => {
            iconfig.push(item)
     })
    const newArr = config.reduceRight((item, next) => {
        hash[next.id] ? '' : hash[next.id] = true && item.push(next);
        return item
    }, []);
    
    

    el-table默认选中

    官网的方法

        toggleSelection(rows) {
          if (rows) {
            rows.forEach(row => {
              this.$refs.multipleTable.toggleRowSelection(row, true)
             //this.$refs.multipleTable.toggleRowSelection(row)
            })
          } else {
            this.$refs.multipleTable.clearSelection()
          }
        },
    

    初始化的时候调用方法即可,vue中必须用.$nextTick可以实现,不然可能不起作用

       this.$nextTick(function() {
              this.toggleSelection(this.employeedetail.shops)
            })
    

    相关文章

      网友评论

          本文标题:es5中的去重复数据以及el-table默认选中

          本文链接:https://www.haomeiwen.com/subject/myjwgftx.html