ant-design-vue中sort排序取值给后端排序

Ant Design of Vue 通过sort排序,返回给后端的排序相关字段

sortField 0更新时间(默认) 1今日被推 sortType 0或者1升序 -1降序

我这里采取的思路是:当你点击到排序时,有一个小三角的颜色会改变,那么就判断css样式是否添加了颜色。
请添加图片描述
当点击了一次,有颜色改变的:请添加图片描述原样式:
在这里插入图片描述
获取所有字段的标题【console.log(record)】,可以看出,我们这里有是13个字段,找到我们需要的字段的下标,然后获取它的样式就可以了。请添加图片描述

这个就是我们要获取的样式的值【record[5].className.length】

其中无变化时的值为: ant-table-column-has-actions ant-table-column-has-sorters ant-table-row-cell-break-word

有变化的值为: ant-table-column-has-actions ant-table-column-has-sorters ant-table-column-sort ant-table-row-cell-break-word
请添加图片描述
然后通过计算它们样式的length就可以了。

原代码:

 <a-table
            :columns="columns"
            :data-source="data"
            size="middle"
            :loading="loading"
            :pagination="false"
            :scrollToFirstRowOnChange="true"
            :customHeaderRow='click'  
        >
     <!--:customHeaderRow='click'  这个是关键,当点击表头的时候,触发click-->
          <div slot="今日被推" slot-scope="PushedTimes"></div>
          <div slot="更新时间" slot-scope="UpdateTime"></div>
          <div slot="操作" slot-scope="record"></div>
        </a-table>
data() {
        return {
             form: {
                sortField: 0, //0更新时间, 1 今日被推
                sortType: 0, //0、1升序 , -1降序
            },
            columns: [
                {
                    dataIndex: 'PushedTimes',
                    // sorter: (a, b) => a.PushedTimes - b.PushedTimes,//这个是前端排序
                    sorter:true,
                    slots: { title: '今日被推' },
                    scopedSlots: { customRender: '' },
                },
                {
                    title: '更新时间',
                    dataIndex: 'UpdateTime',
                    // sorter: (a, b) => a.UpdateTime - b.UpdateTime, //这个是前端排序
                    sorter:true,
                    scopedSlots: { customRender: '更新时间' },
                },
                {
                    title: '操作',
                    scopedSlots: { customRender: '操作' },
                },
            ],
            editingKey: '',
            a:0,//今日被推
            b:0,//更新时间
            acout:1,//今日被推计数器
            bcout:1,//更新时间计数器
        }
    },
methods: {
click(record, index) {
        return {
          on: {
            click: () => {
              console.log(record)//获取所有头标题
              this.a = record[5].className.length //今日被推
              this.b = record[11].className.length //更新时间
              
              //确认排序字段,用户点击了那个字段
              if(this.a==79){
                this.form.sortField=1
                this.bcout = 1
              }
              if(this.b==79){
                this.form.sortField=0
                this.acout = 1
              }
                
			  //计算用户点了几次,来判断降序还是升序
              //这里可以再用一个函数,因为操作方式都是一样的,我这用了比较笨的办法
              if(this.form.sortField===1){
                // console.log("今日被推")
                  if(this.acout==2){
                    // console.log("降序")
                    this.form.sortType=-1
                    this.acout++
                  }else if(this.a%79===0 && this.acout==1){
                    // console.log("升序")
                    this.form.sortType=1
                    this.acout++
                  }else if(this.acout==3){
                    // console.log("升序")
                    this.form.sortType=0
                    this.acout = 1
                  }
              }

              if(this.form.sortField===0){
                // console.log("更新时间")
                  if(this.bcout==2){
                    // console.log("降序")
                    this.form.sortType=-1
                    this.bcout++
                  }else if(this.bcout==1){
                    // console.log("升序")
                    this.form.sortType=1
                    this.bcout++
                  }else if(this.bcout==3){
                    // console.log("升序")
                    this.form.sortType=0
                    this.bcout = 1
                  }
              }
              // console.log(this.form.sortField)
              // console.log(this.form.sortType)
            }
          }
        }
      },
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值