I'm new to jqgrid finally i've setup a grid. Suppose i need to setup jsonReader so that the grid knows where to get my grid-data in the json return. However i got blank cells after trying for days.
Here is my grid:
jQuery("#list48").jqGrid({
url: 'dbtest.aspx/get_offsite_history2',
datatype: "json",
mtype: 'POST',
ajaxGridOptions: { contentType: "application/json" },
serializeGridData: function(postData) {
return JSON.stringify(postData);
},
jsonReader: {
root: function(obj) { alert(JSON.stringify(obj.d)); return obj.d; },
repeatitems: false
},
height: 'auto',
rowNum: 30,
rowList: [10, 20, 30],
colNames: ['name', 'start_date', 'duration', 'offsite_cat'],
colModel: [
{ name: 'name', index: 'name', width: 80, align: 'left', editable: true, edittype: 'text' },
{ name: 'start_date', index: 'start_date', width: 120, align: 'left', editable: true, edittype: 'text' },
{ name: 'duration', index: 'duration', width: 120, align: 'left', editable: true, edittype: 'text' },
{ name: 'offsite_cat', index: 'offsite_cat', width: 120, align: 'left', editable: true, edittype: 'text'}],
pager: "#plist48",
viewrecords: true,
sortname: 'name',
caption: "Grouping Array Data",
gridview: true
});
This is the server return from url dbtest.aspx/get_offsite_history2:
{"d":"[{\"name\":\"A\",\"start_date\":\"B\",\"duration\":\"C\",\"offsite_cat\":\"D\"}]"}
i suppose to get the result by setting "root: 'd'" but i got 64 blank rows for that...
look for comments... many thanks
The reason of your problem is the bug in your server code. You make serialization to JSON twice. After deserializing of d property of the server response you get still JSON string (!!!) instead of object. Typical error is manual usage of JavaScriptSerializer.Serialize in the web method. One should return the object itself instead of the string which is the result of serializing.
Without modifying of your current server code you can fix the problem by usage of
jsonReader: {
root: function (obj) {
alert(typeof obj.d === "string" ? obj.d : JSON.stringify(obj.d));
return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d;
},
repeatitems: false,
page: function () { return 1; },
total: function () { return 1; },
records: function (obj) {
return typeof obj.d === "string" ? $.parseJSON(obj.d).length : obj.length;
}
}
or (if you use loadonce: true) just
jsonReader: {
root: function (obj) {
return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d;
},
repeatitems: false
}
Because your current server code seems not implemented the paging of data you should increase rowNum to some large enough value like rowNum: 10000 or to use loadonce: true.
UPDATED: You can find here modified demo which works. It displays
after the alert message.
I think the problem is the structure of your returned json data.
Below is one that I use :
{ "page":1,
"rows":[{"id":"1","cell":["1","1","Row 1","3","9",""]},
{"id":"2","cell":["2","2","Row 2","2","1",""]},
{"id":"3","cell":["3","4","Row 3","2","0",""]}],
"records":3,
"total":1
}
You may need to add a colModel for id to uniquely identify each row.
e.g.
colNames: ['id', 'name', 'start_date', 'duration', 'offsite_cat'],
colModel: [
{ name: 'id', index: 'id', hidden: true },
{ name: 'name', index: 'name', width: 80, align: 'left', editable: true, edittype: 'text' },
{ name: 'start_date', index: 'start_date', width: 120, align: 'left', editable: true, edittype: 'text' },
{ name: 'duration', index: 'duration', width: 120, align: 'left', editable: true, edittype: 'text' },
{ name: 'offsite_cat', index: 'offsite_cat', width: 120, align: 'left', editable: true, edittype: 'text'}],
Hope that helps.
Related
Hii Guys!!!
I made jqgrid in which i added a FilterToolbar.Now as per my need i need dropdownmenu for unique values in FilterToolbar column.I am using asp.net...
Here is my code...
$(function () {
$("#UsersGrid").jqGrid({
url: 'jqGridHandler.ashx',
datatype: 'json',
height: 250,
colNames: ['UserID', 'UserName', 'FirstName', 'MiddleName', 'LastName', 'EmailID'],
colModel: [
{ name: 'UserID', index: 'UserID', width: 100, sortable: true },
{ name: 'UserName', width: 100, sortable: true },
{ name: 'FirstName', width: 100, sortable: true },
{ name: 'MiddleName', width: 100, sortable: true },
{ name: 'LastName', width: 100, sortable: true },
{ name: 'EmailID', width: 150, sortable: true }
],
rowNum: 10,
rowList: [10, 20, 30],
pager: '#UsersGridPager',
sortname: 'UserID',
viewrecords: true,
sortorder: 'asc',
autowidth:true
//caption: 'JSON Example'
});
$("#UsersGrid").jqGrid('navGrid', '#UsersGridPager', { edit: false, add: false, del: false });
$("#UsersGrid").jqGrid('filterToolbar', { stringResult: true, searchOnEnter:true, defaultSearch: 'cn' });
$("#UsersGrid").jqGrid('navButtonAdd', '#UsersGrid-pager', { caption: "Filter", title: "Toggle Searching Toolbar", buttonicon: 'ui-icon-pin-s', onClickButton: function () { $("#UsersGrid")[0].toggleToolbar(); }
});
Plz guys help me to get the solution..
Thanx in advance....
The easiest way is the usage stype: "select". If you want fill dropdown with unique value from the column you can do this either on the server side or on the client side. If you provide all possible the unique values on the server side in form HTML fragment of <select> you can specify dataUrl option of searchoptions (or editoptions). If you provide the data on the client side you can specify value property of searchoptions (or editoptions) instead. In the answer you will find the example of possible implementation.
I've encountered a problem with jqGrid datagrid.
I need to delete one row or more with a button "Delete".
Here my code:
$grid.navGrid('#pager', { edit: false, add: false, del: false, search: false });
$grid.navButtonAdd('#pager', {
caption: "Delete",
buttonicon: "ui-icon-del",
position: "last",
onClickButton: function() {
alert("Deleting Row");
row_ids = $grid.getGridParam('selarrrow');
$grid.delGridRow(row_ids, {
dataType: 'json',
mtype: 'GET',
url: '<?php echo $_SERVER['PHP_SELF'].'?app=my_app&action=record_delete'; ?>'
});
}
});
This snippet of code send to the "url:" option the id/s of selected row/s (it works).
That url return a response, in json format.. This answer say if the operation was successful or not.
I need to display an alert with that message, how do I do?
And, another problem, when I click on "Delete" button (on bottom of the jqGrid), it displays an ajax windows with the question "Do you want to delete selected items?", with the "Yes" input and the "No" input. When I click "Yes", this ajax window will not close!
Thank you!
Dante
EDIT [1] && [2]
$(document).ready(function()
{
$grid = $("#list");
fixPositionsOfFrozenDivs = function() {
if (typeof this.grid.fbDiv !== "undefined") {
$(this.grid.fbDiv).css($(this.grid.bDiv).position());
}
if (typeof this.grid.fhDiv !== "undefined") {
$(this.grid.fhDiv).css($(this.grid.hDiv).position());
}
};
$grid.jqGrid({
url: '<?php echo $_SERVER['PHP_SELF'].'?app=my_app&option=get_records'; ?>',
datatype: 'json',
mtype: 'GET',
height: 'auto',
width: window.innerWidth-35, //width: '1225',
colNames: ['ID', 'Column A', 'Column B', 'Column C', 'Column D'],
colModel: [
{ name: 'id', index: 'id', width: 130, align: 'center', search: true,
sortable: true, frozen: true, editable: true, edittype: 'text', formatter: 'showlink',
editoptions: { size: 130, maxlength: 50 }, stype: 'text' },
{ name: 'col_a', index: 'col_a', width: 250, align: 'left', search: true,
sortable: true, frozen: false, editable: true, edittype: 'text',
editoptions: { size: 250, maxlength: 40 }, stype: 'text' },
{ name: 'col_b', index: 'col_b', width: 120, align: 'left', search: true,
sortable: true, frozen: false, editable: true, edittype: 'text',
editoptions: { size: 120, maxlength: 40 }, stype: 'text' },
{ name: 'col_c', index: 'col_c', width: 100, align: 'right', search: true,
sortable: true, frozen: false, editable: true, edittype: 'text',
editoptions: { size: 100, maxlength: 40 }, stype: 'text' },
{ name: 'col_d', index: 'col_d', width: 100, align: 'right', search: true,
sortable: true, frozen: false, editable: true, edittype: 'text',
editoptions: { size: 100, maxlength: 6 }, stype: 'text' }
],
caption: 'Objects',
pager: '#pager',
sortname: 'id',
sortorder: 'ASC',
rowNum: 25,
rowList: [25, 50, 100, 200, <?php echo $totrecords; ?>],
loadonce: true,
gridview: true,
viewrecords: true,
rownumbers: true,
rownumWidth: 40,
toppager: true,
multiselect: true,
autoencode: true,
ignoreCase: true,
shrinkToFit: false,
loadComplete: function() {
$("option[value=<?php echo $totrecords; ?>]").text('All');
},
editurl: '<?php echo $_SERVER['PHP_SELF'].'?app=my_app&option=delete_records'; ?>'
});
$grid.jqGrid('filterToolbar', {multipleSearch: false, stringResult: false, searchOnEnter: false, defaultSearch: 'cn'});
$grid.navGrid('#pager', { edit: false, add: false, del: false, search: false });
$grid.navButtonAdd('#pager', {
caption: "Delete",
buttonicon: "ui-icon-del",
position: "last",
onClickButton: function() {
row_ids = $grid.getGridParam('selarrrow');
$grid.delGridRow(row_ids, {
closeOnEscape: true,
mtype: 'POST',
afterSubmit: function(data_from_server, array_data) {
var result = data_from_server.responseText;
var message = eval('(' + result + ')');
alert(message.query);
}
});
}
});
$grid.jqGrid('setFrozenColumns');
$grid[0].p._complete.call($grid[0]);
fixPositionsOfFrozenDivs.call($grid[0]);
});
You should be able to use the afterSubmit event to display your message. From the jqGrid documentation:
afterSubmit
fires after response has been received from server. Typically used to display status from server (e.g., the data is successfully deleted or the delete cancelled for server-side reasons). Receives as parameters the data returned from the request and an array of the posted values of type id=value1,value2.
When used this event should return array with the following items [success, message]
where success is a boolean value if true the process continues, if false a error message appear and all other processing is stopped.
afterSubmit : function(response, postdata)
{
…
return [succes,message]
}
Regarding your second problem, I am not sure why the Ajax window will not close. Have you debugged the code to see if you are receiving a JavaScript error at that time? If not, it may be necessary for you to post a small example demonstrating the problem.
It's not good to use HTTP GET for delete operation. Do you want that the previous server response on the same URL will be cached and not send to the server? Default value of mtype is 'POST'. If you have RESTfull services on the server side you will need to use mtype: 'DELETE', but the usage of 'GET' can have sense only for the some dummy code which don't delete anything on the server.
Moreover you use dataType: 'json' parameter which is not exist for delGridRow (see the documentation). Even if you would use ajaxDelOptions: { datyType: "json" } you will get the server response (see the answer of Justin) not automatically converted from JSON to object. The reason is that the current code of delGridRow use complete callback instead of success callback (see here). So if you get JSON response from the server you will have to call $.parseJSON explicitly inside of afterSubmit callback (see the Justin's answer).
I have the following JQgrid which is working fine.
$("#Groups_UseCases_Grid").jqGrid({
url: 'groupsHandler.ashx?mod=3',
datatype: 'json',
mtype: 'GET',
loadonce: true,
height: 'auto',
width:'750px',
colNames: ['id', 'groupname', 'workSpace'],
colModel: [
{ name: 'id', index: 'id', key:true, width: 200, sortable: false, editoptions: {readonly: true,size: 10}},
{ name: 'groupname', width: 200, sortable: false, editable:false },
{ name: 'workSpace', width: 400, sortable: false, editable:true , edittype:"select",editoptions:{multiple:true, size:"<%=combolistSize%>", value:"<%=combolist%>"} }
],
rowNum: 30,
rowList: [30, 60, 90],
pager: '#pager',
sortname: 'id',
viewrecords: true,
sortorder: 'asc',
caption: 'Groups UseCases Ascosiation Grid',
editurl: 'groupsHandler.ashx?mod=3',
hidegrid: false,
altRows:false,
altclass:'myAltRowClass',
grouping:true,
groupingView : {
groupField : ['useCase'],
groupText : ['<b> useCase : {0} (Records {1}) </b>']
}
});
jQuery("#Groups_UseCases_Grid").jqGrid('navGrid',"#pager",
{edit:false,add:false,del:false});
jQuery("#Groups_UseCases_Grid").jqGrid('inlineNav',"#pager",
{edit:true,add:false,del:false},
{afterSubmit: function(rowid, aData){document.forms[1].submit();//alert("row id is "+rowid)
}},
{closeAfterEdit: true,reloadAfterSubmit: false},
{closeAfterAdd: true,reloadAfterSubmit: false},
{reloadAfterSubmit: true});
C# code :
private string combolist;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
combolist =create_combolist();
}
}
private string create_combolist(/string groupID/)
{.....}
My problem is that i want column workSpace values to be generated by giving the id of the selected row as it depends on it and narrows the dropdown valuelist
Is it possible using the onSelectRow?
I tried
onSelectRow: function(id){<%usecasesample.create_combolist(); <%combolist = usecasesample.create_combolist();%>; } ,
that works too but i cant supply row id in it.
Any help appreciated, thanks in advance,
alex
This seems to work fine
$("#Groups_UseCases_Grid").jqGrid({
url: 'groupsHandler.ashx?mod=2',
datatype: 'json',
mtype: 'GET',
loadonce: true,
height: 'auto',
width:'850px',
colNames: ['groupid', 'groupname', 'workspaces'],
colModel: [
{ name: 'groupid', index: 'id', key:true, width: 200, sortable: false, editoptions: {readonly: true,size: 10}},
{ name: 'groupname',index: 'groupname', width: 200, sortable: false, editable:false },
{ name: 'workspaces', width: 400, sortable: false, editable:true , edittype:"select",editoptions:{multiple:true, size:"" }}
],
onSelectRow: function(id) {
var name='workspaces';
jQuery("#Groups_UseCases_Grid").setColProp(name, { editoptions: {multiple:true, dataUrl: 'groupsHandler.ashx?mod=3&rowID='+id }})
},
rowNum: 30,
cellEdit:false,
rowList: [30, 60, 90],
pager: '#pager',
sortname: 'id',
viewrecords: true,
sortorder: 'asc',
caption: 'Groups UseCases Ascosiation Grid',
editurl: 'groupsHandler.ashx?mod=2&rows=20&page=1',
hidegrid: false,
altRows:false,
altclass:'myAltRowClass',
grouping:true,
groupingView : {
groupField : ['useCase'],
groupText : [' useCase : {0} (Records {1}) ']
}
});
jQuery("#Groups_UseCases_Grid").jqGrid('navGrid',"#pager",
{edit:false,add:false,del:false});
jQuery("#Groups_UseCases_Grid").jqGrid('inlineNav',"#pager",
{edit:true,add:false,del:false},
{afterSubmit: function(rowid, aData){document.forms[1].submit();//alert("row id is "+rowid)
}},
{closeAfterEdit: true,reloadAfterSubmit: false},
{closeAfterAdd: true,reloadAfterSubmit: false},
{reloadAfterSubmit: true});
I have
jQuery(document).ready(function () {
jQuery("#list").jqGrid({
url: '/TabMaster/GetGridData',
datatype: 'json',
mtype: 'GET',
colNames: ['col ID', 'First Name', 'Last Name', '', '', '', ''],
colModel: [
{ name: 'colID', index: 'colID', width: 100, align: 'left' },
{ name: 'FirstName', index: 'FirstName', width: 150, align: 'left' },
{ name: 'LastName', index: 'LastName', width: 300, align: 'left' },
{ name: 'add', width: 18, sortable: false, search: false,
formatter: function () {
return "<span class='ui-icon ui-icon-plus'></span>"
}
},
{ name: 'edit', width: 18, sortable: false, search: false,
formatter: function () {
return "<span class='ui-icon ui-icon-pencil'></span>"
}
},
{ name: 'del', width: 18, sortable: false, search: false,
formatter: function () {
return "<span class='ui-icon ui-icon-trash'></span>"
}
},
{ name: 'details', width: 18, sortable: false, search: false,
formatter: function () {
return "<span class='ui-icon ui-icon-document'></span>"
}
}
],
onSelectRow: function (rowJsonId)
{ handleSelectedRow(rowJsonId); },
pager: jQuery('#pager'),
rowNum: 4,
rowList: [1, 2, 4, 5, 10],
sortname: 'colID',
sortorder: "asc",
viewrecords: true,
gridview: true,
rownumbers: true,
multiselect: true,
imgpath: '/scripts/themes/steel/images',
caption: 'Tab Master Information'
}).navGrid(pager, { edit: true, add: true, del: true, refresh: true, search: true });
});
function handleSelectedRow(thing) {
}
but i could not able to see buttons for Add, Edit, Delete and View i am only able to see for empty column in JQGrid.
for your reference
Please provide your valuable comments for the same.
It seems to me that you try to use my demo from the answer:
All classes for the ui-icon-plus, ui-icon-trash and so on come from jQuery UI CSS. You shoul just verify that you included jQuery UI correctly together with the images subdirectory.
I'm trying to implement a simple grid using jqGrid, my first read example of my own.
Here is my JSON code:
$(document).ready(function() {
$("#editgrid").jqGrid({
url: '<%= ResolveUrl("~/User/index") %>',
datatype: "json",
myType: 'GET',
colNames: ['UserId', 'Surname', 'Forename', 'Pax', 'Mobile', 'Active', 'Email'],
colModel: [
{ name: 'UserId', index: 'UserId', width: 80, editable: true, editoptions: { size: 10} },
{ name: 'Surname', index: 'Surname', width: 90, editable: true, editoptions: { size: 25} },
{ name: 'Forename', index: 'Forename', width: 60, align: "right", editable: true, editoptions: { size: 10} },
{ name: 'Pax', index: 'Pax', width: 60, align: "right", editable: true, editoptions: { size: 10} },
{ name: 'Mobile', index: 'Mobile', width: 60, align: "right", editable: true, editoptions: { size: 10} },
{ name: 'Active', index: 'Active', width: 55, align: 'center', editable: true, edittype: "checkbox", editoptions: { value: "Yes:No"} },
{ name: 'Email', index: 'Email', width: 100, sortable: false, editable: true, edittype: "textarea", editoptions: { rows: "2", cols: "20"} }
],
rowNum: 10,
rowList: [10, 20, 30],
pager: '#pagered',
sortname: 'Surname',
viewrecords: true,
sortorder: "desc",
caption: "Editing Example",
editurl: "detail.aspx"
});
$("#bedata").click(function() {
var gr = jQuery("#editgrid").jqGrid('getGridParam', 'selrow');
if (gr != null) jQuery("#editgrid").jqGrid('editGridRow', gr, { height: 280, reloadAfterSubmit: false });
else alert("Please Select Row");
});
});
I have the following HTML :
<table id="editgrid"></table>
<div id="pagered"></div>
<input type="button" id="bedata" value="Edit Selected" />
But when I run all this I get client code breaking within jquery-1.5.2.js (Microsoft JScript runtime error: Object doesn't support this property or method) where I can do nothing about this, this is probably of no use but here is the error below, stops at the finally part :
// resolve with given context and args
resolveWith: function( context, args ) {
if ( !cancelled && !fired && !firing ) {
// make sure args are available (#8421)
args = args || [];
firing = 1;
try {
while( callbacks[ 0 ] ) {
callbacks.shift().apply( context, args );
}
}
finally {
fired = [ context, args ];
firing = 0;
}
}
return this;
},
Must be something simple making this crash out on me, any ideas anyone?