使用自定义比较器对 table 列进行排序

Sort table column with custom comparator

我有一个 table 列显示客户的全名(名字 + 姓氏)。但是,我想先按名字然后按姓氏对这一列进行排序。因此我在我的控制器中添加了一些比较器功能:

_customNameComparator: function(value1, value2) {
    // Separate all words of the full name
    var aWordsName1 = value1.split(" ");
    var aWordsName2 = value2.split(" ");

    // Get the last and first names of the two names
    var sFirstName1 = value1.substring(0, value1.lastIndexOf(" "));
    var sLastName1 = aWordsName1[aWordsName1.length - 1];
    var sFirstName2 = value2.substring(0, value1.lastIndexOf(" "));
    var sLastName2 = aWordsName2[aWordsName2.length - 1];

    // 0 values are equal
    // -1 value1 smaller than value2
    // 1 value1 larger than value2

    if (sLastName1 === sLastName2) {
        if (sFirstName1 === sFirstName2) {
            return 0;
        } else if (sFirstName1 > sFirstName2) {
            return 1;
        } else {
            return -1;
        }
    } else if (sLastName1 > sLastName2) {
        return 1;
    } else {
        return -1;
    }
}

单击 Header 列时,我尝试调用

var aSorter = [];
aSorter.push(new sap.ui.model.Sorter("FullName", bDescending, false, this._customNameComparator)); 
var oBinding = this.byId("tableTargetGroupDetails").getBinding("items");
oBinding.sort(aSorter);

比较器不是这样工作的。排序与往常一样(按全名)。我做错了什么?

顺便说一句:我知道这可能会导致一些错误的排序(例如,姓氏包含两个或更多单词),但由于它是 "only" 排序,目前对我来说没问题.

除非您的绑定 operationModeClient,否则您的比较器可能无法工作。您可以使用 { parameters: { operationMode: 'Client' } }.

设置绑定模式