动态修改或生成Java代码

问题描述 投票:0回答:1

我正在对某些表数据进行多重排序。我想在我的AngularJS网站中动态修改指定排序优先级的代码行。 (我只是在修改数组的结构,因为AngularJS监视绑定数据的更改并将其动态反映在HTML显示中。)

对于多分类,我正在使用thenBy.js。从本质上讲,您创建了一个以firstBy(compareFunctionFunction)开头的变量,然后可以根据需要添加任意数量的thenBy(comparativeFunction)

我正在根据需要动态添加和删除排序比较器。这是我从简单的两级多重排序开始的地方:

// I want to modify and append to this line
var s = firstBy( (a, b) => a["Status"].localeCompare(b["Status"]) )
  .thenBy( (a, b) => parseFloat(a["#"]) - parseFloat(b["#"]) );

[它最初将根据“状态”属性中的数据对行进行排序,然后对作为ID号的“#”进行子排序。

我想知道的是如何在上面的行中动态添加更多代码。例如,假设我想添加一个第三级排序,例如在“ Uniquie ID”上。对行进行硬编码,我可以这样:

var s = firstBy( (a, b) => a["Status"].localeCompare(b["Status"]) )
  .thenBy( (a, b) => parseFloat(a["#"]) - parseFloat(b["#"]) )
  .thenBy( (a, b) => parseFloat(a["Unique ID"]) - parseFloat(b["Unique ID"]) );

我只是不确定如何以编程方式将更多的'thenBy()'方法追加到该行。我应该生成一个字符串并通过eval()

运行它吗?有没有更好(更安全)的方法?

这里是程序:

self.dataSource = [
    {"#": "1", "Unique ID": "100130", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 1 - Box 1 - Position 1", "Status": "Available"},
    {"#": "2", "Unique ID": "100131", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 1 - Box 1 - Position 2", "Status": "Available"},
    {"#": "3", "Unique ID": "100132", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 1 - Box 1 - Position 3", "Status": "Available"},
    {"#": "4", "Unique ID": "100133", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 1 - Box 1 - Position 4", "Status": "Available"},
    {"#": "5", "Unique ID": "100134", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 1 - Box 1 - Position 5", "Status": "Checked Out"},
    {"#": "6", "Unique ID": "100135", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 1 - Box 1 - Position 6", "Status": "Checked Out"},
    {"#": "7", "Unique ID": "100136", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 1 - Box 1 - Position 7", "Status": "Checked Out"},
    {"#": "8", "Unique ID": "100137", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 1 - Box 1 - Position 8", "Status": "Checked Out"},
    {"#": "9", "Unique ID": "100138", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 1 - Box 1 - Position 1", "Status": "Available"},
    {"#": "10", "Unique ID": "100139", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 2 - Box 1 - Position 1", "Status": "Available"},
    {"#": "11", "Unique ID": "100140", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 2 - Box 1 - Position 2", "Status": "Available"},
    {"#": "12", "Unique ID": "100141", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 2 - Box 1 - Position 3", "Status": "Lost"},
    {"#": "13", "Unique ID": "100142", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 2 - Box 1 - Position 4", "Status": "Lost"},
    {"#": "14", "Unique ID": "100143", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 3 - Box 1 - Position 1", "Status": "Available"},
    {"#": "15", "Unique ID": "100144", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 3 - Box 1 - Position 2", "Status": "Available"},
    {"#": "16", "Unique ID": "100145", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 4 - Box 1 - Position 1", "Status": "Checked Out"},
    {"#": "17", "Unique ID": "100146", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 4 - Box 1 - Position 2", "Status": "Available"},
    {"#": "18", "Unique ID": "100147", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 4 - Box 1 - Position 3", "Status": "Available"},
    {"#": "19", "Unique ID": "100148", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 4 - Box 1 - Position 4", "Status": "Checked Out"},
    {"#": "20", "Unique ID": "100149", "Name": "Book", "Section": "Paraguay", "Position": "Shelf 1 - Rack 5 - Box 1 - Position 1", "Status": "Available"}       
];

var self = this;

self.multiSortDict = {};

self.addSort = function(sortName, sortComparatorFunction)
{
    self.multiSortDict[sortName] = sortComparatorFunction;
}

// Example code to add a bunch of sorting methods
self.addSort("Status",(a, b) => a["Status"].localeCompare(b["Status"]));
self.addSort("#", (a, b) => parseFloat(a["#"]) - parseFloat(b["#"]));
self.addSort("Name",(a, b) => a["Name"].localeCompare(b["Name"]));
self.addSort("Unique ID", (a, b) => parseFloat(a["Unique ID"]) - parseFloat(b["Unique ID"]));
self.addSort("Position",(a, b) => a["Position"].localeCompare(b["Position"]));


self.rebuildMultiSort = function (multiSortDict) {
    var sortList = Object.values(multiSortDict);

    var sortMethod = firstBy( sortList[0] ); // Grab the first entry to set the 'firstBy()' method

    for (var i = 1; i < sortList.length; i++) { // Starting at second position
        // I want to dynamically modify this line, being able to append as many 'thenBy()' statements as necessary
        // remove the semicolon from the previous sortMethod assignment
        // append the new 'thenBy()' to assignment
        sortMethod +=
            .thenBy(value);
    }
    return sortMethod;
}

使用上面的示例,理想情况下,我会得到这样的代码行:

var s = firstBy( (a, b) => a["Status"].localeCompare(b["Status"]) )
  .thenBy( (a, b) => parseFloat(a["#"]) - parseFloat(b["#"]) )
  .thenBy( "Name",(a, b) => a["Name"].localeCompare(b["Name"]) )
  .thenBy( (a, b) => parseFloat(a["Unique ID"]) - parseFloat(b["Unique ID"]) )
  .thenBy( "Position",(a, b) => a["Position"].localeCompare(b["Position"]) )

有什么建议吗?

我正在对某些表数据进行多重排序。我想在我的AngularJS网站中动态修改指定排序优先级的代码行。 (我只是在修改...

javascript sorting dynamic code-generation
1个回答
0
投票

[几乎在所有情况下,有比采用evil更好的处事方式。

© www.soinside.com 2019 - 2024. All rights reserved.