order of event firing - need sort of column when adjusting row data

order of event firing - need sort of column when adjusting row data

loukinglouking Posts: 259Questions: 52Answers: 0

Please refer to https://codepen.io/louking/pen/eevMav . Hopefully there's not too much extraneous code there -- this is a cut/paste from my application.

I am currently doing a calculation during the preDraw event, and updating the cells in loc column from rowCallback function, but this appears too late to be handled by the table sort (default value [[0,'asc']]).

This calculation does scan of the table about to be drawn in order to determine the value for the 'loc' column. Based on some research (https://datatables.net/forums/discussion/32993/best-way-to-update-cell-in-row-based-on-data-in-another-cell) I decided to use rowCallback because when I tried to use render, it seemed like it was being called too early in the processing (i.e, before preDraw).

In this example I'm calculating loc column based on lat,lng columns, and want the table to be sorted by loc.

I couldn't find any documentation on the ordering of processing along with the events which are fired during that processing. Maybe I missed that? In any case, what is the best way to get this column sorted?

[also couldn't get the preDraw event to fire when using data option, so this is going to my live site for the data]

This question has an accepted answers - jump to answer

Answers

  • loukinglouking Posts: 259Questions: 52Answers: 0

    I got a bit closer with the changes made in https://codepen.io/louking/pen/MOpXbP .

    Rather than using rowCallback I'm using the cell method to set the data, in the preDraw function.

    While the sort arrows now work in the loc column, the table still does not initially render as sorted, so the sort before drawing must happen before preDraw.

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    The preDraw happens right before the draw, but after any sort or filter operations, so I'm afraid its already going to be too late for any sorting changes to be taken into account.

    Do you have a hook into whatever is causing the redraw, can you recalculate the values at that point? There isn't a preOrder event at the moment I'm afraid (although I can see that it might be useful in such a case).

    Allan

  • loukinglouking Posts: 259Questions: 52Answers: 0
    edited November 2017

    Hmm. I don't think so. I'm using yadcf to do the filtering. I need to do my processing between the filtering and the sort because the numbers I choose depend on what rows are visible in the table.

    Is there a way to sort the table after the draw, or will I get into an infinite loop? Maybe I only have to sort after the first draw, though, so I won't spin.

    Maybe if I invoke draw at the end of the draw event, just the first time?

    ETA: actually I'm not sure if just doing this after the first draw will work in my application. I have to think about that. Best if I can find a way to sort after the draw without infinite loop.

  • loukinglouking Posts: 259Questions: 52Answers: 0

    Maybe I can work around this by always drawing twice, keeping state in a boolean and checking said state in preDraw and draw to avoid infinite loop. I'll have to experiment.

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Answer ✓

    I think that would be another workaround. Its a little messy, but if there is no other hook, that is probably the best option.

    Allan

  • loukinglouking Posts: 259Questions: 52Answers: 0

    That workaround seems to take care of the sorting when first loaded. Yes it's a bit messy.

    See https://codepen.io/louking/pen/POjjBa for the example.

    I'll keep my eye out for a preOrder event, but I expect I won't be coming back to change this.

    Thanks for your time, and once again for the great plugin.

This discussion has been closed.