el-table实现后端支持的排序

本文介绍了如何在HTML中使用ElementUI的el-table组件实现视频排行榜的动态排序,包括前端自定义排序和与后端的交互,通过onSortChange事件处理prop和order参数,实现播放量、点赞量和评论量的定制排序功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Leaderboard:{
        type:2,
        page:1,
        limit:10,
        sort:'play_count',//传给后端的数据
        sort_order:'desc',//升序or降序  默认降序
        start_date:'',
        end_date:''
      },
prop:'play_count',//表格里面要写的
type:'desc',//表格里面要写的

注意的点:
1.el-table标签上要写 @sort-change=“onSortChange” :default-sort = “{‘prop’: prop, ‘order’: type}” ,点击升序或者降序调用的函数sort-change,prop是哪个属性执行排序,order:执行的是升序还是降序,
2.在需要排序的el-table-column上添加属性sortable=‘custom’ ,custom表示后端排序,如果只写sortable,就表示仅在前端排序,详情可在官网查看,比较简单

<el-table  height="600" max-height="600" :data="videoListData" style="width: 100%;"  :header-cell-style="{color:'#fff',fontSize:'14px',fontWeight:'500'}"  @sort-change="onSortChange"  :default-sort = "{'prop': prop, 'order': type}" >
    <el-table-column prop="play_count" label="播放量"  sortable='custom'  align="center" width="86"></el-table-column>
    <el-table-column prop="digg_count" label="点赞量"   sortable='custom' align="center" width="86"></el-table-column>
    <el-table-column prop="comment_count" label="评论量" sortable='custom'   align="center" width="86"></el-table-column>
</el-table>
 onSortChange({ prop, order }){
      console.log(prop,order);//play_count,ascending
      this.Leaderboard.sort=prop;
      this.Leaderboard.sort_order=order=='ascending'?'asc':'desc'
      this.getVideoData()//获取表格数据的函数,里面是请求表格数据的接口
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值