el-tabale 多选添加 分页 默认选中

el-tabale 多选添加 分页 默认选中

使用el-table进行多选添加,要求:已经添加的选项默认选中;可以多选进行添加;表格可搜索、可分页。


组建封装逻辑:father 组件通过 props 传参,传递已选中的 selectionGoods.sync 修饰方便更新数据。

1. 子组件 initData 初始化获取数据之后 通过 this.$nexttick(()=>{})el-table 进行默认选中。当前选中的项 包括三部分 :
  • el-table 当前页 选中项。–> selectionGoodsList
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectionGoodsList = selection
    }
  • father 组件通过 props 传递进来的 已添加项。 --> selectionGoods
 <AssociatedGoods :visible.sync="showGoodsDialog" :selectionGoods.sync="associatedGoods"></AssociatedGoods>
  • 临时存储 所有的 选中项(已选中项,当前页新增的选中项)。 --> currentSelect
2. 需要吧这三项进行合并去重,存储到 currentSelect . 然后根据 el-table 当前页的数据 过滤后 默认选中。默认选中会刷新 selectionGoodsList 为当前页面选中的数据。

切换界面的时候 需要刷新当前页的选中项 每一次刷新当前页的选中项 上一页的选中项 就会丢失。所以我们在进行默认选中之前 把所有的选中的项 合并去重 更新到 currentSelect 临时存储了起来。之后再进行 新页面的默认选中行为。就不会丢失新增的选中项了。

代码片段1:

    //初始化
    initData() {
      this.name = ''
      this.pageNum = 1
      this.pageSize = 10
      this.total = 0
      this.currentSelect = []
      this.selectionGoodsList = []
      this.getGoodsList()
    },
    //获取table数据
    async getGoodsList() {
      this.loading = true
      const params = {
        name: this.name,
        pageSize: this.pageSize,
        pageNum: this.pageNum
      }
      const response = await listAppGoods(params)
      this.goodsList = response.rows
      this.total = response.total
      this.loading = false
      this.defaultSelectionGoods()
    },
    //默认选中
    defaultSelectionGoods() {
      let selection = []
      //合并所有选中项  进行存储
      this.currentSelect = [...new Set([...this.currentSelect, ...this.selectionGoods, ...this.selectionGoodsList])]
      //过滤出当前页 可进行选中的项
      this.currentSelect.forEach(item => {
        const select = this.goodsList.filter(ele => {
          return item.id == ele.id
        })
        if (select.length > 0) {
          selection.push(...select)
        }
      })
      //进行默认选中。必须使用 $nexttick
      this.$nextTick(() => {
        selection.forEach(item => {
          this.$refs.dataTable.toggleRowSelection(item, true)
        })
      })
    },
3. 确认选中的时候 就更新所有的选中项 currentSelect 。就OK了,因为我的是 dialog 添加的。所以用了 .sync
 // 确认关联商品
    handleSubmit() {
      //商品原有的标签是否进行合并
      // const endList = [
      //   ...this.selectionGoods,
      //   ...this.selectionGoodsList
      // ]
      //
      // let newArr = [endList[0]]
      // let arrId = [endList[0].id]
      // for (let i = 1; i < endList.length; i++) {
      //   if (arrId.indexOf(endList[i].id) === -1) {
      //     newArr.push(endList[i])
      //     arrId.push(endList[i].id)
      //   }
      // }
      const newArr = [...new Set([...this.currentSelect, ...this.selectionGoods, ...this.selectionGoodsList])]
      this.$emit('update:selectionGoods', newArr)
      this.cancel()
    },
组件的使用和源码
 <AssociatedGoods :visible.sync="showGoodsDialog" :selectionGoods.sync="associatedGoods"></AssociatedGoods>
<template>
  <el-dialog
    title="提示"
    :visible.sync="visible"
    width="900px"
    append-to-body
    @opened="initData"
  >

    <el-row :gutter="10" style="display: flex;align-items: center;justify-content: flex-start">
      <el-col :span="3">商品名称:</el-col>
      <el-col :span="8">
        <el-input v-model="name" placeholder="请输入商品名称" @keydown.enter.native="searchGoods"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" @click="searchGoods">搜索</el-button>
      </el-col>
    </el-row>
    <div style="height: 20px"></div>
    <el-table
      ref="dataTable"
      v-loading="loading"
      :data="goodsList"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="商品图片" align="center" prop="name">
        <template v-slot="scope">
          <img
            width="60px"
            height="60px"
            :src="
              scope.row.appGoodsImgList.length > 0
                ? scope.row.appGoodsImgList[0].img
                : ''
            "
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="name"/>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 50, 100,200]"
      :page-size="pageSize"
      :total="total"
    >
    </el-pagination>

    <p slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </p>
  </el-dialog>
</template>

<script>
import { listAppGoods } from '@/api/goods/AppGoods'

export default {
  name: 'AssociatedGoods',
  props: {
    visible: {
      type: Boolean,
      default: () => false,
      require: true
    },
    selectionGoods: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      currentSelect: [],
      pageSize: 10,
      pageNum: 1,
      total: 0,
      goodsList: [],
      selectionGoodsList: [],
      name: '',
      loading: false
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.pageNum = 1
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getGoodsList()
    },
    defaultSelectionGoods() {
      let selection = []
      this.currentSelect = [...new Set([...this.currentSelect, ...this.selectionGoods, ...this.selectionGoodsList])]
      this.currentSelect.forEach(item => {
        const select = this.goodsList.filter(ele => {
          return item.id == ele.id
        })
        if (select.length > 0) {
          selection.push(...select)
        }
      })
      this.$nextTick(() => {
        selection.forEach(item => {
          this.$refs.dataTable.toggleRowSelection(item, true)
        })
      })
    },
    initData() {
      this.name = ''
      this.pageNum = 1
      this.pageSize = 10
      this.total = 0
      this.currentSelect = []
      this.selectionGoodsList = []
      this.getGoodsList()
    },
    searchGoods() {
      this.pageNum = 1
      this.getGoodsList()
    },
    async getGoodsList() {
      this.loading = true
      const params = {
        name: this.name,
        pageSize: this.pageSize,
        pageNum: this.pageNum
      }
      const response = await listAppGoods(params)
      this.goodsList = response.rows
      this.total = response.total
      this.loading = false
      this.defaultSelectionGoods()
    },
    cancel() {
      this.name = ''
      this.pageNum = 1
      this.pageSize = 10
      this.total = 0
      this.currentSelect = []
      this.selectionGoodsList = []
      this.$emit('update:visible', false)
    },
    // 确认关联商品
    handleSubmit() {
      //商品原有的标签是否进行合并
      // const endList = [
      //   ...this.selectionGoods,
      //   ...this.selectionGoodsList
      // ]
      //
      // let newArr = [endList[0]]
      // let arrId = [endList[0].id]
      // for (let i = 1; i < endList.length; i++) {
      //   if (arrId.indexOf(endList[i].id) === -1) {
      //     newArr.push(endList[i])
      //     arrId.push(endList[i].id)
      //   }
      // }
      const newArr = [...new Set([...this.currentSelect, ...this.selectionGoods, ...this.selectionGoodsList])]
      this.$emit('update:selectionGoods', newArr)
      this.cancel()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectionGoodsList = selection
    }

  }
}
</script>

<style scoped>

</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值