vue实现列表排序的效果

16 篇文章 2 订阅

前言:

        实现element的列表动态排序效果。

效果图:

实现步骤:

一级页面中直接展示:

1、页面列表:

    <el-table
          v-if="uploadTable"
          ref="productOrderTable"
          :data="productOrderData"
          @selection-change="selectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column label="顺序" align="center" min-width="100">
            <template slot-scope="{ row }">
              <span>{{ row.productOrder }}</span>
            </template>
          </el-table-column>
        </el-table>

2、js方法:

// 商品顺序
    selectionChange() {
      const selArr = this.$refs.productOrderTable.selection
      this.productOrderData.forEach(item => {
        item.productOrder = null
      })
      let newArr = []
      selArr.forEach((item,i)=>{
        newArr.push({
          id:item.id,
          index:i+1
        })
      })

      this.productOrderData.forEach(one=>{
        newArr.forEach(two=>{
          if(one.id === two.id){
            one.productOrder = two.index
          }
        })
      })
    },

如果该列表在弹框中:

页面方法:

<el-dialog
  title="商品顺序"
  :visible.sync="productOrderVisible"
  width="80%"
  append-to-body>

        <el-table
          v-if="uploadTable"
          ref="productOrderTable"
          :data="productOrderData"
          @selection-change="selectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column label="顺序" align="center" min-width="100">
            <template slot-scope="{ row }">
              <span>{{ row.productOrder }}</span>
            </template>
          </el-table-column>
        </el-table>

 
    <div slot="footer">
       <el-button @click="productOrderVisible = false">取消</el-button>
       <el-button type="primary" @click="editProductOrderFun">确定</el-button>
    </div>
</el-dialog>

1、弹框打开方法:

this.productOrderData = res.body //列表赋值

      //缓存
      let arr = []
      this.productOrderData.forEach(one=>{
        this.selectionBF.forEach(two=>{
          if(one.id === two.id){
            one.productOrder = two.productOrder
            arr.push(one)
          }
        })
      })
      this.uploadTable = false
      this.$nextTick(()=>{
        this.uploadTable = true
        setTimeout(()=>{
          arr.forEach(two=>{
            this.$refs.productOrderTable.toggleRowSelection(two)
          
          this.productOrderVisible = true //打开弹框
        },100)
      })

 2、弹框点击确定方法:

 // 确认修改商品顺序
    async editProductOrderFun() {
      this.selectionBF = this.productOrderData.filter(v=>v.productOrder)
      this.$message.success('修改商品顺序成功!')
      this.productOrderVisible = false
    }

3、复选框选中事件

​
// 商品顺序
    selectionChange() {
      const selArr = this.$refs.productOrderTable.selection
      this.productOrderData.forEach(item => {
        item.productOrder = null
      })
      let newArr = []
      selArr.forEach((item,i)=>{
        newArr.push({
          id:item.id,
          index:i+1
        })
      })

      this.productOrderData.forEach(one=>{
        newArr.forEach(two=>{
          if(one.id === two.id){
            one.productOrder = two.index
          }
        })
      })
    },

​ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值