¿Cómo se recarga mediante programación una tabla de datos jquery?
Frecuentes
Visto 816 equipos
2
I have a jquery datatable on a page that I want to reload after a user action. Also, I do not set the ajax
parameter on my datatable because the url is always changing and I need to process the JSON data a bit after I get it.
Here is my code to initially set the datatable.
$.getJSON(uri, function (people) {
// ... code removed for brevity ...
$("#people-table").dataTable({
"data": people,
"columns": [{
"data": "id",
"title": "Id"
}, {
"data": "full_name",
"title": "Name"
}, {
"data": "phone",
"title": "Home Phone"
}, {
"data": "cell_phone",
"title": "Mobile"
}, {
"data": "email",
"title": "Email"
}]
});
How do I reload the table after a user action (like a button click)?
1 Respuestas
2
Así es como lo haces.
(I would recommend only doing this if you must pre-process the JSON data before setting it into the datatable. If you can use the datatable ajax
parameter, that is preferable.)
$.getJSON(uri, function(people) {
// ... code removed for brevity ...
if (if ($.fn.DataTable.isDataTable("#people-table"))) {
var api = $("#people-table").dataTable().api();
api.clear();
people.forEach(function(row, idx) {
api.row.add(row);
});
api.draw();
} else {
$("#people-table").dataTable({
"data": people,
"columns": [
{ "data": "id", "title": "Id" },
{ "data": "full_name", "title": "Name" },
{ "data": "phone", "title": "Home Phone" },
{ "data": "cell_phone", "title": "Mobile" },
{ "data": "email", "title": "Email" }
]
});
}
contestado el 29 de mayo de 14 a las 15:05
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas datatables or haz tu propia pregunta.
Aaaaand I'm about to refactor this code and rip it all out. I can use the datatables
ajax
setting after all. I can use thecolumnDefs
to do my data manipulation. And I can useajax.url()
to change the URI. Money! Less is More! - Cadena