how do display database data in datatables dynamically
how do display database data in datatables dynamically
vaishnavkoka
Posts: 132Questions: 23Answers: 1
I want to display data in my datatable of webpage without reloading the webpage or click any button for the retrieval purpose, but i should be able to get more data using the pagination.
for example: https://datatables.net/examples/server_side/defer_loading.html
I need something of these kind but here the data is static, and even the ajax seems to pretty static in these example, i would like to know how would i achieve this task.
Thanks
Koka
This discussion has been closed.
Answers
The easy way is to use
ajax.reload()
to reload the data into the Datatable. Might not be the most efficient though. I have a Datatable that displays the contents of log messages that are pushed into a DB table. I perform the initial table load and get the highest unique ID. Then periodically query the DB for higher ID's and userows.add()
to add them to the Datatable.Kevin
https://datatables.net/examples/server_side/defer_loading.html
1.javascript part
line no: 5
"ajax": "scripts/server_processing.php",
ajax represents the location of the php file right ?
line no:6
"deferLoading": 57
how do i make this dynamic as there can be any number of records in the database
initially you have added some records in the datatable and these would increase with every click of pagination button.
but i completely want to display data which is retrieved from the database( every single record) and hence would like to make it dynamic retrieval.
css part
its clear to me
server-side script part
line no :36
why you have separately written the format for start_date?
well in my case i have to dates a) start date b)end date
how should i write a array for dates part?
line no:53
though i have written the correct details in my program i still didnt get the results on my webpage
line no:66
how do i write a code for ssp.class.php ?
can you should be your code ?
line no:68
how do i make use of json_encode?
is there any separate code written for json for your datatable ?
line no:69
i have used the post method in my php program and changed to get method still it didnt work
can you please help to solve my issue ? and how do i write a ajax code for my issue as you have mentioned in your answer
Thanks
Koka
Hi @vaishnavkoka - I don't think the example you have referenced is correct for what you're trying to achieve (correct me if I'm wrong). What exactly is it you're trying to do with your data tables?
Here's some links I think could help you with this:
Paging
//This is what enables the pagination control
dom
//This attribute is where you put the option to include pagination in the table
(Note most importantly the relationship between the 'p' in the dom string, and pagination)
@MSLtd ,
I have used the ajax code from here :
https://datatables.net/examples/server_side/post.html
But if the number of records of my datatable are less than 10 i need to reload the page to fetch the records dynamically from the database and moreover if the new records are added to the database(or the row values get changed in the database) , it doesnt shows up in my datatable until i reload the page.
can you help me out with there ?
Here is my code:
$(document).ready(function() {
var t= $('#example').DataTable( {
"lengthMenu": [[4, 8, 12, -1], [4, 8, 12, "All"]],
autoWidth:true,
paging: true,
"pagingType": "full_numbers",
"processing": true,
serverSide: true,
"ajax":"serverSide.php",
} );
} );
@vaishnavkoka , are the new records being added at the same page and are you using the editor to submit data?
if that is the case I'd recommend putting this:
In your
$(document).ready(function(){/*Your code here + new function })
When you've done that it should look something like this:
I am not declaring any column in that script and when i directly used your code i.e,
//(When information has successfully been submitted to the database)
editor.on( 'submitSuccess', function ( e, type ) {
//Completely reload the table from the data source - with new data
t.ajax.reload();
} );
I didnt get any output.
My datatabase table are :
http://prntscr.com/k5j4ol
My datatable is :
http://prntscr.com/k5j52t
Can you use the debugger to give us a trace of the table's state if you aren't able to link to a test case.
Thanks,
Allan
Here is the link :
https://debug.datatables.net/ucopob
Thanks,
Koka
Hi Koka,
The debug trace shows that the table is populated with 9 rows. But your image above shows zero rows. That doesn't appear to equate with the debugger results.
I think I'd need a link to a test case showing the issue.
Allan
@MSltd,
I am not using editor to submit data, i am using php to insert data in my mysql database and i am using ajax code as you have seen earlier to retrieve the data from database and display it in datatable.
@allan,
Here is the new report :
https://debug.datatables.net/oyigef
I am surprised to see the report.
script i have used:
Serverside code:
It is currently showing that you have a display length of 4, and 4 records (out of 21) are being shown:
Since you are using server-side processing, all you need to do to refresh the data is call
draw()
. You don't need to callajax.reload()
- although it basically does the same thing for server-side processing.Allan
I used the draw API, and i insert a record in my database just to check whether my datatable counter gets incremented , unfortunately to my surprise i found that until and unless i use the pagination buttons i couldn't the see the increment in counter of the page(Showing 1 to 4 of 26 entries). I used ajax reload and even in that case it didn't work out for me. The only thing worked for me is using below code:
setInterval( function () {
t.ajax.reload();
}, 5000);
t.draw();
But i dont think this as a feasible solution, moreover whenever a new entry is made into database, it would reflect on the last pagination button, Is it possible to show the new record on the first page of datatable?
Thanks
Koka
The ordering of the rows is entirely defined by the ordering applied to the column contents. It sounds like you might have reverse ordering from what you want. The
order
option will define the default order (to be applied by your server-side processing script).Allan
So you mean i need to use default ordering ?
To have a immediate effect of changes in the database which would reflect my datatable ?
The default for
order
is[[0, 'asc']]
. It sounds like you might want[[0, 'desc']]
. If its not thatn, then I've misunderstood and a test case showing the issue would be useful.Allan