jqgrid won't load json data - json

I am new to jqgrid and am trying to use json to load the data. I have tried to use the jqgrid demos as a base and then build from there. The json data looks good, but I can't get anything loaded into the grid. Any ideas? I was hoping the use of loaderror or loadcomplete would at least give me insight, but I am not able to retrieve any message why the grid won't load.
json data:
{
"page": "1",
"total": 1,
"records": "12",
"rows": [
[
"67",
"3 - Sandbox: Appointment Set"
],
[
"68",
"1 - Sandbox: Email requested"
],
[
"69",
"2 - Sandbox: Questions not answered"
],
[
"74",
"1 - TenPointSix: Email requested for more information"
],
[
"75",
"2 - TenPointSix: Registered for webinar2"
],
[
"76",
"3 - TenPointSix: Webinar registration confirmed"
],
[
"93",
"5-Test Entry"
],
[
"94",
"test3"
],
[
"95",
"test2"
],
[
"97",
"Jeff"
],
[
"103",
"sortorder"
],
[
"106",
"reload"
]
]
}
My grid code:
<table id="jsonmap"></table>
<div id="pjmap"></div>
<script language="JavaScript" type="text/javascript">
jQuery("#jsonmap").jqGrid({
url:'sampleLoad.php?client=<?=$clientId5?>',
datatype: "json",
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
colNames:['Inv No','Name'],
colModel:[
{name:'id',index:'id', width:55},
{name:'name',index:'name', width:100}
],
rowNum:15,
rowList:[15,30,45],
pager: '#pjmap',
sortname: 'id',
viewrecords: true,
sortorder: "asc",
jsonReader: {
root: "Rows",
cell: "",
page: "Page",
total: "Total",
records: "Records",
repeatitems: false,
id: "0"
},
loadComplete: function() {
alert("Load Complete");
},
loadError: function(xhr,st,err) { $("#jsonmapMessage").html("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText); },
caption: "JSON Mapping",
width: '900',
height: '300'
});
jQuery("#jsonmap").jqGrid('navGrid','#pjmap',{edit:true,add:false,del:false});
Any help would be appreciated.
Thanks,
Jeff

The problem is the wrong jsonReader which you use. For example you use rows in the JSON data, but use root: "Rows". The format of the data corresponds default repeatitems: true property, but you used repeatitems: false and so on.
The correct jsonReader is
jsonReader: {
cell: "",
id: "0"
}
Additionally I would recommend you to add gridview: true and use height: 'auto' instead of height: '300' which simplify the setting of height.
The demo shows the modifications.

Related

Populate Custom Caption from Json

How can i populate caption of the table with the hospitallName & Date supplied in the json data?
** expected output is added here **
Json :
{
"sEcho":2,
"iTotalRecords":243,
"iTotalDisplayRecords":243,
"aaData":[
[
"Mark Zuckerberg",
"USA",
"Feb 14, 1943"
],
[
"John Mathew",
"UK",
"Feb 14, 1943"
],
[
"John Mathew",
"USA",
"Feb 14, 1943"
]
],
"Date":"Aug 01, 2016 - Aug 05, 2016",
"hospitallName":"Apollo Hospital Center"
}
--
on my server side,i am calling like this,
$(document).ready(function() {
oTable = $('#report').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bServerSide": true,
"bProcessing":true,
"bFilter":true,
"oLanguage": {
"sProcessing": "Please wait..."
},
"sAjaxSource": "myDataTableAction.action",
"aaSorting": [],
});
$('td.options span').hide();
$('<caption/>').html('<h2 style="text-align: center;background-color:#f9b660 !important ; line-height: 45px;">Have to show hosital Name & Date here</h2>').appendTo( '#report' );
});
You can use ajax instead of sAjaxSource and use the dataSrc callback to populate the headers. You are not giving any example of layout or markup, but if you give the relevant headers an id you can do the following :
ajax: {
url: 'myDataTableAction.action',
dataSrc: function(json) {
$('#date_head').text(json.Date)
$('#hospitalName_head').text(json.hospitallName)
return json.aaData
}
}
see demo -> http://jsfiddle.net/zvqdpeky/

Bootstrap table using JSON loading empty values

