Completely lost on column formatting after fnAddData

Completely lost on column formatting after fnAddData

aussiepeteaussiepete Posts: 1Questions: 0Answers: 0
edited April 2018 in Free community support

Hi - my sincere apologies for being not so clever... I've been struggling with this for days now. Unfortunately I cannot send any URL for debugging as it's on an internal secure network.

My issue is that after much searching around the forum, I've managed to be able to load my XML data to the table, using this code:

var thisTable = $("#dashboard-products-list-2").dataTable();

 $.ajax({
 url: "xml/projectlist/ADV-GLOBAL.xml",
 dataType: "xml",
 async: false,
 success: function (response) {
 var $Users = $(response).find("ADV-GLOBAL");

 $Users.each(function (index, User) {
 var $User = $(User),
 addData = [];

//  addData.push($User.attr("ProjectID"));
 $User.children().each(function (i, child) {
 addData.push($(child).text());
 });

 thisTable.fnAddData(addData);
 });
 
  }
 
 });

Which is great - I'm bringing in a significant amount of data in the rows and columns I want... unfortunately, all of the previous formatting that was done (by someone else here, not me) on the columns (columdefs, etc) are all completely lost when the data comes in... number/currency formats on specific columns, expansion button for viewing extra data etc... is there anything I can do to order the events or something so that after the data is added (per the above), I can apply all of the initialization features?

Sorry if this is a stupid question - I'm a procurement guy with a little bit of knowledge (which us dangerous) to make our data look a little fancy, but all the pretty stuff has gone once I load the data using fnAddData. Please, please any help would be great - I'm pulling my hair out over this... happy to buy someone who can help me a beer :)

Replies

  • NanakiSBSNanakiSBS Posts: 1Questions: 0Answers: 0

    Howdy,

    If I have understood right you want to re order the columns so that new data is ordered correctly?

    Using the new DataTables API this can be done easily!

    $('YourTableName').DataTable().order([2,'asc'], [8, 'asc']).draw();

    Check the documentation for the .order() function!

    Looking at it your issue might be the same one I had which was mixing the old dataTables and the new DataTables API.

    Hopefully that's helpful!

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @aussiepete ,

    If the 'previous formatting' was done in DataTables, it would be worth staying with the implementation they had, as they must've been doing that formatting somewhere.

    If that's not possible, you can add row/cell formatting in several places, such as columns.render for when the columns are manipulated (searched/ordered/etc), or createdRow which will be called each time you add a row with row.add().

    Hope that helps,

    Colin

This discussion has been closed.