I have a simple code to pull data using ajax in my datatable:
$(document).ready(function() {
$('#datatable').dataTable( {
"pageLength": 50,
"ajax": "/test/pull/",
"columnDefs": [ {
"targets": 6,
"data": null,
"defaultContent": "<button type=\"button\" class=\"btn btn-success btn-sm\" onclick>Click</button>"
} ],
"columns": [
{ "data": "a" },
{ "data": "b" },
{ "data": "c" },
{ "data": "d" },
{ "data": "r" },
{ "data": "f" }
]
} );
I am pulling from server one more column - g and I would like to add this value as a new attribute ( for example ID ) to a button named "Click".
Was trying to find anything in google but unsuccessfully - will be thankful for your support
Thanks in advance,
Suppose I have the following json to display in my DataTable:
// JSON structure for each row in this example:
// {
// "engine": {value},
// "browser": {value},
// "platform": {value},
// "version": {value},
// "grade": {value}
// }
$('#example').dataTable( {
"ajaxSource": "sources/objects.txt",
"columns": [
{ "data": "engine" },
{ "data": "browser" },
{ "data": "platform" },
{ "data": "version" },
{ "data": "grade" }
]
} );
What I want is, Add an Index Column to this data table for number the row.
Something like this :
"columns": [
{"data" : "Index"}, <------- this should number my rows
{ "data": "engine" },
{ "data": "browser" },
{ "data": "platform" },
{ "data": "version" },
{ "data": "grade" }
]
Note: I don't have any Index as data passed in my Json(Although I can do that, is there any better solution to handle this in my Javascript itself? )
Help appreciated..!
Try this.
"render": function ( data, type, full, meta ) {
return meta.row + 1;
} },
The concept is that you have to create the initial "Index" values either in javascript or in the server. The values can be zero or just empty strings or whatever (there is no need to calculate a counter or something). For example you can create an index column in javascript after you have received the data:
for (var i=0; i<data.length; i++){
data[i].index = 0;
}
So now that you have the index column in your data you declare it as the first column of your table:
$('#example').dataTable( {
.....
"columns": [
{ "data": "index" },
{ "data": "engine" },
{ "data": "browser" },
{ "data": "platform" },
{ "data": "version" },
{ "data": "grade" }
]
} );
Now the index values are all 0. To create the real index values that will be shown in the table you need to add an event handler that listens to the ordering and searching of the table. On these events the real index values will be calculated as described in the
datatables example:
datatable_object.on( 'order.dt search.dt', function () {
datatable_object.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
cell.innerHTML = i+1;
} );
} ).draw();
When the table is searched or ordered (ordering is done by default when the table is created - see default value of the order option), the innerHtml of the "Index" cells will be calculated based on the index of the row.
Just add the code below to your datatables
{ 'data': 'id', defaultContent: '' },
"columnDefs": [ ////define column 1 , make searchable false
{
"searchable": false,
"orderable": false,
"targets": 0
},
Below is the updated code:
var table=$('#example').dataTable( {
"ajaxSource": "sources/objects.txt",
"columns": [
{ 'data': 'id', defaultContent: '' },
{ "data": "engine" },
{ "data": "browser" },
{ "data": "platform" },
{ "data": "version" },
{ "data": "grade" },
"columnDefs": [ ////define column 1
{
"searchable": false,
"orderable": false,
"targets": 0
},
]
});
And the following line will add number to your id(index) column:
if (t.data().length != 0) {
t.on('order.dt search.dt', function () {
t.column(0, { search: 'applied', order: 'applied' }).nodes().each(function (cell, i) {
cell.innerHTML = i + 1;
t.cell(cell).invalidate('dom');
});
}).draw();
Live example: http://live.datatables.net/woboviqi/2/edit
Need to use DT_RowIndex for indexing rows. Like this - ## Heading ##
"columns": [
{ "data": 'DT_RowIndex'}, // row index
{ "data": "engine" },
{ "data": "browser" },
{ "data": "platform" },
{ "data": "version" },
{ "data": "grade" }
]
You can use unshift()
"columns": [
{ "data": "engine" },
{ "data": "browser" },
{ "data": "platform" },
{ "data": "version" },
{ "data": "grade" }
].unshift({"data" : "Index"})
Or by using an temp array
var cols = [{
"data": "engine"
}, {
"data": "browser"
}, {
"data": "platform"
}, {
"data": "version"
}, {
"data": "grade"
}];
cols.unshift({
"data": "Index"
})
....
"columns": cols
Have a look on this URL: Data table Index column
It might help you.
Here is my code you can refer to:
My DataTable is complete custom.
I'm fetching data from the database using Ajax and CodeIgniter.
HTML
<table width="100%" class="table table-striped table-hover" id="table_id_dataTable">
<thead>
<tr>
<th>Sr No</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
<th>Column 5</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Sr No</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
<th>Column 5</th>
</tr>
</tfoot>
<tbody>
</tbody>
</table>
JS Script
var temp_table = $('#table_id_of_dataTable').DataTable({
"language": {
"zeroRecords": "No records found."
},
"ajax": {
"type": "POST",
"url": "<?php echo base_url('Controller/method'); ?>"
},
"responsive": true,
"columnDefs": [
{
"searchable": false,
"orderable": false,
"targets": 0
}
],
"order": [
[1, 'asc']
],
"columns": [
{ "data": null }, // <-- This is will your index column
{ "data": "column_2_element_name_given_in_controller" },
{ "data": "column_3_element_name_given_in_controller" },
{ "data": "column_4_element_name_given_in_controller" },
{ "data": "column_5_element_name_given_in_controller" }
]
});
// Here we create the index column in jquery datatable
temp_table.on('order.dt search.dt', function() {
temp_nuggets_table.column(0, {
search: 'applied',
order: 'applied'
}).nodes().each(function(cell, i) {
cell.innerHTML = i + 1;
});
}).draw();
Hope it helps!
That is simple... This work for me.
Cell: function ( data, type, full, counter ) {
return data.index + 1
}
enter link description here
Try this:
var table=$('#example').dataTable( {
"ajaxSource": "sources/objects.txt",
"columns": [
{
"data": null, "render": function (data, type, full, meta) {
return meta.row + 1;
}
},
{ "data": "engine" },
{ "data": "browser" },
{ "data": "platform" },
{ "data": "version" },
{ "data": "grade" }
});
table.on('order.dt search.dt', function () {
table.column(0, { search: 'applied', order: 'applied' }).nodes().each(function (cell, i) {
cell.innerHTML = i + 1;
});
}).draw();
enter link description here
Try this:
var table=$('#example').dataTable( {
"ajaxSource": "sources/objects.txt",
"columns": [
{
"data": null, "render": function (data, type, full, meta) {
return meta.row + 1;
}
},
{ "data": "engine" },
{ "data": "browser" },
{ "data": "platform" },
{ "data": "version" },
{ "data": "grade" }
});
I want to implement function in which data will be loaded into datatable after onChange event. So for that I am trying to implement code as below.
var viewdatatab = $('#dataTablesFeedback').dataTable({
"columns": [
{ "data": "resourceId" },
{ "data": "feedbackRecommendation" },
{ "data": "technicalSkillGaps" },
{ "data": "technicalAvgSkills" },
{ "data": "feedbackType" },
{ "data": "feedbackId" },
{ "data": "isNew" },
]
});
Which is creating my datatable layout and I am calling below function on dropdown change event is :
function loadFeedback(){
viewdatatabJS = $('#dataTablesFeedback').dataTable({
"processing" : true,
"retrieve" : true,
"ajax" : "/nhp/rest/feedback/viewFeedback",
"fnServerParams": function ( aoData ) {
aoData.push( { "name": "userName", "value":employeeId } ,
{ "name": "resourceId", "value":mentorDataJson[$('#dropDownId').val()].resourceId });
},
});
}
Where I am passing some parameter in aoData.push but my URL is not getting called.
I Solved the issue by simply implementing datatable properties. i wrote my code of datatable
var viewdatatab = $('#dataTablesFeedback').dataTable({
"columns": [
{ "data": "resourceId" },
{ "data": "feedbackRecommendation" },
{ "data": "technicalSkillGaps" },
{ "data": "technicalAvgSkills" },
{ "data": "feedbackType" },
{ "data": "feedbackId" },
{ "data": "isNew" },
]
});
in jsp document.ready(function()) and then on my request call of drop down change event i wrote below code on my javascript function.
$.ajax({
url : "",
type: 'GET',
contentType: "application/json",
data: {
'userName': value,
'resourceId' : value,
},
success: function(data) {
var table = $('#dataTablesFeedback').DataTable();
table.clear();
table.rows.add(data.data);
table.draw();
});
this way i first clear my datatable and then redraw it using my json which i got from my ajax call.
Thanks
My JSON data looks like this and I am trying to get it sent through datatables.
{
"RANDOM-UNIQUE-STRING-1":
{
"column1": "stuff",
"column2": "more stuff",
"column3": "example"
},
"RANDOM-UNIQUE-STRING-2":
{
"column1": "stuff",
"column2": "more stuff",
"column3": "example"
},
{ ... }
}
I can't figure out how to look passed RANDOM-UNIQUE-STRING-1, etc and go straight for the data. This is what I have so far. Anyone think they could help?
var theTable = $('#mytable').dataTable({
"bProcessing": true,
"aaData": data, //data == my above JSON object
"aoColumns": [
{ "mData": "column1" },
{ "mData": "column2" },
{ "mData": "column3" }
]
});
You could make a function that "normalizes" the JSON. Like this :
function normalize(data) {
var result = [];
for (var row in data) {
result.push(data[row]);
}
return result;
}
and then call that function in your dataTables initialisation :
var theTable = $('#mytable').dataTable({
aaData: normalize(data),
aoColumns: [
{ mData: "column1" },
{ mData: "column2" },
{ mData: "column3" }
]
});
demo -> http://jsfiddle.net/o27hgzjr/
I have a html drop down that initially populates and shows the data table. The data is coming from a json array. Once the drop down is changed i'm getting the reinitialise error and having trouble fixing it.
I've tried the table.ajax.reload and also the table.fnReloadAjax(); I work with datatables off an on so not the greatest with them.
Here is the code:
function Population() {
var table = $('#Population').dataTable();
$("#quickStats").change(function () {
var optionValue = $("#quickStats").val();
console.log(optionValue);
if (optionValue == 1) {
$("#display").append('<table cellpadding="0" cellspacing="0" border="0" class="display" id="Population">' +
'<thead><tr><th>Demographic</th><th>Total</th></thead>'
+ '</table>');
$('#Population').dataTable({
"data": usPopulation,
"bJQueryUI":true,
"columns": [
{ "data": "Demographic" },
{ "data": "Total" }
],
});
}
table.ajax.reload();
});
}
Try destroying the previous one before creating new:
$('#Population').dataTable().fnDestroy();
$('#Population').dataTable({
"data": usPopulation,
"bJQueryUI":true,
"columns": [
{ "data": "Demographic" },
{ "data": "Total" }
],
});
or do like this:
var table = $('#Population').dataTable({
"data": usPopulation,
"bJQueryUI":true,
"columns": [
{ "data": "Demographic" },
{ "data": "Total" }
],
});
table.draw();