JQgrid Web Api Json on a SPA - jqgrid

I just found JQGrid , and think that JQgrid is very mature and perfect to use with Web API
I have a few questions:
we are using Web Api and have the following HTML code and Controller.
The Image field loses its data when It comes back to the controller. Google debugger shows data for the image field on the HTML, but if I set a breakpoint on the Put method inside the controller, Visual Studio , I can see the product entity with all its fields filled with data, all but one; the image field is null.
It took me some digging to get this working , therefore I decided to share the HTML code + the WebApi controller it with the community.
1- am I am doing this right (Grid parameters) ?
2-Why the image fields comes back null ?
3-How can I implement a search ?
this will not get called
public dynamic GetProducts4JqGrid1(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper, string searchString)
4-why i Must pass the Entity ID to the controller ? I tried many other ways and it always come back as "0" so must pass the Id to the Put method , is this the only way ?
JQGrid is awesome with Web Api
nice to have everything under one roof
http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jqgriddocs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using RoboMVC.Contracts;
using RoboMVC.Model;
namespace RoboMVC.Web.Controllers.jQgrid
{
public class ProductsJQgridController : ApiControllerBase
{
public ProductsJQgridController(IRoboMVCUow uow)
{
Uow = uow;
}
public dynamic GetProducts4JqGrid(string sidx, string sord, int page, int rows)
{
//var products = Uow.Product.GetAllProducts().OrderBy(e => e.ProductId) as IEnumerable<Product>;
var pageIndex = Convert.ToInt32(page) - 1;
var pageSize = rows;
var totalRecords = Uow.Product.Count();
var totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
var products = Uow.Product.GetAllProducts().OrderBy(e => e.ProductId).Skip(pageIndex * pageSize).Take(pageSize) as IEnumerable<Product>;
return new
{
total = totalPages,
page = page,
records = totalRecords,
rows =
(
from product in products
select new
{
id = product.ProductId,
cell = new object[]
{
product.ProductId,
product.Description,
product.SalePrice,
product.SaleCommissionFactor,
product.Stock,
product.StockMax,
product.StockMin,
product.DateOfEntry,
product.LastDateOfSale,
product.SKU,
product.LastCostPaid,
product.Weight,
product.Image,
product.Categoy,
}
}).ToList()
};
}
public HttpResponseMessage Put( int id, Product product)
{
product.ProductId = Convert.ToInt32(id);
Uow.Product.Update(product);
Uow.Commit();
var result = new HttpResponseMessage(HttpStatusCode.NoContent);
return result;
}
public HttpResponseMessage Post(int id, Product product)
{
product.ProductId = Convert.ToInt32(id);
product = Uow.Product.Add(product);
Uow.Commit();
var response = Request.CreateResponse<Product>(HttpStatusCode.Created, product);
string uri = Url.Route(null, new { id = product.ProductId });
response.Headers.Location = new Uri(Request.RequestUri, uri);
return response;
}
public HttpResponseMessage Delete(int id)
{
Uow.Product.Delete(id);
Uow.Commit();
var result = new HttpResponseMessage(HttpStatusCode.NoContent);
return result;
}
}
}
<script>
var API_URL = "/api/ProductsJQgrid/";
jQuery("#gridMain").jqGrid({
//http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options
url: "/api/ProductsJQgrid/GetProducts4JqGrid/",
datatype: 'json',
mtype: 'GET',
pager: '#pagernav',
viewrecords: true,
height: '60%',
width: '100%',
hoverrows: true,
rownumbers: true,
shrinkToFit: true,
gridview: true,
search: true,
ignoreCase:true,
loadonce: false,
viewsortcols: [false, "horizontal", true],
imgpath: '../../Content/jquery.jqGrid/content/images/',
rowNum: 10,
cache: true,
rowList: [10, 20, 30],
sortable: true,
sortname: 'productId',
sortorder: "desc",
// sorttype: "text",
caption: "CRUD With ASP.NET Web API",
autowidth: true,
colNames: ['productId', 'Description', 'SalePrice', 'SaleCommissionFactor', 'Stock', 'StockMax', 'StockMin', 'DateOfEntry', 'LastDateOfSale', 'SKU', 'LastCostPaid', 'Weight', '<image src=images/arrow-right.gif width=16 height=16>', 'Categoy'],
colModel: [
//http://www.trirand.com/jqgridwiki/doku.php?id=wiki:colmodel_options
{ key:true, name: 'productId', index: 'productId', width: 40, search : true , sorttype: "int", editable: false },
{ name: 'description', index: 'description', editable: true, edittype: 'text', width: 70 },
{ name: 'salePrice', index: 'salePrice', editable: true, edittype: 'text', width: 50, align: "right", sorttype: "float", formatter: "number" },
{ name: 'saleCommissionFactor', index: 'saleCommissionFactor', editable: true, edittype: 'text', width: 50, align: "right", sorttype: "float", formatter: "number" },
{ name: 'stock', index: 'stock', editable: true, edittype: 'text', width: 50, align: "right", sorttype: "int" },
{ name: 'stockMax', index: 'stockMax', editable: true, cellEdit:true, edittype: 'text', width: 50, align: "right", sorttype: "int"},
{ name: 'stockMin', index: 'stockMin', editable: true, edittype: 'text', width: 50, align: "right", sorttype: "int"},
{ name: 'dateOfEntry', index: 'dateOfEntry', editable: true, edittype: 'text', width: 50, align: "right", sorttype: "date", formatter: "date" },
{ name: 'lastDateOfSale', index: 'lastDateOfSale', editable: true, edittype: 'text', width: 50, align: "right", sorttype: "date", formatter: "date" },
{ name: 'sKU', index: 'sKU', editable: true, edittype: 'text', width: 70 },
{ name: 'lastCostPaid', index: 'lastCostPaid', editable: true, edittype: 'text', width: 50, align: "right", sorttype: "float", formatter: "number" },
{ name: 'weight', index: 'weight', editable: true, edittype: 'text', width: 70 },
{ name: 'image', index: 'image', editable: false, search:false, width: 50, align: "right", },
{ name: 'categoy', index: 'categoy', editable: true, edittype: 'text', width: 70 }
],
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: true,
id: "id", // from the controller returned value
cell: "cell",
search: "_search"
},
subGrid: true
});
function updateDialog(action) {
return {
url: "/api/ProductsJQgrid/"
, closeAfterAdd: true
, closeAfterEdit: true
, afterShowForm: function (formId) { }
, modal: true
, onclickSubmit: function (params) {
var list = $("#gridMain");
var selectedRow = list.getGridParam("selrow");
var rowData = list.getRowData(selectedRow);
params.mtype = action;
params.url += rowData.productId;
debugger;
// image has the right data here
}
, width: "600"
};
}
jQuery("#gridMain")
.jqGrid('navGrid', '#pagernav',
{ add: true, edit: true, del: true, search: true,refresh: true },
updateDialog('Put'),
updateDialog('Post'),
updateDialog('Delete'),
{ sopt: ["cn"] } // Search options. Some options can be set on column level
)
// .filterToolbar({ autoSearch: false, searchOnEnter: false, stringResult: true })
;
$(window).bind('resize', function () {
jQuery("#gridMain").setGridWidth($('#parentDiv').width() - 30, true);
}).trigger('resize');
</script>

