Ordering columns in JQuery DataTable -- when data is fed through Javascript - javascript

There are some forum questions similar to what I'm trying to do but I haven't been able to make it work.
We are pulling data into a DataTable through javascript. I'd like to set the data-order using a different column.
However, when I try the column is being interpreted as a string and not integer.
Here is what makes the table:
var securityTable = $('#security-table').DataTable({
"data": securitydata.guards,
"columns": [
{
"className": 'details-control',
"data": null,
"orderable": false,
//creates square for details row
"render": function (d) {
return '<i class="fa fa-plus-square" aria-hidden="true"></span>';
},
"defaultContent": ''
},
// is sorting by "sort" but is seeing numbers as alphebetical not numeric
{ "data": {
_: "date.display",
sort: "date.date_order"
} },
{ "data": "place" },
{ "data": {
_: "shot.display",
sort: "shot.shot_order"
} },
],
"paging": false,
"searching": false
});
This is what the data looks like:
var securitydata = {
"guards": [
{
"date": {
"display": "April 15, 2011",
"date_order": 1
},
"reported": "Yes",
"place": "Chicago, auto parts yard",
"shot": {
"display": "No one hit",
"shot_order": 24
},
"blurb": "A 52-year-old guard at an auto parts lot shot at a vehicle he said was coming toward him. The man inside the vehicle, accused of stealing equipment from the lot, drove away and was not reported injured.",
"link": ""
},
This is what we were using for help

Using the same example, but with sorthing
$(document).ready(function() {
$('#example').DataTable( {
order: [[ 2, "desc" ]],
ajax: "data/orthogonal.txt",
columns: [
{ data: "name" },
{ data: "position" },
{ data: "office" },
{ data: "extn" },
{ data: {
_: "start_date.display",
sort: "start_date.timestamp"
} },
{ data: "salary" }
]
} );
} );
Link to order here

Related

Format data returned from JSON in DataTable [duplicate]

This question already has answers here:
use of comma and datatable decimals
(2 answers)
Closed 7 months ago.
I am reading a json file from remote url and displaying it in DataTable.
<script>
$(document).ready(function () {
$('#example').DataTable(
{
ajax: {
method: 'GET',
url: 'https://chainformed.s3.us-east-1.amazonaws.com/Data/mostwatched.json',
dataSrc: ''
},
paging: false,
responsive: true,
language: {
url: "//cdn.datatables.net/plug-ins/1.12.1/i18n/tr.json",
},
columns: [
{ "data": "id" },
{ "data": "rank" },
{ "data": "name" },
{ "data": "watchlist" },
{ "data": "percentage" },
{ "data": "difference" },
{ "data": "timestamp" }
]
});
$('.dataTables_length').addClass('bs-select');
});
</script>
I would like to be able to format and manipulate the data returned from that json file. What is the best way to do it?
For example, i would like to change the values from ({ "data": "watchlist" }) field to number format with comma. How can i achieve it?
I tried this but with no luck: https://datatables.net/reference/option/ajax.dataSrc
Use render element.
Read document from Format output data - orthogonal data
and example
Another way u can use columnDefs
<script>
$(document).ready(function () {
$('#example').DataTable(
{
ajax: {
method: 'GET',
url: 'https://chainformed.s3.us-east-1.amazonaws.com/Data/mostwatched.json',
dataSrc: ''
},
paging: false,
responsive: true,
language: {
url: "//cdn.datatables.net/plug-ins/1.12.1/i18n/tr.json",
},
columns: [
{ "data": "id" },
{ "data": "rank" },
{ "data": "name" },
{ "data": "watchlist" },
{ "data": "percentage" },
{ "data": "difference" },
{ "data": "timestamp" }
],
'columnDefs': [
{
"targets": 3,
"data": "watchlist",
"render": function ( data, type, row, meta ) {
return data.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
}
]
});
$('.dataTables_length').addClass('bs-select');
});

jquery datatable doesn't sort

I have a jquery datatable that is populated by server side ajax. Supposedly, datatables are supposed to be sortable by default without having to add any parameters. Mine isn't. The sort arrows show up in the column headers and clicking on them flips the arrow but nothing gets sorted.
Here's the datatable definition:
$('#appPotTable').DataTable({
"ordering": true,
"processing": true,
"serverSide": true,
"ajax": "/MoneyMachine/screen_analystEst.php",
"columns": [
{ "data": "Symbol", "sortable":true },
{ "data": "CompanyName" },
{ "data": "StockType" },
{ "data": "ExDivDate" },
{ "data": "Dividend" },
{ "data": "DivYield" },
{ "data": "DivFrequency" },
{ "data": "DivPayDate" },
{ "data": "PriceToNav" },
{ "data": "AppreciationPotential" }
]
});
I've tried it with and without the "ordering" and "sortable" parameters but same result. I've also tried various column definition parameters with no joy. Suggestions?
Thanks to Ogreucha for suggesting to turn off server side processing. I did that and now sorting works fine. Here is the new code:
$('#appPotTable').DataTable({
"ajax": "/MoneyMachine/screen_analystEst.php",
"columns": [
{ "data": "Symbol" },
{ "data": "CompanyName" },
{ "data": "StockType" },
{ "data": "ExDivDate" },
{ "data": "Dividend" },
{ "data": "DivYield" },
{ "data": "DivFrequency" },
{ "data": "DivPayDate" },
{ "data": "PriceToNav" },
{ "data": "AppreciationPotential" }
]
});

How to assign an ID to a button in DataTables?

I have a table with some content and Edit button. This is the code which produces a datatable:
$(document).ready(function() {
var params = {};
callFunction("getAgentList", params,
function(bSuccess, res) {
var table = $('#example').DataTable({
data: res,
"columns": [
{ "data": "ID" },
{ "data": "Name" },
{ "data": "AltName1" },
{ "data": "AltName2" },
{ "data": "AltName3" },
{ "data": "AltName4" },
{ "data": "AltName5" },
{ "data": "AltName6" },
{ "data": "AltName7" },
{ "data": "AltName8" },
{ "data": "AltName9" },
{ "data": "AltName10" },
{
"orderable": false,
"data": null,
"defaultContent": '<button class="btn btn-warning" id='+res+' onclick="edit(this)">Edit</button>'
},
],
"order": [
[1, 'asc']
]
});
}
);
});
Each row should have an edit button for editing that specified row. For this I need an ID which I have in res JSON. But I am not sure how to get the ID for each of the edit buttons?
Right now if I put only res I get Object or with res.ID it is undefined. Which makes sense because I need to loop over the array...
Anyone?
EDIT
This is the res JSON:
{ AltName1: "Test1", AltName10: "", AltName2: "Test1Alt", AltName3: "Test1", AltName4: "Test1", ID: "1" }
{ AltName1: "Test2", AltName10: "", AltName2: "", AltName3: "", AltName4: "", ID: "2" }
If I set the id as <button class="btn btn-warning" id='+res[0].ID+' onclick="edit(this)">Edit</button>
Then all buttons have the same ID.
Change all the first letters to lowercase in JavaScript then use the render function of datatable:
{ "targets": 0, "data": 'iD' },
{ "targets": 1, "data": 'name' },
{ "targets": 2, "data": 'altName1' },
{ "targets": 3, "data": 'altName2' },
{ "targets": 4, "data": 'altName3' },
{ "targets": 5, "data": 'altName4' },
{ .... },
{
"targets": 6,
"data": 'iD',
"render": function (data, type, row, meta) {
return '<button class="btn btn-warning" id='+data+' onclick="edit(this)">Edit</button>'
}
}
In the render function you can access the ID with different ways :
1 : "data" : 'iD' then you use data (as I did)
2 : "data" : 'anything you want' then you use : row.iD
Also note please that you need to add a prefix before the ID to avoid repeated Id's (I always do that)
just try this
"defaultContent": '<button class="btn btn-warning" id='+ID+' onclick="edit('+ID+')">Edit</button>'

How to load json foriegn key data to jquery datatable

This is how my JSON data is getting using the url.
[
{
"id": 1,
"fullName": "Menushi",
"email": "menushi#gmail.com",
"contact": "04567383",
"department": {
"id": 1,
"departmentName": "Computing and Information System(CIS)",
"description": "This department is regarding computer science ",
"active": true
},
"imageUrl": "LCR438D8E9C68",
"active": true,
"file": null,
"address": "Polonnaruwa"
},
I want to display the department name in the Jsondata table.Below shows the part of the javascript file.Department object is in onetoone relationship.I want to display department object data also in the table.
$table.DataTable({
pageLength: 5,
lengthMenu: [[3, 5, 10, -1], ["3 Records", "5 Records", "10 Records", "All"]],
ajax: {
url: jsonUrl,
dataSrc: ''
},
columns: [
{
data : 'imageUrl',
mRender : function(data,type,row){
return '<img src="'+window.root+'/resources/images/'+data+'.jpg" class="dataTableImg">';
}
},
{
data: 'fullName'
},
{
data: 'address'
},
{
data: 'email'
},
{
data: 'department.departmentName',
},
{
data: 'contact'
}
I have used the above way to display the department name in my javascript file..But it display nothing.How I can over come this issue?

How to add edit functionality in datatable with bootstrap and MVC

I have done following code and now iI want to add edit column in Bootstrap Datatable with server side functionality in MVC and Bootstrap. Finally I want proper CRUD operation, so how can I approach for this?
<script type="text/javascript">
var assetListVM;
$(function () {
assetListVM = {
dt: null,
init: function () {
dt = $('#assets-data-table').DataTable({
"serverSide": true,
"processing": true,
"ajax": {
"url": "#Url.Action("Get","Asset")"
},
"columns": [
{ "title": "ID", "data": "ID", "searchable": true },
{ "title": "Name", "data": "Name", "searchable": true },
{ "title": "City", "data": "City", "searchable": true },
{ "title": "EmpCode", "data": "EmpCode", "searchable": true },
{ "title": "DOJ", "data": "DOJ", "searchable": true },
{ "title": "Address", "data": "Address" },
{ "title": "DOB", "data": "DOB" }
],
"lengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]],
});
}
}
// initialize the datatables
assetListVM.init();
});
</script>

Categories