处理VUE框架中,ElementUI控件u-table空值排序问题

按照以下步骤进行操作:

  1. 分离数据 :首先,将原始数据按照username为空和非空进行分类。
  2. 对非空表进行排序 :对非空表按照username进行升序排序。
  3. 合并表格:将空表和排序后的非空表合并,保证空表挂接在排序后的非空表后面。
  4. 赋值给tableData :将合并后的结果赋值给tableData
  5. 实现代码如下:

<template>

<el-table :data="tableData">

<el-table-column label="Username" prop="username"></el-table-column>

<el-table-column label="Other Data" prop="otherData"></el-table-column>

<!-- 其他列 -->

</el-table>

</template>

<script>

export default {

data() {

return {

// 原始数据(假设是从接口获取或定义的)

originalData: [

{ username: 'Alice', otherData: 'Data 1' },

{ username: '', otherData: 'Data 2' },

{ username: 'Bob', otherData: 'Data 3' },

{ username: '', otherData: 'Data 4' },

{ username: 'Charlie', otherData: 'Data 5' },

// ...更多数据

],

tableData: [] // 最终显示的表格数据

};

},

methods: {

processData() {

// 1. 分离空表和非空表

const emptyUsernameData = this.originalData.filter(item => item.username === '');

const nonEmptyUsernameData = this.originalData.filter(item => item.username !== '');

// 2. 对非空表按照 username 进行升序排序

const sortedNonEmptyData = nonEmptyUsernameData.sort((a, b) => {

return a.username.localeCompare(b.username);

});

// 3. 合并非空表和空表

this.tableData = [...sortedNonEmptyData, ...emptyUsernameData];

}

},

created() {

// 在组件加载时处理数据

this.processData();

}

};

</script>

相关推荐
无我Code6 分钟前
前端-2025年末个人总结
前端·年终总结
VX:Fegn089518 分钟前
计算机毕业设计|基于springboot + vue敬老院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
文刀竹肃23 分钟前
DVWA -SQL Injection-通关教程-完结
前端·数据库·sql·安全·网络安全·oracle
LYFlied28 分钟前
【每日算法】LeetCode 84. 柱状图中最大的矩形
前端·算法·leetcode·面试·职场和发展
Bigger30 分钟前
Tauri(21)——窗口缩放后的”失焦惊魂”,游戏控制权丢失了
前端·macos·app
Bigger1 小时前
Tauri (20)——为什么 NSPanel 窗口不能用官方 API 全屏?
前端·macos·app
bug总结1 小时前
前端开发中为什么要使用 URL().origin 提取接口根地址
开发语言·前端·javascript·vue.js·html
老华带你飞2 小时前
建筑材料管理|基于springboot 建筑材料管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·spring
一招定胜负2 小时前
网络爬虫(第三部)
前端·javascript·爬虫
Shaneyxs2 小时前
从 0 到 1 实现CloudBase云开发 + 低代码全栈开发活动管理小程序(13)
前端