Related

How to save/restore the CheckBox state in jqGrid across webMethod?

I am using jqGrid with "multiselect: true" and webMethods. I need to persist state, so I am going to put the Grid state in the DB, in order to do this I need to know which rows have selected checkboxes and pass that through the webMethod, then on the other side I need to be able to specify to the Grid to select or unselect a particular checkbox.
This is my current binding code, serializeGridData doesn't pick up the checkbox state.
$(document).ready(function () {
jQuery("#selectedInmateList").jqGrid({
url: "<%= AdminPath %>WebMethods/WebService1.asmx/GetUsersJSON3",
postData: {
inmateList: function () {
return InmateListArg;
}
},
mtype: 'POST',
datatype: 'json',
ajaxGridOptions: { contentType: "application/json" },
serializeGridData: function (postData) {
var propertyName, propertyValue, dataToSend = {};
for (propertyName in postData) {
if (postData.hasOwnProperty(propertyName)) {
propertyValue = postData[propertyName];
if ($.isFunction(propertyValue)) {
dataToSend[propertyName] = propertyValue();
} else {
dataToSend[propertyName] = propertyValue
}
}
}
return JSON.stringify(dataToSend);
},
onSelectRow: function (id) {
},
jsonReader: {
root: "d.rows",
page: "d.page",
total: "d.total",
records: "d.records"
},
colNames: ['select', 'LastName', 'FirstName', 'id'],
colModel: [
{ name: 'select', index: 'select', width: 300, align: 'center' },
{ name: 'LastName', index: 'LastName', width: 300, align: 'center' },
{ name: 'FirstName', index: 'FirstName', width: 300, align: 'center' },
{ name: 'id', index: 'id', align: 'center', hidden: true }
],
pager: '#prod_pager',
rowList: [10, 20, 30],
sortname: 'Code',
sortorder: 'desc',
rowNum: 10,
loadtext: "Loading....",
shrinkToFit: false,
multiselect: true,
emptyrecords: "No records to view",
//width: x - 40,
height: "auto",
rownumbers: true,
//subGrid: true,
caption: 'Selected Inmates'
});
});
If I understand your correctly you need first of all to send the current list of selected rows to the server. For example if the user select or unselect new row you can send the current list of the rows directly to the server. You can do this inside of onSelectRow and onSelectAll callbacks. Additionally you need that the server send you back only the data of the current page (the full data if you use loadonce: true option), but the list of ids of the rows which need be selected too. Inside of loadComplete you can call in the loop setSelection method to select the rows.
I would recommend you to examine the code of the callback onSelectRow, onSelectAll and loadComplete of the demo created for the answer. The old answer provide the basis of the same idea.
To pass the selected row IDs into the webMethod I used this:
var selectedIDs = jQuery('#selectedInmateList').jqGrid('getGridParam', 'selarrrow');
Then I added that to my webMethod param 'InmateListArg'
Then I added a hidden column which indicated if the row should be checked or not, then I used the loadComplete event to select the desired row based on the data in the hidden column.
$(document).ready(function () {
jQuery("#selectedInmateList").jqGrid({
url: "<%= AdminPath %>WebMethods/WebService1.asmx/GetUsersJSON3",
postData: {
inmateList: function () {
return InmateListArg;
}
},
mtype: 'POST',
datatype: 'json',
ajaxGridOptions: { contentType: "application/json" },
serializeGridData: function (postData) {
var propertyName, propertyValue, dataToSend = {};
for (propertyName in postData) {
if (postData.hasOwnProperty(propertyName)) {
propertyValue = postData[propertyName];
if ($.isFunction(propertyValue)) {
dataToSend[propertyName] = propertyValue();
} else {
dataToSend[propertyName] = propertyValue
}
}
}
return JSON.stringify(dataToSend);
},
onSelectRow: function (id) {
},
loadComplete: function (id) {
idsOfSelectedRows = [];
var gridData = jQuery("#selectedInmateList").getRowData();
for (i = 0; i < gridData.length; i++) {
var isChecked = gridData[i]['checked'];
var id = gridData[i]['id'];
if (isChecked == 'True') {
idsOfSelectedRows.push(id);
}
}
var $this = $(this), i, count;
for (i = 0, count = idsOfSelectedRows.length; i < count; i++) {
$this.jqGrid('setSelection', idsOfSelectedRows[i], false);
}
},
jsonReader: {
root: "d.rows",
page: "d.page",
total: "d.total",
records: "d.records"
},
colNames: ['select', 'LastName', 'FirstName', 'id', 'checked'],
colModel: [
{ name: 'select', index: 'select', width: 300, align: 'center' },
{ name: 'LastName', index: 'LastName', width: 300, align: 'center' },
{ name: 'FirstName', index: 'FirstName', width: 300, align: 'center' },
{ name: 'id', index: 'id', align: 'center' /*, hidden: true*/ },
{ name: 'checked', index: 'checked', align: 'center' }
],
pager: '#prod_pager',
rowList: [10, 20, 30],
sortname: 'Code',
sortorder: 'desc',
rowNum: 10,
loadtext: "Loading....",
shrinkToFit: false,
multiselect: true,
emptyrecords: "No records to view",
//width: x - 40,
height: "auto",
rownumbers: true,
//subGrid: true,
caption: 'Selected Inmates'
});
jQuery("#prodgrid").jqGrid('navGrid', '#prod_pager',
{ edit: false, add: false, del: true, excel: true, search: false });
});