I'm using the bootstrap table plugin and can't seem to get it to load any values into the table. I've used it before with success, but can't figure out what I'm doing wrong here. The amount of rows is coming through, so I know the JSON data is being read by the bootstrap table, its just that the values are all empty in the table. I think the JSON is in the proper format, and the field names are all consistent. Any ideas?
Javacript
function populate_quote_table()
{
$('#quote_table').bootstrapTable({
onSearch: function (text) {
},
onLoadSuccess: function (data) {
console.log(data);
//$('#test').html(data);
},
url: "<?php echo $site_url?>/quotes.php",
striped: true,
search: true,
showRefresh:true,
showColumns:true,
pagination: true,
showFilter: true,
columns: [{
field: 'QID',
title: 'QID',
}, {
field: 'CID',
title: 'Customer ID',
}, {
field: 'Distance',
title: 'Distance',
}]
});
};
});
PHP
$i = 0;
$quotes[] = array();
if ($result) {
while($row = mysql_fetch_array($result)) {
$quotes[$i]['"QID"'] = $row['quote_id'];
$quotes[$i]['"CID"'] = $row['customer_id'];
$quotes[$i]['"Distance"'] = $row['distance'];
$i++;
}
}
echo json_encode($quotes);
JSON Output
[
{
"QID": "1",
"CID": "1",
"Distance": "1"
},
{
"QID": "2",
"CID": "2",
"Distance": "11"
},
{
"QID": "3",
"CID": "20",
"Distance": "5"
},
{
"QID": "4",
"CID": "21",
"Distance": "67"
}
]
Extra quotes were not needed
Original -
$quotes[$i]['"QID"'] = $row['quote_id'];
Working -
$quotes[$i]['QID'] = $row['quote_id'];

extjs4 jsonStore root

Help anyone, I need to use JSON data in my store. Can someone point what is wrong in my jsonstore config? I need to use the rain_value and air_pressure parameter in my graph but 'data' in JSON wont be set as root.
My device model:
Ext.define('device', {
extend: 'Ext.data.Model',
config: {
fields : [
{
name : 'dev_id',
type : 'int'
},
{
name : 'location'
}
],
hasMany : [
{
model : 'stationData',
name : 'data',
associationKey : 'data'
},
]
}
});
My deviceData model:
Ext.define('stationData', {
extend : 'Ext.data.Model',
config : {
fields : [
'dateTimeRead',
'rain_value',
'air_pressure'
]
}});
My jsonStore:
var store = new Ext.data.Store({
autoLoad : true,
model : 'device',
proxy : {
type : 'ajax',
url : 'dataURL',
reader: {
type: 'json',
root: 'data'
},
}
});
JSON Data:
[{
"dev_id": 171,
"location": "Sample location",
"province": "Sample Province",
"cell_num": "0123456789",
"posx": "longitude",
"posy": "latitude",
"elevation": "105",
"battery": "LP",
"region": "VI",
"type_id": "AWS",
"imei_num": "300234011463010",
"is_ftp": false,
"data": [{
"dateTimeRead": "2013-11-14 11:45:32",
"rain_value": "0.52",
"rain_intensity": "19.3",
"air_pressure": "1002.02",
"wind_speed": "7.9",
"air_humidity": "76.7",
"solar_radiation": "-305363.70",
"wind_direction": "327",
"air_temperature": "29.2",
"rain_cum": "238.07",
"soil_moisture1": "8.88",
"soil_temperature1": "28.7",
"soil_moisture2": "6.37",
"soil_temperature2": "27.6",
"rain_duration": "180",
"wind_speed_max": "14.1",
"sunshine_count": "0",
"sunshine_cum": "19286",
"wind_direction_max": "18"
}, {
"dateTimeRead": "2013-11-14 11:30:32",
"rain_value": "0.00",
"rain_intensity": "0.0",
"air_pressure": "1002.02",
"wind_speed": "8.9",
"air_humidity": "68.6",
"solar_radiation": "-239488.50",
"wind_direction": "322",
"air_temperature": "30.2",
"rain_cum": "237.55",
"soil_moisture1": "8.91",
"soil_temperature1": "28.5",
"soil_moisture2": "6.72",
"soil_temperature2": "27.6",
"rain_duration": "0",
"wind_speed_max": "15.1",
"sunshine_count": "455",
"sunshine_cum": "19286",
"wind_direction_max": "10"
}, {
"dateTimeRead": "2013-11-14 11:15:32",
"rain_value": "0.00",
"rain_intensity": "0.0",
"air_pressure": "1002.02",
"wind_speed": "10.2",
"air_humidity": "67.8",
"solar_radiation": "251642.70",
"wind_direction": "333",
"air_temperature": "31.0",
"rain_cum": "237.55",
"soil_moisture1": "8.90",
"soil_temperature1": "28.4",
"soil_moisture2": "7.01",
"soil_temperature2": "27.6",
"rain_duration": "0",
"wind_speed_max": "18.3",
"sunshine_count": "900",
"sunshine_cum": "18831",
"wind_direction_max": "9"
}]
}]

jqgrid datetime column sorting in 24 hour format

