Data render doesn't update with ajax.reload()

Data render doesn't update with ajax.reload()

MSLtdMSLtd Posts: 56Questions: 5Answers: 4

I'm currently rendering a float to two decimal places using this rendered:

render: $.fn.dataTable.render.number( ',', '.', 2, getCurrencyByCID() )

getCurrencyByCID:

function getCurrencyByCID(){
        var returnVal;
        var ajaxutil = new AjaxUtil();
        var sURL = 'php/y_post_po.php';
        if(!isNew){
            aData = {func: 'GET_CURRENCY_BY_CID', object_id: m_nPO_ID};
            ajaxutil.postAjax(aData, sURL, function(bSuccess, result){if(bSuccess){returnVal = convertToSymbol(result[0].currency_symbol);}});
            return returnVal;
            
            function convertToSymbol(code){
                switch(code){
                    case "£":
                        return "£";
                    case "$":
                        return "$";
                    case "€":
                        return "€";
                }
            }
        }else{
            return "£";
        }
    }

Essentially this will return £ or $ or €

This works perfectly well when the page first loads, but whenever I redraw the table from the source (which just so happens to be whenever I change the currency) the format of the data is not changed.
I don't want to reload the page every time - It's what the system currently doesn't and I've been instructed to minimise the number of times the program has to reload a new page header in the tab.

Thanks in advance - D

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin

    Is ajaxutil.postAjax a synchronous or asynchronous process? I would expect it to be async, and if it is, that's the issue. The returnVal will be undefined.

    Allan

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

    Hi @MSLtd ,

    The rows would be cached, as that speeds up ordering/searching/etc, so that's why. I believe there are two ways you could go,

    1. call rows().invalidate() to flush the cache - but this would probably cause the data to be reloaded from Ajax
    2. change it from a render function to a draw (rowCallback) - that way it would change on each draw if necessary,

    Hope that helps,

    Cheers,

    Colin

  • MSLtdMSLtd Posts: 56Questions: 5Answers: 4

    @allan - it is unfortunately a synchronous process. Despite having been designed only two years ago, the software's architecture is very outdated and it's someone's job (enter: me) to update the system...
    At present I'm still avoiding the task of implementing asynchronous ajax handling due to the monolithic time requirement (the ajaxutil is utilised by every sub system - almost all of which require synchronous returns to not be undefined).

    @colin

    When I change the currency, It updates the currency assigned to the entity with the id m_nPO_ID in my database table, and then calls the redraw of the table. Via an extensive process of pasting console.log()s in my code I've found that whenever I draw the table after page reload, the render doesn't call upon getCurrencyByCID() - and neither ajax.reload() nor rows().invalidate() seem to change that :confused:

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin

    Ouch :). That's going to be one slow loading table!

    Is it possible it is doing some kind of caching which is causing the issue?

    Does it return bSuccess as true and is returnVal updated?

    Allan

  • MSLtdMSLtd Posts: 56Questions: 5Answers: 4

    @allan I know that the function: getCurrencyByCID() works fine, I call it whenever I load the page, and it works no problem when the table loads up the first time (I load everything in the table using the editor Field::inst - so that uses asynchronous processing, which is a total god send since this table will be populated with tens of thousands of rows over the coming years).
    I've found that the issue is that the columns render attribute doesn't call the function whenever the table is reloaded, or the ajax is reloaded, or the rows are marked invalid - thus invoking a reload.

    @colin has mentioned using rowCallback, but I feel like it'd be much more time effective to work out what the issue I'm experiencing is and fixing that if possible, than creating a new function, that may also cause me issues of some sort.

    Cheers - D

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Answer ✓

    I've found that the issue is that the columns render attribute doesn't call the function whenever the table is reloaded, or the ajax is reloaded, or the rows are marked invalid - thus invoking a reload.

    Ah! I'm with you now. That's because the renderer for the column has already been asigned. It was assigned during initialisation of the table. If you want to alter the rendering function you'd need to destroy the table and then create a new DataTable instance. There isn't currently a way to dynamically change the rendering function.

    The other option is to have a function inside the rendering function. It would lookup to see if there is a change, and if so, get the new prefix, then use that. If not, then use the old one. This is probably the method I would use myself for this.

    Allan

  • MSLtdMSLtd Posts: 56Questions: 5Answers: 4

    Aha! @allan , I understand now how this function works:
    $.fn.dataTable.render.number( ',', '.', 2, getCurrencyByCID ).

    Fortunately whilst waiting for your response I had been developing my own version of that function for a different system, where I needed to be able to pass in the data in the form data.coloum_name, and it was literally a copy and paste and done :joy:

    The function is used is:

    {
                    data: "line_item_unit_cost",
                    render: function (data){
                        var returnVal = getCurrencyByCID() + parseFloat(data).toFixed(2);
                        return returnVal;
                    }//$.fn.dataTable.render.number( ',', '.', 2, getCurrencyByCID )
                },
    

    And it works wonders

    Many Thanks
    - y

This discussion has been closed.