how to set the jqgrid column width, and re-sizable, in a scrollable jqgrid?

i am using jqgrid and facing a problem with column width
here is my js code
jQuery(document).ready(function () {
var grid = jQuery("#grid");
grid.jqGrid({
url: '/Admin/GetUserForJQGrid',
datatype: 'json',
mtype: 'Post',
cellsubmit: 'remote',
cellurl: '/Admin/GridSave',
//formatCell: emptyText,
colNames: ['Id', 'Privileges', 'First Name', 'Last Name', 'User Name', 'Password', 'Password Expiry', 'Type', 'Last Modified', 'Last Modified By', 'Created By', ''],
colModel: [
{ name: 'Id', index: 'Id', key: true, hidden: true, editrules: { edithidden: true } },
{ name: 'Privileges', index: 'Privileges', width: "350", resizable: false, editable: false, align: 'center', formatter: formatLink, classes: 'not-editable-cell' },
{ name: 'FirstName', index: 'FirstName', width:350, align: "left", sorttype: 'text', resizable: true, editable: true, editrules: { required: true } },
{ name: 'LastName', index: 'LastName',width:350, align: "left", sorttype: 'text', resizable: true, editable: true, editrules: { required: true } },
{ name: 'UserName', index: 'UserName', width:300,align: "left", sorttype: 'text', resizable: true, editable: true, editrules: { required: true } },
{ name: 'Password', index: 'Password',width:400, align: "left", sorttype: 'text', resizable: true, editable: false, editrules: { required: true }, classes: 'not-editable-cell' },
{
name: 'Password_Expiry',width:250, index: 'Password_Expiry', align: "left", resizable: true, editable: true, editoptions: {
size: 20, dataInit: function (el) {
jQuery(el).datepicker({
dateFormat: 'yy-mm-dd', onSelect: function (dateText, inst) {
jQuery('input.hasDatepicker').removeClass("hasDatepicker")
return true;
}
});
}
}
},
{
name: 'Type', width: "250", index: 'Type', sorttype: 'text', align: "left", resizable: true, editable: true, editrules: { required: true }, edittype: 'select', editoptions: {
value: {
'Normal': 'Normal',
'Sales': 'Sales',
'Admin': 'Admin',
'SuperAdmin': 'SuperAdmin'
},
dataEvents: [
{
type: 'change',
fn: function (e) {
var row = jQuery(e.target).closest('tr.jqgrow');
var rowId = row.attr('id');
jQuery("#grid").saveRow(rowId, false, 'clientArray');
}
}
]
}
},
{ name: 'Modified',width:250, index: 'Modified', sorttype: 'date', align: "left", resizable: true, editable: false, classes: 'not-editable-cell' },
{ name: 'ModifiedBy', width:250, index: 'ModifiedBy', sorttype: 'text', align: "left", resizable: true, editable: false, classes: 'not-editable-cell' },
{ name: 'CreatedBy', width:250,index: 'CreatedBy', sorttype: 'text', align: "left", resizable: true, editable: false, classes: 'not-editable-cell' },
{ name: 'Delete',width:50, index: 'Delete', width: 25, resizable: false, align: 'center', classes: 'not-editable-cell' }
],
rowNum: 10,
rowList: [10, 20, 50, 100],
sortable: true,
delete: true,
pager: '#pager',
height: '100%',
width: "650",
afterSubmitCell: function (serverStatus, rowid, cellname, value, iRow, iCol) {
var response = serverStatus.responseText;
var rst = 'false';
debugger;
if (response == rst) {
debugger;
setTimeout(function () {
$("#info_dialog").css({
left: "644px", // new left position of ERROR dialog
top: "380px" // new top position of ERROR dialog
});
}, 50);
return [false, "User Name Already Exist"];
}
else {
return [true, ""];
}
},
//rowNum: 10,
//rowList: [10, 20, 50, 100],
sortable: true,
loadonce: false,
ignoreCase: true,
caption: 'Administration',
search: false,
del: true,
cellEdit: true,
hidegrid: false,
pgbuttons : false,
pginput : false,
//viewrecords: true,
gridComplete: function () {
var ids = grid.jqGrid('getDataIDs');
for (var i = 0; i < ids.length; i++) {
var isDeleted = grid.jqGrid('getCell', ids[i], 'Delete');
if (isDeleted != 'true') {
grid.jqGrid('setCell', ids[i], 'Delete', '<img src="/Images/delete.png" alt="Delete Row" />');
}
else {
grid.jqGrid('setCell', ids[i], 'Delete', ' ');
}
}
}
}
);
grid.jqGrid('navGrid', '#pager',
{ resize: false, add: false, search: false, del: false, refresh: false, edit: false, alerttext: 'Please select one user' }
).jqGrid('navButtonAdd', '#pager',
{ title: "Add New users", buttonicon: "ui-icon ui-icon-plus", onClickButton: showNewUsersModal, position: "First", caption: "" });
});
i need a scrollable grid , when use come to this page i have to show the first 7 columns only just seven in full page. i have 11 columns in my grid rest of the columns can be seen by using scroll, but first 7 should be shown when grid loads. and every column should be re-sizable. can any body help me, i will 100% mark your suggestion if it works for me ...thank you ;) . if something is not explained i am here to explain please help me
and can i save the width of column permanently when user re-size the column, so when next time grid get loads the column should have the same width which is set by the user by re-sizing.. is it possible ?
I am not sure what you want,but you can auto adjust the width with JqGrid autowidth and shrinkToFit options.
Please refer this Post jqGrid and the autowidth option. How does it work?
this will do the trick.