I am using a json object as follows from spring MVC to populate the jqgrid by using a json reader.
{
"rows": [
{
"id": 1,
"startTime": "Mar 4 08:00",
"projectId": "Proj_001",
"customerId": "Cust1",
"divisionId": "Div1"
},
{
"id": 2,
"startTime": "Mar 5 15:00",
"projectId": "Proj_001",
"customerId": "Cust1",
"divisionId": "Div1"
},
{
"id": 3,
"startTime": "Apr 14 22:00",
"projectId": "Proj_001",
"customerId": "Cust1",
"divisionId": "Div1"
}
]
}
I need to sort the
startTime
column which comes in 24hour time format (as above in the json object). i tried do some workaround but still unable to succeed.
jqgrid code -
{name:'startTime', index:'startTime', width:90,align:"left",sorttype:'datetime',datefmt:'M d, Y at h:i',sortable: true,editrules:{date:true} },
i will not using AM/PM notation here. i saw in jqgrid.base.js it uses AM/PM notation to sort. can anyone please shed some light on this? i was unable to find any jqgrid example for using date with 24hour time format that will do sort. Thank you in advance.
my jqgrid code -
jq(function(){
jq("#testgrid").jqGrid({
url:"/getall.html",
datatype: 'json',
mtype: 'GET',
colNames:['Id','Start Time','Cust.Id','Div.Id','Proj.Id'],
colModel :[
{name:'id', index:'id', width:90,align:"left",hidden: true},
{name:'startTime', index:'startTime', width:90, align:"left",
sorttype:'datetime', datefmt:'M d h:i',sortable: true,
editrules:{date:true}},
{name:'divisionId', index:'divisionId', width:100,align:"left"},
{name:'departmentId', index:'departmentId', width:120,align:"left"},
{name:'projectId', index:'projectId', width:100,align:"left"},
],
pager: '#testpager',
loadonce: true,
sortname: 'projectId',
sortorder: 'asc',
viewrecords: true,
multiselect: true,
//to hide pager buttons
pgbuttons:false,
recordtext:'',
pgtext:'',
gridview: true,
caption: 'my JQGRID',
jsonReader : {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
cell: "cell",
id: "id"
}
});
//toolbar search
//jq("#testgrid").jqGrid('filterToolbar',
// {stringResult:true,searchOnEnter:false});
});
As I try to reproduce your problem all seems working correct. See the demo live here. If you will sort by the "Start Time" column all work without any problem which I could see.

Unable to populate JqGrid footer with userdata information

I am having a tough time to display footer information using jqGrid. I have followed all the necessary steps to setup footer. The server request is based on some filter condition. The server returns a json with appropriate "userdata" information. Following are the JSON and javascript information.
{
"timeatt": [
{
"empnum" : "12345",
"name" : "ABCDEFG",
"shift" : "1",
"postdate" : "12/27/10",
"regular" : "40",
"ot" : "8",
"dbltime" : "0",
"holiday" : "0",
"vacation" : "0",
"payrate" : "0"
},
{
"empnum" : "67890",
"name" : "HIJKLMN",
"shift" : "1",
"postdate" : "12/27/10",
"regular" : "32",
"ot" : "0",
"dbltime" : "0",
"holiday" : "0",
"vacation" : "8",
"payrate" : "0"
}
],
"userdata": {
"name": "Totals",
"regular": "72",
"ot": "8",
"dbltime": "0",
"vacation": "8",
"holiday": "0"
},
"totalrecords" : "2"
}
jQGrid Information
$("#empinfo").jqGrid({
datatype:'json',
colNames:['Clock#','Name','PostDate','Shift','Regular','Over Time','Dbl Time',
'Vacation','Holiday'],
colModel:[{name:'empnum', index:'empNum', width:60},
{name:'name', index:'name', width:200},
{name:'postdate', index:'postdate', width:60,editable:false,
hidden:true,editrules:{edithidden:false}},
{name:'shift', index:'shift', width:60,editable:true,edittype:'text'},
{name:'regular', index:'regular', width:70,editable:true,
edittype:'text',align:"right", formatter: 'number'},
{name:'ot', index:'ot', width:70,editable:true,edittype:'text',
align:"right", formatter: 'number'},
{name:'dbltime', index:'dltime', width:70,editable:true,
edittype:'text',align:"right", formatter: 'number'},
{name:'vacation', index:'vacation', width:70,editable:true,
edittype:'text',align:"right", formatter: 'number'},
{name:'holiday', index:'holiday', width:70,editable:true,
edittype:'text',align:"right", formatter: 'number'}],
scroll:1,
scrollRows:true,
height:300,
editurl:'clientArray',
footerrow:true,
userDataOnFooter:true,
altRows:true,
onSelectRow: function(rowNum){
if (rowNum && rowNum != lastSel) {
$("#empinfo").saveRow(lastSel);
}
$("#empinfo").editRow(rowNum,true);
lastSel = rowNum;
},
});
I tried everything listed in the jqGrid demo but for somereason, the "userdata" information is not getting populated. Can anybody help me ?
Thanks
SMargabandhu
First of all you should remove comma before }); at the end of your code. Your code is sure not full. For example url parameter is not defined so I suppose the comma come from the reduction of real code.
About your main problem. You should include jsonReader in the jqGrid definition to be able to display the JSON data which you posted. The jsonReader can be following:
jsonReader: {
repeatitems: false,
root:'timeatt',
page: function (obj) { return 1; },
total: function (obj) { return 1; },
records: function (obj) { return obj.length; }
}
After that the data will be displayed inclusive the 'userdata' will be displayed (see here)
the same name of the columns in (colModel) to return userdata, example:
userdata = new { hours= lista.Sum(x => x.Horas).ToString(), function= "Total HH:" }