Datatables columns and JSON Format - json

I have setup a datatables based on the following html
<script>
$('#tableListing').dataTable({
"bAutoWidth" : true,
"bProcessing" : true,
"bServerSide" : true,
"sAjaxSource" : '${actionUrl}',
"aoColumns":[
{ "sTitle": "ID", "mData": "id" },
{ "sTitle": "Username", "mData": "userName" },
{ "sTitle": "Password", "mData": "password" }
]
}
});
</script>
<table id="tableListing" class="display datatable">
<thead>
<tr>
<th><spring:message code="columnLabel.id" /></th>
<th><spring:message code="columnLabel.username" /></th>
<th><spring:message code="columnLabel.enabled" /></th>
</tr>
</thead>
</table>
And the JSON response from server is [1,"abiieez",true] and it works nicely. However I have changed the response from server into something like:
*Edit (updated JSON response) *
{
"iTotalDisplayRecords": 11,
"iTotalRecords": 11,
"aaData": "[{\"creationTime\":0,\"enabled\":true,\"id\":1,\"loginDuration\":0,\"online\":false,\"password\":\"asdasd\",\"userName\":\"abiieez\"},{\"creationTime\":0,\"enabled\":false,\"id\":105,\"loginDuration\":0,\"online\":false,\"password\":\"asdasd\",\"userName\":\"asd1212123\"},{\"creationTime\":0,\"enabled\":false,\"id\":106,\"loginDuration\":0,\"online\":false,\"password\":\"asdsdasd\",\"userName\":\"asdasd23123\"},{\"creationTime\":0,\"enabled\":false,\"id\":107,\"loginDuration\":0,\"online\":false,\"password\":\"asdsdasd\",\"userName\":\"asdasd23123\"},{\"creationTime\":0,\"enabled\":false,\"id\":108,\"loginDuration\":0,\"online\":false,\"password\":\"anak jalanan\",\"userName\":\"alibaba90\"},{\"creationTime\":1351338218227,\"enabled\":false,\"id\":109,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338263527,\"enabled\":false,\"id\":110,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338265078,\"enabled\":false,\"id\":111,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338266329,\"enabled\":false,\"id\":112,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"},{\"creationTime\":1351338267247,\"enabled\":false,\"id\":113,\"loginDuration\":0,\"online\":false,\"password\":\"asdasdasda\",\"userName\":\"asdasdasda\"}]",
"sEcho": "1"
}
The above json is not accepted by the datatables, I believe because the number of columns do not match.
Is there a way for me to retrieve this json object and pick which columns I would like to show on the tables on the client side ?

You can use mData property in aoColumns like this:
$(document).ready(function() {
$('#tableListing').dataTable({
"aoColumns":[
{ "sTitle": "user", "mData": "userName" },
{ "sTitle": "pass", "mData": "password" },
...
]
"bAutoWidth" : true,
"bProcessing" : true,
"bServerSide" : true,
"sAjaxSource" : '${actionUrl}'
});
});

Related

Getting parserError when calling controller using ajax and set data in datatable in mvc project

