selectedMixin.js 2.32 KB
export default {
  methods: {
    handleSelectionServiceTable (selection, row, id) {
      if (selection && selection.find(item => item && item[id] == row[id])) {
        this.addRows(this.selectedList, [row], id)
      } else {
        this.selectedList = this.deleteRows(this.selectedList, [row], id)
      }
    },
    handleSelectionServiceTableAll (selected, id) {
      if (selected.length > 0) {
        this.addRows(this.selectedList, selected, id)
      } else {
        this.selectedList = this.deleteRows(this.selectedList, this.serviceList, id)
      }
    },
    //添加选中
    addRows (selectedItems, rows, val) {
      rows.forEach(row => {
        //过滤,当selectedItems有此条数据时,则直接返回,不执行添加
        if (selectedItems.find(item => item[val] == row[val])) {
          return
        }
        selectedItems.push(row)
      })
    },
    //取消选中
    deleteRows (selectedItems, rows, val) {
      //当selectedItems为空数组时,不执行删除
      if (selectedItems.length == 0) {
        return
      }
      rows.forEach(row => {
        selectedItems = selectedItems.filter(item => item[val] != row[val])
      })
      return selectedItems
    },
    //选中table已有数据
    selectedPreExisting (rows, list, val, refTabel) {
      refTabel.$refs['listMutiTable'].clearSelection()
      if (rows?.length) {
        //这里使用this.$nextTick是防止表格未渲染完成就执行,导致无法添加选中效果
        this.$nextTick(() => {
          rows.forEach(row => {
            //判断row是存在当前tableData
            list.forEach(i => {
              if (i[val] == row[val]) {
                refTabel.$refs['listMutiTable'].toggleRowSelection(i, true)
              }
            })
          })
        })
      } else {
        refTabel.$refs['listMutiTable'].clearSelection()
      }
    },
    setAmount (rows, list, val) {
      if (rows?.length) {
        this.$nextTick(() => {
          rows.forEach(row => {
            list.forEach(i => {
              if (i[val] == row[val]) this.$set(i, 'amount', row.amount)
            })
          })
        })
      }
    },
    changeAmount () {
      const [amount, , row, id] = arguments,
        { selectedList } = this,
        that = selectedList.find(e => e[id] == row[id])
      that.amount = amount
    }
  }
}