Date Will Not Filter Correctly After Using Formatter on jqGrid Column

I'm having some issues getting a row of Dates to filter correctly in my jqGrid. Here's a portion of my .cshtml:
<script type="text/javascript">
$(function () {
var width = $(window).width() - 50;
$("#orders_grid").jqGrid({
datatype: "local",
width: width,
height: "auto",
search: true,
autowidth: false,
shrinkToFit: true,
colNames: ['ID', 'Status', 'Category','Sub Category', 'Title', 'Owner', 'Team', 'Priority', 'Release', 'Business Line', 'Created', 'Update'],
colModel: [
{ name: 'ID', width: 12, align: 'center', sorttype: 'int'},
{ name: 'GridStatus', width: 15, align: 'center'},
{ name: 'GridCategory', width: 15, align: 'center'},
{ name: 'GridSubCategory', width: 15, align: 'center'},
{ name: 'Title', width: 60, align: 'left' },
{ name: 'GridOwnerUser', width: 20, align: 'center'},
{ name: 'GridTeam', width: 30, align: 'center'},
{ name: 'GridPriority', width: 12, align: 'center'},
{ name: 'GridRelease', width: 12, align: 'center'},
{ name: 'GridBusinessLine', width: 12, align: 'center'},
{ name: 'CreatedDateTime', width: 14, align: 'center', sortable: true, sorttype: 'd', formatter: dateFix },
{ name: 'LastUpdateDateTime', width: 14, align: 'center', sortable: true, sorttype: 'd', formatter: dateFix }
],
rowNum: 20,
rowList: [20,50,100,1000,100000],
viewrecords: true,
pager: '#gridpager',
sortname: "ID",
sortable: true,
ignoreCase: true,
headertitles: true,
sortorder: "desc",
onSelectRow: function (rowId)
{
var id = $("#orders_grid").getCell(rowId, 'ID');
document.location.href = '/TicketCenter/Display/'+ id;
}
}).navGrid("#orders_grid_pager", { edit: false, add: false, del: false }, {}, {}, {}, { multipleSearch: true, multipleGroup: false, showQuery: false, recreateFilter: true });
$("#orders_grid").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true, defaultSearch: 'cn' });
setTimeout(function () { searchOrders('#Model.filterFor'); }, 200);
});
function dateFix(LastUpdateDateTime)
{
var x = LastUpdateDateTime.substring(6, LastUpdateDateTime.length - 2);
x = parseInt(x);
x = new Date(x);
x.setMinutes(x.getMinutes() - x.getTimezoneOffset());
x = x.format("mm/dd/yyyy h:MM TT");
return x;
}
function searchOrders(filter)
{
var data = { filter: filter }
var request = $.ajax({
url: "#Url.Action("ListTickets", "TicketCenter")",
type: "GET",
cache: false,
async: true,
data: data,
dataType: "json",
});
request.done(function (orders) {
var thegrid = $("#orders_grid");
thegrid.clearGridData();
setTimeout(function()
{
for (var i = 0; i < orders.length; i++)
{
thegrid.addRowData(i+1, orders[i]);
}
thegrid.trigger("reloadGrid");
}, 500);
});
request.fail(function(orders) {
});
}
</script>
I need to be able to search in the CreatedDateTime and LastUpdateDateTime columns. When I get my data from the database it it initially a datetime. When the grid loads, the date is displayed as "/Date102342523523463246236236", which is obviously in ticks. I formatted this with the dateFix formatter, which returns the date in mm/dd/yyyy h:MM TT format. Now when I try to search by date, I'm getting strange results. I'm thinking that the underlying data is still unformatted and it's searching off of that. Here's an example:
In my database, the CreatedDateTime for one object is 2013-10-11 20:20:10.963.
When the jqGrid loads the data, it shows /Date(1381537210963).
After I add formatter : dateFix to the colModel, it shows 10/11/2013 4:20 PM.
If I type 10 in the search box, it returns that object.
If I type 10/, it doesn't return anything.
If I type 2013, it doesn't return anything.
Is there a way to resolve this issue?
Decided to convert the date to a string in the controller before sending it to the grid. Resolved.

