The same sorting in different tables

The same sorting in different tables

transloydtransloyd Posts: 10Questions: 3Answers: 0

I have 2 different tables. In these tables there is only one common column - the ID. If I sort on the first table by the column which is not in the second table, then how do I make the rows in the second table be sorted in the same way as in the first one?

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    the question needs more explanation. You are asking how to sort on something that is not there.

    Because of that, I suspect that you are actually going to have to turn off sorting on the second table and resort the second data array, not the table, based on the order of ids on the first table and redraw the table.

  • transloydtransloyd Posts: 10Questions: 3Answers: 0

    Let's say there is a table1 (sorted by COL2):

    ID COL1 COL2 COL3
    4 data data1 data
    5 data data2 data
    3 data data3 data
    1 data data4 data
    2 data data5 data

    Now I need to sort the table 2, so that I get the same order in the ID column

    ID COL4 COL5 COL6
    4 test test test
    5 test test test
    3 test test test
    1 test test test
    2 test test test

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    I think this might actually be very difficult to do. The order for the table rows is assumed to come from the content inside those rows - not externally. So you'd need to create an ordering plug-in that checked against your other table to see what the sort order should be. That's really not going to be easy I'm afraid.

    Allan

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    Just had an idea as I hit return: How about having the data from table 1 also in table 2. Then you can sort table 2 by the data from table 1, just have it in hidden columns.

    Allan

  • transloydtransloyd Posts: 10Questions: 3Answers: 0

    It turned out to write a sort that sorts an array according to a given array.

    function orderAccordingToArray(input, output) {
        input.sort((a, b) => {
            if( output.indexOf(a) > output.indexOf(b) )
                return 1
            else
                return -1
        })
    
        return input
    }
    

    Example of work:
    input: [1, 2, 3, 4, 5]
    expected output: [5, 2, 1, 3, 4]
    result: [5, 2, 1, 3, 4]

    But I can't adapt this function to a custom ordering plugin. Can you help me or give me some advices?

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    You'd need to get the current data from the first DataTable, in correct order. That can be done with table.column( columnIndex, { order: 'applied' } ).data(). Where columnIndex is the sorting column index - which you can get with order().

    Allan

  • transloydtransloyd Posts: 10Questions: 3Answers: 0

    I already have an ID array in the correct order. But how to get array of ID's in second table from order plugin

    $.fn.dataTable.ext.type.order['sort-according-to-array-pre'] = function(data) {
       input.sort((a, b) => {
            if( output.indexOf(a) > output.indexOf(b) )
                return 1
            else
                return -1
        })
     
        return input
    };
    
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    You'd need to either query the other table from inside your plug-in, or have a global variable that gets written to when the first table sorts.

    Allan

  • transloydtransloyd Posts: 10Questions: 3Answers: 0

    Hey, Allan! I still cant do it. I already have an array with ID's, which determines the sort order. I still cant implement my algorithm in the table sort function.
    As far as I understand, the standard sort function takes the current and the next value and sorts them, and so on each line. But how to sort by the given order, knowing only the current and next values.

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    Yup, as I said, this is going to be relatively difficult to do since DataTables doesn't present an API specific to order by pre-arranged data. Rather it uses a direct comparison between two data points, just like Array.prototype.sort(). Your sorting code above to sort based on current data is key to the solution.

    If you want to take this approach, the way I'd do it is to listen for order on the parent table and store the data that defines the ordering in a variable. Then call draw() on the child table, and its sorting function would refer to your variable to look up the order.

    However, I think my suggestion above of using hidden columns with duplicate data will be a lot easier to implement. Then you can leave the sorting to the DataTable, just worry about trigger a resort on the child table and on what column.

    Allan

  • transloydtransloyd Posts: 10Questions: 3Answers: 0

    Yes, using hidden columns and duplicate data would be much easier. However, in the main table I have 70+ columns, and in the second one should be 15. I think that in this case this method will reduce the performance of the table.

    I'll try the method with listening for order.

This discussion has been closed.