简体   繁体   English

Javascript:排序对象

[英]Javascript: sorting objects

I'm looking for some ideas of how to accomplish this as I am hitting a wall on it. 当我碰壁时,我正在寻找一些有关如何完成此操作的想法。

I have a table that displays data pulled from a MySQL db. 我有一个表,显示从MySQL数据库提取的数据。 The table goes in a sequence of a row of 13 cells with displayed data followed by a hidden row of one cell. 该表按顺序排列,每行包含13个单元格,其中显示数据,后跟一个单元格的隐藏行。 The hidden row is toggled by clicking a link in cell index 1 of the previous row. 通过单击上一行单元格索引1中的链接可以切换隐藏行。 Like so: 像这样:

row 1 : click this cell to show row 2 : another cell : another cell : ad nauseum till we get to 13 : row 2 which is hidden row 3 : click this cell to show row 2 : another cell : another cell : ad nauseum till we get to 13 : row 4 which is hidden ... 第1行:单击此单元格以显示第2行:另一个单元格:另一个单元格:出现恶心,直到第13行:隐藏的第2行。第3行:单击此单元格以显示第2行:另一个单元格:另一个单元格:无味直到我们到达13:隐藏的第4行...

so using jquery I pulled all the rows, then set a test to determine if it was a displayed row or hidden row, if it was displayed then I put that row and the following one into an object and then placed that object into another object, like so: 因此,使用jquery提取了所有行,然后进行测试以确定它是显示的行还是隐藏的行,如果显示了该行,则将该行和下一行放入一个对象,然后将该对象放入另一个对象,像这样:

//this function is for sorting the data in the table
$(".sort").click(function() { 

        //get what column we are sorting by
        var sortBy = $(this).attr('sortBy');

        //set the colnum to sort by 
        if (sortBy == "itemname") { 
            var sortCol = 1;
        } else if (sortBy == "priority") { 
            var sortCol = 2;
        } else if (sortBy == "livedate") { 
            var sortCol = 10;
        } else if (sortBy == "status") { 
            var sortCol = 11;
        } else if (sortBy == "designer") { 
            var sortCol = 12;
        } 

        //get the table data
        var tableData = getTableData("NO", "null", "YES", sortBy); 

        //get all the rows
        var tableRowArray = $("#productTableBody tr");

        //declare new table object
        var tableObj = new Object;
        var rowPackage = new Object;

        //loop through tableRowArray and put rows into packages of two, the main row and the hidden row
        for(var t=0; t<tableRowArray.length; t++) { 
            if($(tableRowArray[t]).children(":first").attr('class') == "colorCode") { 

                rowPackage[t] = $(tableRowArray[t]).children();
                rowPackage[t+1] = $(tableRowArray[t+1]).children();

                //dump rows into tableObj
                tableObj[t] = rowPackage;
            }

            //clean out rowPackage
            rowPackage = {};
        }


        var x=-2;
        for(thisRow in tableObj) {
            x = x+2;
            var sortItem = $(tableObj[thisRow][x][sortCol]).html();

                            //ack! -->getting stumped here

        }       
    });

I've also collected which column the user wants to sort by. 我还收集了用户要排序的列。 I can then find the cell the user wants to sort by. 然后,我可以找到用户想要排序的单元格。 I know I need to pull that info, put into an array and sort but I guess I am getting stumped on how to apply the sorted array back to my tableObj so I can rewrite the table body HTML...the reason I am getting stumped is that some of the data to be sorted will be identical, for example if sorting by designer the data could be this {"a", "b", "c", "c", "a", "a", ""a"}, which when sorted would be a, a, a, a, b, c, c, but since some are identical I can't go back and loop through the object and find the entry that matches the first item in my sorted array, 4 items will match it. So how do I determine which entry in the object matches up with the first a in the sorted list? 我知道我需要提取该信息,放入数组并进行排序,但是我想我对如何将排序后的数组应用到tableObj感到很困惑,因此我可以重写表主体HTML ...我陷入困境的原因是指某些要排序的数据将是相同的,例如,如果按设计者进行排序,则数据可能是{{a“,” b“,” c“,” c“,” a“,” a“,” “ a”},排序时将是a,a,a,a,b,c,c,但是由于有些相同,所以我无法返回并遍历对象并找到与第一个项目匹配的条目我的排序数组将匹配4个项目,那么如何确定对象中的哪个条目与排序列表中的第一个匹配?

Thanks in advance. 提前致谢。

That's a tough one, but I suppose there is hardly anything impossible in this life. 这是一个艰难的过程,但我想这辈子几乎没有什么不可能的。

I would go like this (using Underscore library ) 我会这样(使用Underscore库

var packs = [];
// assuming you always have even number of tr's
$("#productTableBody tr:odd").each(function(i, tr){
    packs.push( {main: tr, sub: tr.next()} ); 
    // tr.next() will be :even, so it's not yet selected

    // now detach rows from the table
    // note the order - next() wont work otherwise
    tr.next().remove();
    tr.remove();
});

var sortedPacks = _(packs).sortBy(function(pack){
    return $(pack.main).find('td:nth('+sortCol')').text();
});

// now in sortedPacks you have touples of rows sorted by "main" rows sortCol column
// and you would probably want to restore the table now
$.each(packs, function(i, pack){
    $("#productTableBody").append(pack.main).append(pack.sub);
});

The code may not reflect your situation perfectly, but I suppose you should be able to get the main idea. 该代码可能无法完美地反映您的情况,但是我想您应该能够理解主要思想。

Not really to easy to get what you are asking for here, but this will at least enable you to get the data sorted. 在这里很难得到您想要的东西,但这至少将使您能够对数据进行排序。

Start by collecting your data into an array, eg data , each row can be represented by either an array or an object. 首先将数据收集到数组中,例如data ,每一行都可以由数组或对象表示。

Now simply call 现在只需致电

data.sort(function(a, b){
     // select the two values from a and b depending on the column to sort by
     a = a[colNr];
     b = b[colNr];
     return a == b ? 0 : a < b ? -1 : 1;
 });

Now you can easily rebuild your table based on the sorted array. 现在,您可以轻松地基于已排序的数组重建表。

If you during data-collection also added a reference to the row to the array/object, then you can now remove all rows from the table, loop over the data array, and add each node back to the table. 如果您在数据收集期间还向阵列/对象添加了对该行的引用,那么您现在可以从表中删除所有行,遍历data数组,并将每个节点添加回表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM