0

I'm using jQuery to parse a JSON file of employees, which contains their name, department, subdepartment, and some other details.

e.g.:

[
{
  "LAST":"Betsy",
  "FIRST":"Jackson",
  "DEPT":"Customer Service",
  "SUBDEPT":"Tech Support",
  "JOINED":"02/94",
  "TITLE":"Technical Customer Service Representative",
  "RESIDENCE":"Someplace",
  "HOBBIES":"Reading, Walking, Sleeping"
},
{
  "LAST":"Sally",
  "FIRST":"Smith",
  "DEPT":"Customer Service",
  "SUBDEPT":"Installation Customer Service Representative",
  "JOINED":"01/04",
  "TITLE":"Technical Customer Service Representative",
  "RESIDENCE":"Someplace",
  "HOBBIES":"Reading, Walking, Sleeping"
},
]

I'm trying to build an application where users can click on the name of an employee and see a refresh of results where every employee in that employee's department is shown, organized by the sub-departments, and scrolled-down to the given employee.

I've successfully generated a list of employee names, with data-* attributes to hold their department and sub-department. When an employee name is clicked, I've been able to parse the JSON file a second time return all the employees who are also in that department, and build a grid, and then push the entire matching employee object into a new array called "results."

note: dept = data-dept passed by jquery selector..

$.each(data, function(i, employee) { 
    if (employee.DEPT == dept) {
    var employeetext = '<span class="name">'
     + employee.FIRST+' '+ employee.LAST+'</span>',
     employee.JOINED, 
     employee.TITLE, 
     employee.RESIDENCE, 
     '...', 
     employee.HOBBIES;  

        $('#employees').append('<div class="employee_block"><img src="" width="85" height="113">' + employeetext + '.</div>');  

        results.push(employee); 
}
}) // end stepping through employees

However, I need to build the grid based on a new sorted order from the array, rather than y the alphabetical that is being used now. I need to split the results by sub-department, according to a priority that is not alphabetical, but rather a custom order which I'm hoping to define in a separate object (would this be a "relational database?") e.g:

var subdeptorder =  [
{
    "DEPT": "Marketing",
    "SUBDEPTS": ["Administration", "Purchasing", "Advertising", "PR"]
},
{
    "DEPT": "Sales",
    "SUBDEPTS": ["Administration", "Business Development"]
}
]

So I need to sort the "results" array according to the employee's department (and that department's subdepartment order) within it.

How do I write a comparison function to re-sort the "results" array according to a priority established in a separate object?

4

1 回答 1

2

像这样格式化单独的对象:

var subdeptorder = {
    "Marketing": ["Administration", "Purchasing", "Advertising", "PR"],
    "Sales": ["Administration", "Business Development"]
};

然后你可以像这样对数据进行排序:

var dept = …; // the chosen one

var results = $.grep(data, function(employee) {
        return employee.DEPT = dept;
    }),
    order = subdeptorder[dept];
results.sort(function(a, b) {
    // sort them by the indices of their SUBDEPTs in the order array
    return $.inArray(a.SUBDEPT, order) - $.inArray(b.SUBDEPT, order);
});
$.each(results, function(i, employee) { 
     $('#employees').append('<div class="employee_block"><img src="" width="85" height="113">' + [
         '<span class="name">'+employee.FIRST+' '+employee.LAST+'</span>',
         employee.JOINED,
         employee.TITLE,
         employee.RESIDENCE,
         '…',
         employee.HOBBIES
     ].join(' ') + '.</div>');
});

请参阅按自定义顺序排序以获得优化版本(不是$.inArray每次都用于索引)。

于 2013-08-28T21:46:48.830 回答