【问题标题】:Is there any way to specify the column name in the custom sort function of an Aurelia Table?有没有办法在 Aurelia Table 的自定义排序函数中指定列名?
【发布时间】:2018-10-30 19:22:32
【问题描述】:

这可能是使用 aurelia-table 的一种非常具体的方式,但我想知道是否有任何方式可以访问您在 custom.bind 函数中的 aut-sort 参数中定义的密钥。

我没有使用 data 的静态数组填充表,而是加载 JSON 数组并根据 JSON 中的内容动态创建表,因此我不能通过指向数组来使用 aut-sort列表值。

如果我只能根据我单击排序的列访问键值,那么我将能够利用自定义排序来满足我的需要。

我已经尝试定义一个全局字符串,并在 Aurelia 表标题上使用 change.delegate 将更新附加到该字符串,但 change.delegate 会触发 在 custom.bind 排序和无论如何,custom.bind 函数的范围仅限于 Aurelia 表。

<table aurelia-table="data.bind: data; display-data.bind: $resultsData; current-page.bind: currentPage; page-size.bind: pageSize; total-items.bind: totalItems; api.bind: tableApi;">
            <thead>
                <tr>
                    <th repeat.for="[k,v] of responseFields" aut-sort="custom.bind: someSort">${v.displayName}</th>
                </tr>
            </thead>
            <tbody>
                <tr repeat.for="result of $resultsData">
                    <td repeat.for="[k,v] of responseFields">
                        ${result[k]}
                    </td>
                </tr>
            </tbody>
        </table>

Map 只允许我动态设置标题。

这里是排序函数:

someSort(a, b, sortOrder) {
    let sortColumnName = this.sortColumnName;
    let code1 = this.data[this.data.indexOf(a)][sortColumnName];
    let code2 = this.data[this.data.indexOf(b)][sortColumnName];

    if (code1 === code2) {
        return 0;
    }
    if (code1 > code2) {
        return 1 * sortOrder;
    }
    return -1 * sortOrder;
}

我希望能够定义一个sortColumnName,但我目前不能,除非我对其进行硬编码,但我需要它是动态的。

【问题讨论】:

  • 如果对您有帮助,请务必点赞并接受答案

标签: aurelia


【解决方案1】:

有一种自定义顺序的方法:

import {HttpClient} from "aurelia-fetch-client";

export class Example {
    users = [];

    bind(){
        let client = new HttpClient();

        return client.fetch('data.json')
            .then(response => response.json())
            .then(users => this.users = users);
    }

    nameLength(row) {
        return row.name.length;
    }

    dateSort(a, b, sortOrder) {
        let date1 = new Date(a.registered);
        let date2 = new Date(b.registered);

        if (date1 === date2) {
            return 0;
        }

        if (date1 > date2) {
            return 1 * sortOrder;
        }

        return -1 * sortOrder;
    }
}
 .a ut-sort:before{
    font-family: FontAwesome;
    padding-right: 0.5em;
    width: 1.28571429em;
    display: inline-block;
    text-align: center;
}

.aut-sortable:before{
    content: "\f0dc";
}

.aut-asc:before{
    content: "\f160";
}

.aut-desc:before{
    content: "\f161";
}
<template>
    <table class="table table-striped" aurelia-table="data.bind: users; display-data.bind: $displayData">
        <thead>
        <tr>
            <th aut-sort="key.bind: nameLength">Name</th>
            <th aut-sort="key: age; default: desc">Age</th>
            <th>E-mail</th>
            <th aut-sort="custom.bind: dateSort">Registered</th>
            <th aut-sort="key: isActive">Active</th>
        </tr>
        </thead>
        <tbody>
        <tr repeat.for="user of $displayData">
            <td>${user.name}</td>
            <td>${user.age}</td>
            <td><a href="mailto:${user.email}">${user.email}</a></td>
            <td>${user.registered}</td>
            <td>${user.isActive}</td>
        </tr>
        </tbody>
    </table>
</template>

【讨论】:

    【解决方案2】:

    我能够通过使用click.capture 来实现此功能,该click.capture 调用具有索引的函数(click.delegate 不起作用,因为它在标头的custom.bind 之后被触发。

    <tr class="table-header">
    <th repeat.for="item of tableData.columns" click.capture="sort($index)" aut-sort="custom.bind: sortList">
       ${item}
       <div class="arrow"></div>
    </th>
    

    sort() 只是设置一个私有变量

    private sortIndex = 0;
    public sort(index) {
    this.sortIndex = index;
    }
    

    我的自定义排序看起来像这样并使用sortIndex

     public sortList = (a, b, order) => {
    
    //here I use the index of the header to get the right data from the row
    const firstCell = a.cells[this.sortIndex].text;
    const secondCell = b.cells[this.sortIndex].text;
      if (order === 1) {
        if (firstCell < secondCell) {
        return -1;
        }
        if (firstCell > secondCell) {
        return 1;
        }
      }
      if (order === -1) {
        if (firstCell < secondCell) {
          return 1;
        }
        if (firstCell > secondCell) {
          return -1;
        }
      }
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-05
      • 2020-07-16
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多