I am working in mvc and i got the parserError when i call the controller using ajax and want to set list using datatable. but i don't know what i am doing wrong here.
when try to debug then i was not got my self in controller.
After i searched i found that use datatype:'text' but that don't want it.
I want only json data and want to set using data table.
but don't know what i doing wrong here.
Any one have idea, let me share here.
<div class="card-body">
<table id="userGrid" class="table table-bordered table-striped">
<thead>
<tr>
<th>Email</th>
<th>Password</th>
<th>Zipcode</th>
<th>IsAdmin</th>
<th>IsFirstTimeLoggedIn</th>
</tr>
</thead>
</table>
</div>
#section Scripts{
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "/User/UserList",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert("failure:"+response.d);
},
error: function (response, jqXHR, textStatus, errorThrown) {
debugger
alert("Error:"+response.d);
}
});
});
function OnSuccess(response) {
$("#userGrid").DataTable(
{
"paging": true,
"lengthChange": false,
"searching": false,
"ordering": true,
"info": true,
"autoWidth": false,
"responsive": true,
"serverSide": true,
"processing":true,
data: response,
columns: [{ "data": "Email" },
{ "data": "Password" },
{ "data": "Zipcode" },
{ "data": "IsAdmin" },
{ "data": "IsFirstTimeLoggedIn" }]
});
};
</script>
}
public ActionResult UserList()
{
try
{
List<User> users = _userService.GetAllUsers().ToList();
return Json(new { data = users }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
_log.ErrorFormat("Error in get user list. Error : {0}", ex.Message);
_log.Error(ex);
throw;
}
}
I want only json data and want to set using data table
Since the json format returned by the UserList method contains data before the users , you need to obtain the returned data collection through response.data in the success method of ajax.
On the other hand, because you get the data in the success method (not directly through ajax), you need to comment out the "serverSide": true statement.
Here is the example:
function OnSuccess(response) {
$("#userGrid").DataTable(
{
"paging": true,
"lengthChange": false,
"searching": false,
"ordering": true,
"info": true,
"autoWidth": false,
"responsive": true,
// "serverSide": true,
"processing":true,
data: response.data,
columns: [{ "data": "Email" },
{ "data": "Password" },
{ "data": "Zipcode" },
{ "data": "IsAdmin" },
{ "data": "IsFirstTimeLoggedIn" }]
});
};
Here is the test result:

KendoUI Grid Server Binding Example

I have successfully set up several KendoUI Grids, but I cannot get one using server-side paging to work.
I modified my rest service so I will return a total value (hard coded right now).
I also modified my javascript source. [see below].
Usually I just get a blank screen.
Would be very grateful for any assistance.
Script:
$(document).ready(function(){
// Setup Rest Service
var loc = ( location.href );
var url = loc.substring( 0, loc.lastIndexOf( "/" ) ) + "/xpRest.xsp/test/";
dataSource = new kendo.data.DataSource({
pageSize: 20,
serverPaging: true,
serverFiltering: true,
serverSorting: true, transport : {
read : {
url : url + "READ",
dataType : "json"
},
type : "READ"
},
schema : {
total: "total",
model : {
id : "unid",
fields : {
unid : {
type : "string",
nullable : false
},
tckNbr : {
type : "string",
editable : false
},
tckSts : {
type : "string",
editable : false
}
}
}
}
});
grid = $("#grid-databound-dataItem").kendoGrid({
dataSource : dataSource,
height: 550,
filterable: true,
sortable: true,
pageable: true,
columns : [
{field : "tckNbr", title : "Number", type: "string"},
{field : "tckSts", title : "Status", type: "string"}
]
}).data("kendoGrid");
});
JSON feed:
[
{
"total":100,
"data":
[
{
"tckNbr":"3031",
"tckSts":"1 Not Assigned",
"unid":"0014DA9095BF6D638625810700597A36",
"tckReqs":"Bryan S Schmiedeler",
"tckNts":
[
"Bryan DeBaun"
],
"tckBUs":
[
"NAP\/IFI"
],
"tckApps":"GTM",
"tckType":"Issue",
"tckPriority":"Medium"
},
{
"tckNbr":"3031",
"tckSts":"1 Not Assigned",
"unid":"00598976D88226D2862581070059AD25",
"tckReqs":"Bryan S Schmiedeler",
"tckNts":
[
"Bryan DeBaun"
],
"tckBUs":
[
"NAP\/IFI"
],
"tckApps":"GTM",
"tckType":"Issue",
"tckPriority":"Medium"
}
]
}
]
Correct your JSON feed, you need to return object not array:
{
"total": 10,
"data": []
}
After that say what is data and what is total in you schema:
schema : {
total: "total",
data: "data",
.
.
}
Note: if you mock data like in your case (total: 100, data -> size is 2) your paginatio will be created by total parameter not data itself. You will see 5 pages with same data (that is ok).

Datatables.js issues and questions

I have a action in my controller that returns a list of activities and this was rendering to my datatable without issue, however, I did not see the number of records displayed at the bottom of the table rendering properly. I did some digging and it looks like there are some additional properties required to get this to work. I added those properties in my controller and ajax call and now I see 'Showing 1 of 3 of 3 entries' but no data in my datatable.
So here is my controller action when the datatable displays results but 'Showing 0 of 0 ...' is displayed.
Controller
[HttpPost]
public JsonResult GetAllActivities(int UserId)
{
return Json(repository.GetAllActivities(UserId), JsonRequestBehavior.AllowGet);
}
HTML
<div class="panel-body">
<table id="master" class="table table-striped table-hover table-condensed" cellspacing="0">
<thead>
<tr>
<th class="rpt_col_bg_head" style="width: 3%;"></th>
<th class="rpt_col_bg_head" style="width: 20%;">Result</th>
<th class="rpt_col_bg_detail" style="width: 20%;">Work Activity</th>
<th class="rpt_col_bg_detail" style="width: 180px;">Effort(%)</th>
<th class="rpt_col_bg_detail" style="width: 7%;">Status</th>
<th class="rpt_col_bg_detail" style="width: 30%;">Were there any innovations</th>
<th class="rpt_col_bg_detail text-center" style="width: 7%;">Action</th>
</tr>
</thead>
</table>
</div>
JavaScript
var table = $('#master').DataTable( {
"processing": true,
"serverSide": true,
"columnDefs" : [{
"targets" : [0],
"visible" : true,
"searchable" : false
}],
"ajax" : {
"type" : "POST",
"url" : "#Url.Action("GetAllActivities", "Activities")",
"data" : { "UserId" : employeeId },
"dataSrc" : ""
},
"columns": [
{
"className" : "details-control", "orderable" : false, "data": "ActivityHistoryId" },
{ "data" : "ParentName" },
{ "data" : "ActivityName" },
{ "data" : "Effort" },
{ "data" : "Status" },
{ "data" : "Innovation" },
{ "defaultContent" : '<td><div><button class="btn btn-xs btn-primary" title="edit work activity" name="editWork"></button></div></td>' }]
});
According to the documentation below some additional properties are required to get the footer to work.
https://datatables.net/manual/server-side
I modified my controller action and html accordingly.....
[HttpPost]
public JsonResult GetAllActivities(int UserId)
{
int count = 0;
var data = repository.GetAllActivities(UserId);
foreach (var item in results)
{
count++;
}
return Json(new { draw = 1, recordsTotal = count, recordsFiltered = count, data }, JsonRequestBehavior.AllowGet);
}
HTML
var table = $('#master').DataTable({
"processing": true,
"serverSide": true,
"columnDefs" : [{
"targets" : [0],
"visible" : true,
"searchable" : false
}],
"ajax" : {
"type" : "POST",
"url" : "#Url.Action("GetAllActivities", "Activities")",
"data" : { "UserId" : employeeId },
"dataFilter" : function(data) {
var json = jQuery.parseJSON(data);
json.recordsTotal = json.recordsTotal;
json.recordsFiltered = json.recordsFiltered;
json.data = json.data;
return JSON.stringify(json);
},
"dataSrc" : ""
},
"columns": [
{ "className" : "details-control", "orderable" : false, "data": "ActivityHistoryId" },
{ "data" : "ParentName" },
{ "data" : "ActivityName" },
{ "data" : "Effort" },
{ "data" : "Status" },
{ "data" : "Innovation" },
{ "defaultContent" : '<td><button class="btn btn-xs btn-primary" title="edit work activity" name="editWork"></button></div></td>' }]
});
Here is the JSON returned from my controller, which is valid.
{"draw":1,"recordsTotal":3,"recordsFiltered":3,"data":[{"ActivityHistoryId":1,"UserId":91,"WorkFlowId":4,"ActivityName":"Test Activity 1","ActivityDescription":"Description of Test Activity 1","Status":"\u003cspan class=\u0027badge badge-blue\u0027\u003eNot Started\u003c/span\u003e","Effort":10,"Innovation":false,"ParentId":2,"ParentName":"Test Result 1"},{"ActivityHistoryId":2,"UserId":91,"WorkFlowId":4,"ActivityName":"Test Activity 2","ActivityDescription":"Description of Test Activity 2","Status":"\u003cspan class=\u0027badge badge-blue\u0027\u003eNot Started\u003c/span\u003e","Effort":16,"Innovation":false,"ParentId":2,"ParentName":"Test Result 1"},{"ActivityHistoryId":3,"UserId":91,"WorkFlowId":4,"ActivityName":"Test Activity 3","ActivityDescription":"Description of Test Activity 3","Status":"\u003cspan class=\u0027badge badge-lightBlue\u0027\u003eIn Progress\u003c/span\u003e","Effort":25,"Innovation":false,"ParentId":5,"ParentName":"Test Result 2"}]}
And the datatable...
Any idea how to get BOTH the datatable to render data AND show the number of entries in the table? I find the documentation does not work for every scenario I try.
I used the following to see the data returned...
table.on('xhr', function () {
var json = table.ajax.json();
console.log(table.ajax.json());
});
If you remove dataSrc things should start working:
var table = $('#master').DataTable({
"processing": true,
"serverSide": true,
"columnDefs": [{
"targets": [0],
"visible": true,
"searchable": false
}],
"ajax": {
"type": "POST",
"url": "#Url.Action("
GetAllActivities ", "
Activities ")",
"data": {
"UserId": employeeId
},
"dataFilter": function(data) {
var json = jQuery.parseJSON(data);
json.recordsTotal = json.recordsTotal;
json.recordsFiltered = json.recordsFiltered;
json.data = json.data;
return JSON.stringify(json);
}
},
"columns": [{
"className": "details-control",
"orderable": false,
"data": "ActivityHistoryId"
}, {
"data": "ParentName"
}, {
"data": "ActivityName"
}, {
"data": "Effort"
}, {
"data": "Status"
}, {
"data": "Innovation"
}, {
"defaultContent": '<td><button class="btn btn-xs btn-primary" title="edit work activity" name="editWork"></button></div></td>'
}]
});
Glad that helped. :-)
I don't see problem with the data or server side code - it is the way you are assigning data.
I just assigned data without AJAX and it works see the fiddle https://jsfiddle.net/jituce/1naw041z/6/
var table = $('#master').DataTable( {
"processing": true,
"data":[{"ActivityHistoryId":1,"UserId":91,"WorkFlowId":4,"ActivityName":"Test Activity 1","ActivityDescription":"Description of Test Activity 1","Status":"\u003cspan class=\u0027badge badge-blue\u0027\u003eNot Started\u003c/span\u003e","Effort":10,"Innovation":false,"ParentId":2,"ParentName":"Test Result 1"},{"ActivityHistoryId":2,"UserId":91,"WorkFlowId":4,"ActivityName":"Test Activity 2","ActivityDescription":"Description of Test Activity 2","Status":"\u003cspan class=\u0027badge badge-blue\u0027\u003eNot Started\u003c/span\u003e","Effort":16,"Innovation":false,"ParentId":2,"ParentName":"Test Result 1"},{"ActivityHistoryId":3,"UserId":91,"WorkFlowId":4,"ActivityName":"Test Activity 3","ActivityDescription":"Description of Test Activity 3","Status":"\u003cspan class=\u0027badge badge-lightBlue\u0027\u003eIn Progress\u003c/span\u003e","Effort":25,"Innovation":false,"ParentId":5,"ParentName":"Test Result 2"}],
"serverSide": false,
"columnDefs" : [{
"targets" : [0],
"visible" : true,
"searchable" : false
}],
"columns": [
{
"className" : "details-control", "orderable" : false, "data": "ActivityHistoryId" },
{ "data" : "ParentName" },
{ "data" : "ActivityName" },
{ "data" : "Effort" },
{ "data" : "Status" },
{ "data" : "Innovation" },
{ "defaultContent" : '<td><div><button class="btn btn-xs btn-primary" title="edit work activity" name="editWork"></button></div></td>' }]});
I would say remove JSON.stringy or dataFilter from the AJAX and it should work fine.
Let me know if this helps.

