ColReorder overrides aoColumnDefs?

ColReorder overrides aoColumnDefs?

zorbakanzorbakan Posts: 9Questions: 0Answers: 0
edited November 2013 in General
Hi everyone!

I'm kind of new using DataTables and I think is awesome!
I was tweaking a little bit the column rendering defining the mRender inside aoColumnDefs on initialization. And I have also included the ColReorder plugin. All was good, but when I move a column, and then change the page, the rendering doesn't work. Like I said, I'm new on this and all I can think of is when I move a column, the aoColumnDefs is overrided by ColReorder plugin. This is right? There is some workaround?

Thanks in advance!

Regards!

PS. Sorry about my english

Replies

  • kiwykiwy Posts: 5Questions: 0Answers: 0
    you should provide a working or not working example or at least describe what paramter you're using to initialize your table, because if you're using jSon, you need to also supply a mData for each column that match the name of your column in the json file you generate or download.
    But no doubt what you're asking is possible
  • zorbakanzorbakan Posts: 9Questions: 0Answers: 0
    I hope this helps. The initialization is longer, but I think this have to be enough to understand my point.

    [code]
    self = this;
    self.table_id = "#the_table_id";
    self.the_table = $(self.table_id).dataTable({
    "bLengthChange": true,
    "bFilter": true,
    "bSort": true,
    "bInfo": true,
    "bAutoWidth": true,
    "sDom": 'T<"clear"><"top"l>rtipR',
    "bPaginate": true,
    "sPaginationType": "bootstrap",
    "bSortCellsTop": true,
    "iDeferLoading": 0,
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "webservice/getdatatablesws.php",
    "aoColumnDefs": [
    {
    "mRender": function ( data, type, row ) {
    var html_col = '';
    var img_filename = data;
    html_col += " ";
    html_col += " ";
    html_col += " ";
    return html_col;
    },
    "aTargets": [ 1 ]
    }
    ]
    });
    [/code]
    When I go through pages, all works good, but when I reorder some column and after that change the page, I see the "filename" of the image instead of the image itself.

    Do you have a clue?

    Thanks in advance.

    Regards
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    It sounds like your server-side script isn't taking account of the reordered columns. Do you have any code in getdatatablesws.php which will take account of the fact that the columns have been reordered, or is it expecting them in a static order?

    Allan
  • zorbakanzorbakan Posts: 9Questions: 0Answers: 0
    Hi Allan!
    Exactly, my server side script isn't taking account of reorder. I still have to do that. But, in this case I have 4 columns. Id, Image, Name, SKU. If I swap Name -> SKU, beyond the issue about what data goes to what column, the mRender defined is not executed. Using firebug, I put a break on line 20 (see example above) and it execute in each draw, but when I swap some columns, and change the results page, then I see no execution on line 20.

    Sorry about my lame english. It's even worst when I get nervious about a problem, ha.

    Thanks Allan to participate on this thread.

    Regards
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    This is the script that I use for server-sie processing where the column order can change: https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/objects.php . Note how it does an array search.

    I can't say why your own script isn't taking into account the reordered columns as I don't know what it looks like :-)

    Allan
  • zorbakanzorbakan Posts: 9Questions: 0Answers: 0
    Thanks Allan! Great code! I definitely will use it!
    But maybe I'm not so clear. My problem is not the missplaced column data (actually, I have to solve that yet, but I already know how to do that), the problem here is the non-execution of the customized rendering function I defined to return an tag, instead of the image file name.

    I follow this example:
    http://datatables.net/release-datatables/examples/advanced_init/column_render.html
    and it works like a charm! At least until I reorder a column, and then the mRender is not executed anymore.

    That's why on the tittle I write "ColReorder overrides aoColumnDefs?"

    Thanks in advance!

    Regards
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I would suggest using objects rather than arrays for your data. It will make everything much easier, since you don't need to deal with changing column indexes.

    Allan
  • zorbakanzorbakan Posts: 9Questions: 0Answers: 0
    Ok. So If I use Objects, the change I have to make would be like this?

    From:

    [code]
    "aTargets": [ 1 ]
    [/code]

    To:

    [code]
    "aTargets": [ 'image_filename' ]
    [/code]

    Thanks again!
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Not quite. use mData - see http://datatables.net/blog/Extended_data_source_options_with_DataTables

    Allan
  • zorbakanzorbakan Posts: 9Questions: 0Answers: 0
    I'll read it right away! Thanks Allan! I'll post here between today or tomorrow telling how it goes and how I resolved it in case anyone else have the same situation like me
  • zorbakanzorbakan Posts: 9Questions: 0Answers: 0
    It works like a charm!!! Thanks Allan! I used "mData" instead of "mRender" and the function associated now execute just perfect.

    I don't have enough words to thank you!
This discussion has been closed.