Append table TR TD and colReorder

Append table TR TD and colReorder

lenamtllenamtl Posts: 265Questions: 65Answers: 1

Hi,

When user do a search I have a table append like this filled using Ajax, PHP and jQuery.
I'm also using State Save

var $tr = $('<tr>').append( 

       $('<td>').html(name),
       $('<td>').html(phone),
       $('<td>').html(date),

).appendTo('#my-list tbody');   

I'm looking for a way (if possible) to use colReorder
at the moment if user change the column order, the data follow the column, then if the user do a new search the column header stay at the new position but the content is rebuild as the original order.

Is there a way if the user change column order to get the value following the new order using append?

Thanks

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @lenamtl ,

    There sounds like there's a lot going on there. We're happy to take a look, but it would help, as per the forum rules, if you could link to a running test case showing the issue so we can offer some help. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    Unfortunately I cannot set a demo for this as it using MySQL and PHP OOP and this is required a lot of files.

    What I d'like to know is how can I match the append column with the new column order?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I'm not too clear on the problem, which is why I asked for the example, but this may help - column-reorder - it's triggered when the user reorders

    C

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1
    edited July 2018

    Hi,
    Here is the case

    First the user click a button that fill the table
    Here is the table that get filled this is working fine as the default value.
    Name | Phone | Email

    Now let say the user change column order to this
    Email | Phone | Name

    At this point the data move with the column header.

    Column Settings are saved in Save State

    Now the user do a new search click a button that fill the table
    but the data will not follow the column header
    Email | Phone | Name
    John | 888-555-6666 | john@test.org

    What I would like is to have
    Email | Phone | Name
    john@test.org | 888-555-6666 | John

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    If you're adding as an object, then that wouldn't be an issue, since the column definition would be there, i.e.

    table.row.add({name:'a',position:'aa',office:'aaa',age:'aaaa',start_date:'aaaaa',salary:'aaaaaa'}).draw()
    

    If you're adding as an array, yep, you'll need some logic. You could use colReorder.order() to get the current positions, or iterate through the array, creating a new one based on the results of colReorder.transpose()

    Hope that helps,

    Cheers,

    Colin

  • lenamtllenamtl Posts: 265Questions: 65Answers: 1

    I'm adding as an array and using var to transform the data

        $.each(resultAuditList, function (i, resultAuditList) {
    
         var name = resultAuditList.a_name;
        if (name !== null) name = name.charAt(0).toUpperCase() +name.slice(1);
    
    
        var $tr = $('<tr>').append(
    
               $('<td>').html(name),
               $('<td>').html(phone),
               $('<td>').html(date),
    
        ).appendTo('#my-list tbody');  
    
        });
    

    could you give me an example how can I apply the logic with colReorder.order()

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    colReorder.order() returns an array with the position of the rows currently. So you would iterate through that, something like this psuedo code:

    for (i =0; i < OldRowArray.length; i++)
      newRowArray[colReorder.order()[i]] = oldRowArray[i]
    

    Then just add the newArray ...

    Hope that helps,

    C

This discussion has been closed.