morlandi / django-ajax-datatable

A Django app which provides the integration of a Django project with the jQuery Javascript library DataTables.net
MIT License
208 stars 64 forks source link

Reload table with new data #3

Closed amirj700 closed 3 years ago

amirj700 commented 3 years ago

Hi Morlandi, Thanks for your good library, I was able to initialize my table with some extra data in 'initialize_table' function. now I'm looking for a method to reinitialize or reload the table with a bunch of new extra data. is there any way? I tried a few methods with table.ajax.reload() but they produced confusing errors.

any help is appreciated.

morlandi commented 3 years ago

Hello @amirj700, in the README, you can find this snippet:

AjaxDatatableViewUtils.initialize_table(
    ...
    }, {
        // extra_data
        client_id: '{{client.id}}',
        date_from: function() { return date_input_to_isoformat('#date_from'); },
        date_to: function() { return date_input_to_isoformat('#date_to'); }
    }
);

As you can see, you're not limited to specify constant values: you can pass JS functions as well.

This gives you more flexibility since you can adjust the return values of these functions based on some context variables. I suggest you check if this is enough to solve your use case.

For reloading tables, see:

and check these two helpers (file ajax_datatable/static/ajax_datatable/js/utils.js):