我有4个类别必须按照我在下面的数组中定义的顺序显示。我试图按这些类别排序,然后按字母顺序对每个类别中的消息进行排序。
我在下面的代码中介绍了for循环,但是我不确定如何将我的已排序消息添加回messagesL对象中,该对象是传递给其余代码的对象。任何提示都将不胜感激。
这是我的代码:
PKG.render = function(work,PKG) {
var data = work.details;
var messagesL = new util.LocalDataSource(data);
messagesL.responseSchema = {
resultsList: "messages",
fields: [
"messageId",
"category",
"message"
]
};
/*there could be more categories, ignore them*/
var categories = ["Mentoring", "Availability", "Affordability", "Scalability", "Info"];
var messagesColumnDefs = [
{ key: "Id", label: "ID"},
{ key: "category", label: "Category"},
{ key: "message", label: "Text"}
];
var messagesConfig = { MSG_EMPTY: 'There are no messages to display.',
MSG_ERROR: 'Error while loading table'
};
var sortedMessagesByCategory;
for (var i = 0; i < categories.length; i++)
{
var messagesByCategory;
for(var j = 0; j < data.messages.length; j++)
{
if(data.messages[j].category === categories[i])
messagesByCategory.push(data.messages[j].message);
}
if(typeof messagesByCategory !== "undefined")
messagesByCategory.sort();
/*not sure how to add sorted messages back in messagesL object*/
}
messagesTable = new widget.DataTable('wrmessagesTable', messagesColumnDefs, messagesL, messagesConfig);
workResultsWidgetCollectionPush(messagesTable);
}