WepApi post parameters are null

I'm new to WebApi and I have problem with the received post parameters.
I want to populate jqgrid with data received from webapi controller method.
The grid code is as follows:
buildPostData: function (postData) {
var parameters = {
page: postData.page,
rows: postData.rows,
sidx: '',
sord: '',
_search: false,
searchField: '',
searchString: '',
searchOper: '',
filter: { groupOp: '', rules: [] }
};
$.extend(parameters, postData);
var data = {
parameters: parameters
};
return JSON.stringify(data);
}
bindGridAllData: function () {
var viewModel = this;
jQuery("#AllDataGrid").jqGrid({
url: 'api/DataApi/GetAllData',
datatype: 'json',
mtype: 'POST',
ajaxGridOptions: {
contentType: 'application/json; charset=utf-8'
},
serializeGridData: viewModel.buildPostData,
serializeRowData: function (data) {
return JSON.stringify(data);
},
jsonReader: { repeatitems: false, root: "d.rows", page: "d.page", total: "d.total", records: "d.records" },
colNames: ['DataId', 'Title', 'Description', 'Date', 'UserId', 'Name', 'Surname', 'PicturePath', 'NumberOfComments', ''],
colModel: [
{ name: 'DataId', index: 'DataId', width: 10, hidden: true, editable: false, sortable: false, key: true },
{ name: 'Title', index: 'Title', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
{ name: 'Description', index: 'Description', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
{ name: 'Date', index: 'Date', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
{ name: 'UserId', index: 'UserId', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
{ name: 'Name', index: 'Name', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
{ name: 'Surname', index: 'Surname', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
{ name: 'PicturePath', index: 'PicturePath', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
{ name: 'NumberOfComments', index: 'NumberOfComments', width: 10, editable: false, align: 'left', sortable: true, hidden: true },
{
name: 'DataInformation', index: 'DataInformation', width: 200, editable: false, align: 'left', sortable: true, formatter: function (cellvalue, options, rowObject) {
var template = $('<div data-bind="template: { name: \'DataRecord\', data: DataRecord, ajax: { async: false } }"></div>');
var data = { DataRecord: rowObject };
ko.applyBindings(data, template[0]);
return template.html();
}
}
],
height: '100%',
width: 900,
pager: '#resultGridAllDataPager',
rowNum: 10,
rownumbers: false,
scroll: false,
sortname: 'Date',
sortorder: "asc",
gridview: true,
hoverrows: false,
viewrecords: true,
cmTemplate: { title: false },
loadComplete: function () {
}
});
}
And the WebApi method is:
[HttpPost]
public JQGridData GetAllData(JQGridSearchParameters parameters)
{
using (dataMKbazaEntities context = new dataMKbazaEntities())
{
JQGridData grid = new JQGridData();
var allData = context.ispolniSITEpodatoci().ToList();
List<object> data = new List<object>();
foreach (var ad in allData)
{
data.Add(new {
DataId=ad.podatokID,
Title=ad.naslov,
Description=ad.opis,
Date=ad.datum,
UserId=ad.idKORISNIK,
Name=ad.ime,
Surname=ad.prezime,
PicturePath=ad.slika,
NumberOfComments=ad.brojKOMENTARI
});
}
grid.rows = data;
grid.page = parameters.page;
grid.records = data.Count;
grid.total = (int)Math.Ceiling((float)data.Count / (float)parameters.rows);
return grid;
}
}
When sending data, parameters have values defined from buildPostData method but when received in WepApi controller method they are null.
I have tried with [FromBody] in webapi method but with no success.
Status code in http request is 200 OK
Make sure of the following:
Your raw request has the Content-Type header. Looking at your client code you seem to be sending it, but just wanted to make sure that it is indeed sent over the wire. Web API had a bug where if you do not send Content-Type header, since we do not know which formatter to deserialize the content, we default to the default value of the C# type. In this case it could be 'null'. This has been fixed later and is not yet publicly available though.
Have the following check to see any model state errors:
if (!ModelState.IsValid)
{
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState));
}

How to properly set the jsonReader of a jqGrid to handle string array of rows

Here are my column and model items:
var col_names = ['Qty', 'SFC', 'Item Nbr', 'Brand', 'Product', 'Supplier', 'Price', 'UOM', 'Case', 'Remarks', 'Weight', 'Par', 'Sort', 'Purchased', 'ProductId'];
var col_model = [
{ name: 'Quantity', index: 'Quantity', width: 22, sorttype: "number", editable: true, edittype: 'text', editoptions: { size: 10, maxlength: 15} },
{ name: 'ProductAttributes', index: 'ProductAttributes', width: 50 },
{ name: 'ItemNum', index: 'ItemNum', width: 50, align: "right"},
{ name: 'BrandName', index: 'BrandName', width: 100 },
{ name: 'ProducName', index: 'ProducName', width: 150 },
{ name: 'SupplierName', index: 'SupplierName', width: 100 },
{ name: 'Price', index: 'Price', width: 40, sorttype: "number", align: "right" },
{ name: 'UOM', index: 'UOM', width: 30 },
{ name: 'CasePack', index: 'CasePack', width: 30 },
{ name: 'PackageRemarks', index: 'PackageRemarks', width: 80 },
{ name: 'AveWeight', index: 'AveWeight', width: 33, align: "right" },
{ name: 'Par', index: 'Par', width: 20, align: "right", editable: true, edittype: 'text', editoptions: { size: 10, maxlength: 15} },
{ name: 'SortPriority', index: 'SortPriority', hidden: true },
{ name: 'LastPurchaseDate', index: 'LastPurchaseDate', width: 50, align: "right" },
{ name: 'ProductId', index: 'ProductId', hidden: true, key: true },
];
here's the grid initialization
favoriteGrid = $('#favoriteGrid');
favoriteGrid.jqGrid({
url: '/xxx/yyy/zzz/',
datatype: 'json',
ajaxGridOptions: { contentType: "application/json" },
jsonReader: {
id: "ProductId",
cell: "",
root: function (obj) { return obj.rows; },
page: function () { return 1; },
total: function () { return 1; },
records: function (obj) { return obj.rows.length; },
repeatitems: false
},
colNames: col_names,
colModel: col_model,
pager: $('#favoritePager'),
pginput: false,
rowNum: 1000,
autowidth: true,
sortable: true, // enable column sorting
multiselect: true, // enable multiselct
gridview: true,
ignoreCase: true,
loadonce: true, // one ajax call per
loadui: 'block',
loadComplete: function () {
fixGridHeight(favoriteGrid);
},
ondblClickRow: function (rowid, ri, ci) {
},
onSelectRow: function (id) {
if (id && id !== lastSel) {
favoriteGrid.restoreRow(lastSel);
lastSel = id;
}
favoriteGrid.editRow(id, true);
},
gridComplete: function () {
}
}).jqGrid('navGrid', '#favoritePager',
{ add: false, edit: false, del: false, search: true, refresh: false },
{},
{},
{},
{ multipleSearch: true, showQuery: false },
{}).jqGrid('sortableRows').jqGrid('gridDnD');
and finally, the data:
{"rows":[["1",null,"342240"," ","15 AMP, 600V, TIME DELAY, CLASS G","Home Depot - Canada","3.83","EA","1","- 15A, 600V - Class G - Mfg #SC-15","0.02","","0",null,"2977175133"],["1",null,"3573375","NEWPRT","STEAK TOP SIRLOIN CH CC 8OZ","SYSCO","6.875","LB","24 PK","8 OZ","24 LB","","0",null,"1675949601"],["1",null,"201805"," ","GE-HOTPOINT DISHWASHER UPPER RACK","Home Depot - Canada","54.43","EA","1","Dishwasher Upper Rack - Fits Models #HDA2000, HDA2100 And GSD2100 - Mfg #WD28X10011","6.5","","0",null,"2977172115"],["1",null,"286044"," ","GE DISHWASHER SILVERWARE BASKET","Home Depot - Canada","19.19","EA","1","Silverware Basket - Mfg #WD28X265","0.06","","0",null,"2977172688"]]}
I get the right number of rows and the columns but no data is displayed in the grid, if that makes sense.
Just for completeness:
[HandleJsonException]
public JsonResult ProductGroupService(Int64 id = 0)
{
var q = Repository.GetFavoriteProducts(SimpleSessionPersister.User.Id, id).ToArray();
var result = (from fp in q
select new string[]
{
Convert.ToString(fp.Quantity),
fp.ProductAttributes,
fp.ItemNum,
fp.BrandName,
fp.ProducName,
fp.SupplierName,
Convert.ToString(fp.Price),
fp.UOM,
fp.CasePack,
fp.PackageRemarks,
fp.AveWeight,
Convert.ToString(fp.Par),
Convert.ToString(fp.SortPriority),
fp.LastPurchaseDate,
Convert.ToString(fp.ProductId)
}).ToArray();
var jsonData = new
{
rows = result
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
Thank you,
Stephen
You main problem is that you used repeatitems: false property of the jsonReader which is wrong for your input.
Moreover you should remove trailing comma at the end of definition of col_model. The error will be ignored by many modern browsers, but not for the old one.
After the changed the grid will be successfully loaded: see the demo.

Resources