Can we initialize datatable in success property of ajax?
Can we initialize datatable in success property of ajax?
chaturvedi_anshumaan
Posts: 25Questions: 5Answers: 0
Question description : https://stackoverflow.com/questions/61023260/can-we-initialize-datatable-in-success-property-of-ajax-call? Can anyone guide me in this ?
This discussion has been closed.
Answers
I copied your code here so we can look at it:
Thats a lot of code to look through. It will be hard to debug without actually seeing the page and the data. Can you post a link to your page or a test case replicating the issue?
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
To answer your question - yes you can initialize Datatables from within the
success
function. Not sure I totally get what you are trying to do.In your SO thread you state:
Does this mean you see an empty Datatable or no Datatable at all?
It seems like what you have is overly complex. You have
serverSide: true,
which is expecting the row data to be paged by the server. Does your server script perform paging or return all of the data?Can you post a snippet of the JSON response from your server?
Kevin
Wow, Kevin,, i am not sure what all to post.
But here goes :
1.Does this mean you see an empty Datatable or no Datatable at all?- I am not seeing any dataTable at all.
2.Does your server script perform paging or return all of the data?-- Row data is not paged by server. It just returns the data from Stored Procedure.
So i am not sure if this would suffice but this is what i am getting JSON response from action controller method.
Thats a good start, thanks.
Looks like 150 rows of data is return along with some other data points. If you just want to display those 150 rows you could do something as simple as this:
http://live.datatables.net/meqofepo/1/edit
You would use
data.res
instead ofdata.data
, for example:You are doing some data manipulation in the Datatables
ajax
function. You can move it into the success function before initializing Datatables.Remove the
serverSide: true
option and theajax
option. Lets see if that gets you closer to what you want.Kevin
Kevin,
I actually solved this problem and ran into something else now. I achieved the onscroll data load. however, after scroll when the next set of 150 records is supposed to come what is happening that it is showing me previous records. I check edin controllers the data they are sending is correct but why previous set of data is gettig loaded in baffling. Morever ,the new set of data i see for a fraction of second before the grid gets refreshed to previous data automatically.
Guessing you are calling
draw()
somewhere that is causing it to go to the first page??Please post a link to your page or a test case replicating the issue so we can take a look.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
setTimeout(function () {
callback({
draw: data.draw,
data: out,
recordsTotal: parseInt(countRecords),
recordsFiltered: parseInt(countRecords)
});
}, 14);
I think ,this might be the problem.
ok, let me generate a link so that you can replicate the test. But , how will i replicate the database, i don't know. My data is coming from database.
You were,right. When i made one of the columns to be displayed in ascending order, i found that after few micro seconds it is going to the first page.
But i have not yet figured out solution.
It looks like you're fiddling with the data inside
ajax
. Wouldn't it be easier to do that in thesuccess
function for the ajax call, and then pass that data into DataTables withdata
?Colin
Any chance you are talking like this?
@colin : are you talking about the above type of code.. it is very complicated what i had to do.
No, the original code around like 54 - that's what I was referring to.
Colin
Lie number 30 in new code is the same.