Ant Design Vue a-table标签各种格式排序

1.基本数字排序

官方例子:
 

sorter: { compare: (a, b) => a.chinese - b.chinese }

2.汉字排序

{  
     title: '名称',  
     dataIndex: 'name',  
     key: 'name',  
     sorter: (a, b) => a.name.localeCompare(b.name), // 启用排序,这里是按名称的字母顺序排序  
},  

3.时间排序

{
  title: '发生时间',
  dataIndex: 'time',
  key: 'time',
  ellipsis: true,
  align: 'center',
  sorter: {
    compare: (a, b) => Date.parse(a.time) - Date.parse(b.time),
  },
},

4.数字和字母组合排序

 {
    title: siteId,
    dataIndex: 'abcId',
    key: 'abcId',
    sorter: (a, b) => {
      // 自定义排序函数
      const aVal = a.abcId.toLowerCase();
      const bVal = b.abcId.toLowerCase();
      let aNum = '', bNum = '';

      // 提取数字部分
      const aNumMatch = aVal.match(/\d+/);
      if (aNumMatch) {
        aNum = aNumMatch[0];
      }
      const bNumMatch = bVal.match(/\d+/);
      if (bNumMatch) {
        bNum = bNumMatch[0];
      }

      // 先比较字母部分
      if (aVal.replace(/\d+/, '') !== bVal.replace(/\d+/, '')) {
        return aVal.localeCompare(bVal);
      }

      // 如果字母部分相同,再比较数字部分
      if (aNum !== bNum) {
        return parseInt(aNum, 10) - parseInt(bNum, 10);
      }

      // 如果都相同,则认为它们相等
      return 0;
    },
    //sortDirections: ['descend'],
},

### 关于 Ant Design Vue 中 A-Table 组件滚动条功能的实现与配置 #### 1. 表格滚动加载功能 为了实现在表格滚动到底部时动态加载更多数据的功能,可以利用 `ref` 获取表格实例,并通过监听 `.ant-table-body` 容器的滚动事件来触发加载逻辑。以下是具体实现方法: ```vue <template> <a-table ref="myTable" size="middle" :columns="columns" :data-source="dataSource" :pagination="false" :scroll="{ y: 'calc(100vh - 160px)' }" :loading="loading"> </a-table> </template> <script> export default { data() { return { columns: [], // 列定义 dataSource: [], // 数据源 loading: false, // 加载状态 currentPage: 1, // 当前页码 pageSize: 100, // 每次加载的数据量 }; }, mounted() { this.$nextTick(() => { const tableComponent = this.$refs.myTable; if (tableComponent) { const tableContainer = tableComponent.$el.querySelector('.ant-table-body'); tableContainer.addEventListener('scroll', this.handleScroll); } }); }, methods: { handleScroll(event) { const target = event.target; if (target.scrollHeight - target.scrollTop === target.clientHeight) { // 滚动到底部时加载下一页数据 this.loadMoreData(); } }, loadMoreData() { this.loading = true; setTimeout(() => { // 模拟异步请求新数据 const newData = Array.from({ length: this.pageSize }, (_, i) => ({ key: `${this.currentPage * this.pageSize + i}`, name: `Item ${this.currentPage * this.pageSize + i}`, })); this.dataSource = [...this.dataSource, ...newData]; this.currentPage += 1; this.loading = false; }, 1000); }, }, }; </script> ``` 此代码实现了当用户滚动到表格底部时自动加载下一批数据的功能[^1]。 --- #### 2. 滚动到指定行位置 如果需要在特定情况下让表格滚动到某一行的位置,可以通过调整 `.ant-table-body` 的 `scrollTop` 属性来完成。以下是一个完整的示例: ```vue <template> <a-table :columns="columns" :data-source="data" :scroll="{ y: 300 }" @scroll="handleScroll" ref="tableRef"> </a-table> </template> <script> export default { data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name' }, { title: 'Age', dataIndex: 'age', key: 'age' }, ], data: Array.from({ length: 50 }, (_, i) => ({ key: i.toString(), name: `User${i}`, age: Math.floor(Math.random() * 50) })), rowKey: 'key', }; }, methods: { scrollToRow(rowKey) { const row = this.data.find((row) => row[this.rowKey] === rowKey); if (!row) return; const tableBody = this.$refs.tableRef?.$el?.querySelector('.ant-table-body'); if (!tableBody) return; const rowElement = tableBody.querySelector(`[data-row-key="${row[this.rowKey]}"]`); if (rowElement) { const tableBodyScrollTop = rowElement.offsetTop - tableBody.offsetTop; tableBody.scrollTop = tableBodyScrollTop; } }, handleScroll() {}, }, mounted() { this.scrollToRow('25'); // 自动滚动到第26行(索引为25) }, }; </script> ``` 该代码展示了如何通过计算目标行相对于表格容器的距离,将其滚动至视图中心[^2]。 --- #### 3. 解决固定列样式错乱问题 当启用 `fixed` 列时,可能会遇到因滚动条更新而导致的样式错乱问题。解决办法是对每一行的高度进行同步调整: ```javascript this.$nextTick(() => { const tableId = "fixedTable"; const scrollDiv = document.querySelector(`#${tableId} .ant-table-scroll > .ant-table-body`); const leftFixedDiv = document.querySelector(`#${tableId} .ant-table-fixed-left .ant-table-body-inner`); setTimeout(() => { this.dataList.forEach((item) => { const rowKey = item.id; const cssSelector = `table.ant-table-fixed tr[data-row-key='${rowKey}']`; const leftFixedTr = leftFixedDiv.querySelector(cssSelector); const scrollTr = scrollDiv.querySelector(cssSelector); if (leftFixedTr && scrollTr) { const theoryTrHeight = getComputedStyle(scrollTr).height; leftFixedTr.style.height = theoryTrHeight; } }); }, 100); }); ``` 这段代码通过对每一行的实际高度重新赋值,解决了固定列和主体部分高度不一致的问题[^3]。 --- ### 总结 以上分别介绍了三种常见场景下的解决方案: 1. 动态分页加载数据; 2. 滚动到指定行位置; 3. 处理固定列样式错乱问题。 这些技术可以帮助开发者更高效地使用 Ant Design Vue 的 A-Table 组件,提升用户体验。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值