jsTree populated with JSON string

i got this jsTree:
$(function () {
$("#tree").jstree({
"json_data" : {
"data" : [
{
"data" : "<?php echo $db_obj->getValue('article_group_name') ?>",
"metadata" : { id : 23 },
"children" : [ "Child 1", "A Child 2" ]
}
]
},
"plugins" : ["themes","json_data", "ui" ]
});
});
I would like to populate it with DB data. The Childs should be line from the database.
I json_encoded the table data, it looks something like this:
[Object { article_id=
"4949"
, article_name_internal=
"Nachtlampe Lumilove Barbapapa"
}, Object { article_id=
"4947"
, article_name_internal=
"Wanduhr Silk von Koziol"
},
Whene i click one of the childs it should go to that page. Not sure how i can populate the tree with this data. Any instructions?
Each node for jsTree have a list of attributes that you can set to it.
just use the attr property in your JSON and add an array of property-value pairs that represent the data you want.
one of these properties should be an href containing the URL for the page you want to opent once someone clicks the node for your jsTree.
now your server should return the data like this.
{
"data": "Root",
"attr": {
"id": "1",
"rel": "Root",
"type": 0
},
"children": [{
"data": "Test 1",
"attr": {
"id": "2",
"href": "http://www.google.com"
"rel": "OrganizationalUnit",
"type": 1
},
"children": [],
"state": "open"
}],
"state": "open"
}
and your JSTree inint function should do something like that:
k_OrgTree = $("#OrgTree").jstree({
json_data: {
ajax: {
url: "/Administration/PopulateTree",
type: "POST",
dataType: "json",
contentType: "application/json charset=utf-8",
success: function (data) { }
}
},
themes: currentTheme,
types: {
valid_children: [k_Root],
types: {
Root: {
valid_children: [k_OrganizationalUnit, k_Calendar],
icon: { image: "/Content/Administration/Images/Root/Root_32x32.png" },
delete_node: false,
},
OrganizationalUnit: {
valid_children: [k_OrganizationalUnit, k_Calendar, k_User],
icon: { image: "/Content/Administration/Images/OrganizationalUnit/OrganizationalUnit_32x32.png" },
},
Calendar: {
valid_children: ["none"],
icon: { image: "/Content/Administration/Images/Calendar/Calendar_32x32.png" },
create_node: false
},
User: {
valid_children: ["none"],
icon: { image: "/Content/Administration/Images/User/User_32x32.png" },
create_node: false
}
}
},
plugins: ["themes", "json_data", "types", "ui"]
});

Can't show the json Data in Treepanel in extjs 4

I am working on showing JSON data in EXTJS 4 TreePanel. But my tree is not showing any data . Please let me know where I am wrong. Let me post my codes below:
View Part: it has got the treepanel
xtype: 'treepanel',
title: 'Standard Geographies',
height: 250,
width: 310,
store: 'Data',
border: 1,
displayField: 'name',
useArrows: true,
rootVisible: true,
multiSelect: true,
preventHeader: true,
renderTo: Ext.getBody(),
columns: [{
xtype: 'treecolumn',
text: 'Standard Geographies',
flex: 1,
sortable: false,
//renderer : change,
dataIndex: 'name'
}],
Model Part: Using json data
Ext.define('TA.model.TAModel', {
extend: 'Ext.data.Model',
fields: ['name','typeId'],
//fields: ['abbr','type'],['name','typeId']
proxy: {
type: 'ajax',
url : 'data/StandardGeoTree.json',
actionMethods: {
read: 'POST'
},
reader: {
type: 'json',
root: 'geographyOptions'
},
}
});
Store Part: I hope all is ok in the store part
Ext.define('TA.store.Data', {
//extend: 'Ext.data.Store',
//model: 'TA.model.TAModel',
extend: 'Ext.data.TreeStore',
model: 'TA.model.TAModel',
autoSync: true,
autoLoad: true,
listeners: {
load:function(){
console.log('Schemes Data store loaded');
}
},
proxy: {
type: 'ajax',
//url : 'data/StandardGeoTree.json',
api: {
//read: 'data/StandardGeo.json',
read: 'data/StandardGeoTree.json',
//update: 'data/updateTradeArea.json'
},
reader: {
type: 'json',
root: 'root',
successProperty: 'success',
idProperty : 'typeId'
}
},
root : {
text: 'Root',
id: 'typeId',
expanded : true,
loaded : true,
children: []
}
});
JSON
{
"success": true,
"root" : [
{
"name": "001-USA",
"typeId" : "1",
"children":[
{"name": "USA", "typeId" : "1", "leaf":"true"},
{"name": "State", "typeId" : "2", "leaf":"true"},
{"name": "DMA", "typeId" : "3", "leaf":"true"},
{"name": "CSA", "typeId" : "4", "leaf":"true"},
]
}
]
}
the store configuration for tree component is actually doesn't need to be so complicated. for example simple store declaration below is enough:
var treestore = Ext.create('Ext.data.TreeStore', {
proxy: {
type: 'ajax',
url: 'data/StandardGeoTree.json'
}
});
then in your tree configuration set store: treestore and rootVisible: false
finally, the store expect json respond in this format:
[{
"text": "To Do",
"cls": "folder",
"expanded": true,
"children": [{
"text": "Go jogging",
"leaf": true
},{
"text": "Take a nap",
"leaf": true
},{
"text": "Climb Everest",
"leaf": true
}]
}]
In your JSON, your root property has to be similar to your children. So it's either "root" or "children", but not both. See this question for the full explanation.
So given you stick to:
reader: {
type: 'json',
root: 'root',
successProperty: 'success',
idProperty : 'typeId'
}
Your JSON should look like so:
{
"success": true,
"root" : [
{
"name": "001-USA",
"typeId" : "1",
"root":[
{"name": "USA", "typeId" : "1", "leaf":"true"},
{"name": "State", "typeId" : "2", "leaf":"true"},
{"name": "DMA", "typeId" : "3", "leaf":"true"},
{"name": "CSA", "typeId" : "4", "leaf":"true"},
]
}
]
}
Nothing worked as solution. So I had to write the below code part in my view which solved my problem. I am pasting the code part below:
var treeStore = Ext.StoreManager.lookup('Data');
treeStore.load();
But I want my store to be loaded automatically. Please let me know if you have any solution to automatically load the store.