Run a function on refresh of Data Table

Hiya People!

Is there any way that I can run a function when a Data Table has been refreshed?

I have tried:

fd.control('TimeLog').ready().then(function(dt) {
		    dt.buttons[0].visible = false;
            dt.$on('change', function(){setTimeout(calculateStuff,1000)});
            dt.$watch('length', function(items) {
               //Do calculations
                setTimeout(calculateStuff,1000); 
            });
});

This works when an item is created or deleted from the table, but if the table is refreshed and new information is added to the table, the calculation doesn't happen.

Please help!

Hello @Jamal_Smith-Graham,

Are you using Data Table or List or Library control?

What exactly do you need to calculate?

Sorry it is a List Control.
I am trying to calculate the sum of a field and then update fields in the form, this is what calculateStuff function does:

function calculateStuff(){
    var productLineItems = fd.control('ProductLineItems').widget.dataItems();
    var [totalCostToLucidica,totalCostToClient,orderProfit,margin,productTime,productNetRate] = [0,0,0,0,0,100];
    //Calculate Product Profitablity
    if (productLineItems.length > 0){
        totalCostToClient = parseFloat((productLineItems[0]['TotalCosttoClient.SUM']).replace('£',''));
        totalCostToLucidica = parseFloat((productLineItems[0]['TotalCosttoLucidica.SUM']).replace('£',''));
        orderProfit = totalCostToClient - totalCostToLucidica;
        if (totalCostToClient != 0 || totalCostToClient != null){
            margin = orderProfit * 100 / totalCostToClient;
        } 
    }
    
    //Calculate Product NetRate
    var productTimeItems = fd.control('TimeLog').widget.dataItems();
    if (productTimeItems.length > 0){
        productTime = parseFloat(productTimeItems[0]['Time.SUM']) / 60;
        if (productTime != 0 || productTime != null){
            productNetRate = orderProfit / productTime;
        } 
    }
    //Update Fields
    updateFieldTitles(productNetRate,margin);
    disableFields(false);
    fd.field('Supplier_x0020_Invoice_x0020_Tot').value = totalCostToLucidica;
    fd.field('Lucidica_x0020_Invoice_x0020_Tot').value = totalCostToClient;
    fd.field('Order_x0020_Overall_x0020_Profit').value = orderProfit;
    fd.field('Margin').value = margin;
    fd.field('Product_x0020_Time').value = productTime;
    fd.field('Product_x0020_NRBPBH').value = productNetRate;
    setTimeout(function(){disableFields(true);},10);
}

I just need to trigger it when the table is refreshed manually as we have the helpdesk plugin so all List controls have a refresh button hard coded in:
image

Hello @Jamal_Smith-Graham,

You can customize the click function of the refresh button like this:

fd.control('SPDataTable1').ready().then(function(dt) {
    //customize refresh button click function
    dt.buttons[4].click = function() {
        fd.control('SPDataTable1').refresh().then(function(){
            alert('List refreshed!');
            //your function
        });
    }
});

You can find more information here:
https://plumsail.com/docs/forms-sp/how-to/list-or-library-buttons.html

3 Likes

ooooh that is awesome! Thank you @mnikitina!

1 Like

Thanks for the sample code snippet @mnikitina!

Is there a way to use the click functionality provided above with the name of the refresh button instead of it's index in case the index changes for whatever reason?

Hello @stormanh,

You can find specific button by icon name like this:

fd.control('SPDataTable1').ready().then(function(dt) {
    //customize refresh button click function
    dt.buttons.forEach(function(b){
        if(b.icon == "Refresh"){
            b.click = function() {
                fd.control('SPDataTable1').refresh().then(function(){
                    alert('List refreshed!');
                    //your function
                });
            }
        }
    });
});
3 Likes

How do I edit this to refresh a list or library?

fd.spRendered(function() {
//new button
var button = {
    text: 'Quick action',
    class: 'btn-secondary',
    visible: true,
    icon: 'LightningBolt',
    iconType: 0,
    click: function() {
        alert('Button clicked!');
    }
}

fd.control('SPDataTable1').ready().then(function(dt) {
    //dt parameter is the same as fd.control('SPDataTable1')
    dt.buttons.push(button);
});

});