Change data source from variable to ajax dinamicaly
Change data source from variable to ajax dinamicaly
koa73
Posts: 6Questions: 1Answers: 0
Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
Is it possible to set first load data from local variable, which I've load during make this page, but then set ajax source data. I wanna load start page with static data, but don't wont to create table on the server side
Thx
Answers
You can use
data
to load the data during initialization, see this example. Or you can userow.add()
(1 row) or ``-api rows.add()` (multiple rows) to add the rows after Datatables has been initillaized.Kevin
If I set ajax option the table will be initialized from ajax request. If I set data options, I should table filling processing by my self May be I did't understand your answer
Maybe I don't understand the question
You are wanting to load the initial data from a local Javascript variable, correct? If so my response explains how.
Are you then wanting to use
ajax
for loading subsequent data? If so please explain your requirements and how you plan to use this feature.Kevin
First load from local variable.
Second load (by on button click event) from server by ajax with data parameters
Probably the easiest way is to use jQuery ajax to fetch the data. You can use
clear()
to clear the table before the ajax call or use it in thesuccess
function. In thesuccess
function userows.add()
to populate with the new data.Kevin
Ok thx
Do you mean the
ajax.dataSrc
option?What events catcher and how do they not work well. Please provide more specifics like do you get errors, etc.
Maybe instead of
"data": "some variable"
you need to useajax.data
as a function to allow for different values. See the examples in the docs and this example. You don't need to enableserverSide: true
as shown in the example.The best thing to do is to provide a link to your page or a test case replicating the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
"What events catcher and how do they not work well. Please provide more specifics like do you get errors, etc."
.on( 'preDraw', function() {sp = {buy:0, sell:0, buyErr:0, sellErr:0};})
.on( 'draw.dt', function() {
$.when(offSpinner()).then(
function(){
gageMaker(sp, "Accuracy");
commonDonut();
initCount('day-', [sp.sell, sp.buy, sp.sellErr, sp.buyErr]);
}
)
They are do nothing If Iset data source like this https://datatables.net/reference/option/data
And they are work if I set options for data source like this https://datatables.net/examples/server_side/custom_vars.html
"Maybe instead of "data": "some variable" you need to use ajax.data" I use this way but I wanna set data source 1 time "data": "some variable" , but on event 'click button' ajax
"The best thing to do is to provide a link to your page or a test case replicating the issue so we can help debug." it's not available at this moment
In general the Datatables initialization options can't be changed. There are some cases like
ajax.url()
where you can make changes. Datatables isn't built to support bothdata
andajax
at the same time. If you want to load the initial data usingdata
then the use of jQuery ajax will be much easier than trying to manipulate Datatables to go from usingdata
toajax
. See my above comments.To have the
preDraw
anddraw
events called at initialization when usingdata
you will need to initialize the event handlers before initializing Datatables, something like this:http://live.datatables.net/sowuheha/1/edit
The reason they work when using ajax is Ajax is asynchronous and the event handlers are initialized while waiting for the ajax response.
Kevin
It's cool. Thx a lot. Everything work ok after changing order of events (set it before table initialize) So now I load data from variable then call load data by ajax on click event.
table_arch.ajax.url( 'rest/data/history/' + d).load();
Set ajax error handler
$.fn.dataTable.ext.errMode = function ( settings, helpPage, message ) {
console.log(message);
};
Everything is ok Thx
No. This is why I suggested using jQuery ajax.
Kevin
Maybe you can incorporate the use of
$.fn.dataTable.ext.errMode
anderror
if you still want to useajax.url().load()
.Kevin