jqGrid Edit Grid fails when column value has space - jqgrid

I am new to jqGrid. I am working on creating a grid with edit functionality to each row. I was able to successfully create the grid and edit the rows. But i found that when there i space in my name column(editable column) in the grid. the edit doesn't work. The row remains disabled if i click on pencil icon.
For Example: the edit works if name is like 'test' but fails when name is 'test run'.
$(this.tableElement).jqGrid('GridUnload');
$(this.tableElement).jqGrid({
url: url1,
editurl: urledit,
mtype: 'POST',
datatype: 'json',
postData: {
id: ID
},
colNames: [
' Name',
' ID',
'QuoID',
''
],
colModel: [
{ name: 'Name', id: 'Name', width: 50, sorttype: 'text', sortable: true, editable: true },
{ name: 'ID', id: 'ID', hidden: true, editable: true },
{ name: 'QuoID', id: 'QuoID', hidden: true, editable: true },
{
name: 'Actions', index: 'Actions', width: 30, height: 120, formatter: 'actions',
formatoptions: {
keys: true,
onEdit: function (rowid)
{ alert(rowid); }
}
},
],
rowNum: 25,
rowList: [25, 50, 100],
sortname: 'PackageID',
sortorder: "desc",
firstsortorder: 'desc',
loadonce: !GetFeatureToggle("MultiplePackages"),
sortable: true,
viewrecords: true,
caption: "Packages",
width: 450,
height: 200,
hidegrid: false,
loadComplete: function (data) {
if (_this.firstLoad == true) {
setTimeout(function () { $(_this.elements.list).jqGrid('setGridParam', { page: 1 }).trigger("reloadGrid"); }, 1);
_this.firstLoad = false;
}
}

Related

Set column to show 0 in JqGrid

I have a jqGrid.
my function is --
$("#grid").jqGrid({
url: "/Log/GetLogs",
datatype: 'json',
mtype: 'Get',
colNames: ['LogID', 'Agency Billing Code', 'License Number', 'Equipement Number', 'Year', 'Make', 'Model', 'Color', 'Begin Miles', 'End Miles'],
colModel: [
{ key: true, hidden: true, name: 'ID', index: 'ID' },
{ key: false, name: 'Year', index: 'Year', editable: false },
{ key: false, name: 'Make', index: 'Make', editable: false },
{ key: false, name: 'Model', index: 'Model', editable: false },
{ key: false, name: 'Color', index: 'Color', editable: false },
{ key: false, name: 'Miles', index: 'Miles', editable: true, cellvalue: "" },
],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 20, 30, 40],
height: '100%',
viewrecords: true,
caption: 'Log List',
emptyrecords: 'No Records',
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
id: "0"
},
autowidth: true,
multiselect: false
});
When the grid loads, database is returning the value of "Miles" but I don't want to show that. I just want to show 0, and when I edit the miles values it should map to Miles in my object.
Please let me know how can I achieve that?
Thanks..
To accomplish what you need you need to have a formatter and unformatter for the miles column. You have not specified on how you will be editing the grid row(Inline, form Edit, custom...etc) but I created inline edit as an example for you.
Here is a the complete solution in jsfiddle if you want to play with it. for editing just click the row and the miles will show its original value when on edit but show 0 on non edit mode. For more details on how formatting works see Here
var mileformatter= function (cellval, options, rowObject) {
return "<span data-val='"+cellval+"'>0</span>";
}
var mileUnFormat= function (cellvalue, options, cell) {
return $('span', cell).attr('data-val');
}
"use strict";
var mydata = [
{ID:"1", Year: "1933", Make: "Nissan",Model:"Model1", Color: "White",Miles:1222},
{ID:"2", Year: "2008", Make: "Toyota",Model:"Model2" , Color: "Gray",Miles:3000},
];
$("#list").jqGrid({
data: mydata,
datatype: "local",
mtype: 'Get',
colModel: [
{ key: true, hidden: true, name: 'ID', index: 'ID' },
{ key: false, name: 'Year', index: 'Year', editable: false },
{ key: false, name: 'Make', index: 'Make', editable: false },
{ key: false, name: 'Model', index: 'Model', editable: false },
{ key: false, name: 'Color', index: 'Color', editable: false },
{ key: false, formatter:mileformatter,unformat:mileUnFormat, name: 'Miles', index: 'Miles', editable: true, cellvalue: "" },
],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 20, 30, 40],
height: '100%',
viewrecords: true,
caption: 'Log List',
emptyrecords: 'No Records',
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
id: "0"
},
autowidth: true,
multiselect: false,
onSelectRow: function (id) {
jQuery('#list').editRow(id, true);
}
});
Here is an edited answer for what you are looking for and a new jsfiddle link to play with Note that I removed the unformatter and also added beforeSaveRow function.
var onEdit=false;
var mileformatter= function (cellval, options, rowObject) {
if(onEdit==true)
{
return cellval;
onEdit=false;
}
return 0;
}
"use strict";
var mydata = [
{ID:"1", Year: "1933", Make: "Nissan",Model:"Model1", Color: "White",Miles:1222},
{ID:"2", Year: "2008", Make: "Toyota",Model:"Model2" , Color: "Gray",Miles:3000},
];
$("#list").jqGrid({
data: mydata,
datatype: "local",
mtype: 'Get',
colModel: [
{ key: true, hidden: true, name: 'ID', index: 'ID' },
{ key: false, name: 'Year', index: 'Year', editable: false },
{ key: false, name: 'Make', index: 'Make', editable: false },
{ key: false, name: 'Model', index: 'Model', editable: false },
{ key: false, name: 'Color', index: 'Color', editable: false },
{ key: false, formatter:mileformatter, name: 'Miles', index: 'Miles', editable: true, cellvalue: "" },
],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 20, 30, 40],
height: '100%',
viewrecords: true,
caption: 'Log List',
emptyrecords: 'No Records',
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
id: "0"
},
autowidth: true,
multiselect: false,
onSelectRow: function (id) {
jQuery('#list').editRow(id,
{
"keys": true,
oneditfunc: function () {
},
"successfunc": function () {
alert('successfunc');
},
"url": null,
"extraparam": {},
"aftersavefunc": function () {
alert('aftersavefunc');
},
"errorfunc": null,
"afterrestorefunc": null,
"restoreAfterError": true,
"beforeSaveRow": function (options, rowid) {
onEdit=true;
jQuery("#list").saveRow(id, false);
return false;
}
});
}
});
You can customize the SaveRow as follows and put the post url of your own.
saveparameters = {
"successfunc" : null,
"url" : "yoururl",
"extraparam" : {},
"aftersavefunc" : null,
"errorfunc": null,
"afterrestorefunc" : null,
"restoreAfterError" : true,
"mtype" : "POST"
}
jQuery("#grid_id").jqGrid('saveRow',rowid, saveparameters);

How to rebind JQgrid data after applying search on the existing grid data

I have a customer details where I am showing the customers bill status which is working fine,
Is it possible to rebind the grid once we loaded the data in grid for example:
when I am coming from another page how to apply search on the existing grid data and rebind it to grid.
Below is my colmodel and other properties which I am using but not working properly,
$("#BillDetails").jqGrid({
url: '../Handler.ashx,
datatype: "json",
colNames: [ 'S.No', 'Bill NO', 'Customer Name', 'Customer Type', 'Bill Date',Bill Status'],
colModel: [
{ name: 'SERIAL', sortable: false, search: true, stype: 'text', width: 40, hidden: false,
align: 'right', sorttype: 'int' },
{ name: 'BILL_NO', sortable: false, search: true, width: 80, hidden: false,
align: 'right',formatter: editLink,},
{ name: 'CUSTOMER_NAME', search: true, align: 'left', width: 150, aligh: 'left',
sortable: false },
{ name: 'CHECK_TYPE', align: 'left', width: 150, search: true, aligh: 'left',
sortable: false},
{ name: 'BILL_DATE', width: 85, sortable: false, search: true, align: 'center',
sorttype: 'date', formatter: "date", formatoptions: { newformat: 'd/m/Y' } },
{ name: 'BILL_STATUS', width: 90, align: 'left', search: true, stype: 'select',
searchoptions: { value: 'Pending:Pending;
On Hold:On Hold;Clear:Clear;Incompelete:InComplete,sortable: false } },
],
width: '980',
height: '450',
shrinkToFit: false,
sortable: true,
mtype: 'GET',
scrollbar: true,
sortname: 'BILL_NO',
sortorder: 'asc',
hidegrid: false,
loadonce: true,
ignoreCase: true,
rowNum: 50,
pager: '#Pager',
viewrecords: true,
// Search on page load from grid data
gridComplete: function () {
var searchFilter = "Clear"; // In this I will receive the the status from query string,
//for now i have provided the the hard core value to test
var gridData = $("#jQGridDemo").jqGrid('getGridParam', 'data');
console.log(gridData);
if (searchFilter != "") {
var grid = $("#BillDetails"), f;
f = { rules: [] };
f.rules.push({ field: "STATUS", op: "eq", data: searchFilter });
grid[0].p.search = true;
$("#BillDetails").setGridParam({ data: gridData, postData: { filters: f },
datatype: "local", });
}
}
});
If you want to reload jqGrid when coming from another page with search parameters you could use postData property of jqGrid.
Something like this:
$("#BillDetails").jqGrid({
url: '../Handler.ashx',
datatype: "json",
mtype: 'GET',
postData:
{
Page: 'Dashboard',
strUserID: strUserID,
}
You can populate this property with js or on server side as you need.
UPDATE
You also can use setGridParam method of jqgrid:
$("#BillDetails").jqGrid('setGridParam',
{
postData: {
Page: 'Dashboard',
strUserID: strUserID,
}
});
$("#BillDetails").trigger("reloadGrid");
But as i understand this way you also just add values to postData property.
Also you can try it this way:
$("#BillDetails").trigger('reloadGrid', [{page:1}]);
You can pass there your new params as on grid init.

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.

jqgrid edit button accessing a set of checkboxes that are selected

I have the below code in my jqgrid
<script type="text/javascript">
jQuery(document).ready(function() {
var grid = jQuery("#list");
$("#editBtn").click(function() {
alert("hi"); });
jQuery("#list").jqGrid({
url: '<%= Url.Action("DynamicGridData") %>',
datatype: 'json',
mtype: 'POST',
colNames: ['checkbox', 'Id','col1','col2' ],
colModel: [
{ name: 'checkbox', index: 'checkbox', sortable: false, formatter: "checkbox", formatoptions: { disabled: false }, editable: true, edittype: "checkbox" },
{ name: 'Id', index: 'Id', search: false, stype: 'text', sortable: true, sorttype: 'int', hidden: true },
{ name: 'col1', index: 'col1', search: false, stype: 'text', sortable: true, sorttype: 'int', search: false, hidden: true },
{ name: 'col2', index: 'col2', sortable: true, search: false, width: 30, stype: 'int' } ],
pager: jQuery('#pager'),
rowNum: 40,
rowList: [20, 40, 60, 100],
sortname: 'Id',
sortorder: 'asc',
gridview: true,
autowidth: true,
rownumbers: true,
viewrecords: true,
toppager: true,
height: "100%",
width: "100%",
caption: 'Grid Data'
});
});
I can fire the test alert in the editBtn function, how can a user access the id column of the records that have their checkboxes selected by the user?
use the following code to get the Id column data of which checkboxes are ticked...
var grid = jQuery("#list");
$("#editBtn").click(function() {
var str = '';
var data = grid.getRowData();
for(i=0;i<data.length;i++){
if(data[i].checkbox==='Yes')
str += data[i].Id+',';
}
alert(str);
});
str variable consists of the values of Id column for which checkbox is selected by user.

problem loading data in details jqGrid from master grid?

When I am making a call first time it shows data in my details grid from master grid but when selecting other row its not populating the new data in the details grid..
jQuery("#list10").jqGrid({
sortable: true,
url: '/cpsb/unprocessedOrders.do?method=getInitialUnprocessedList',
datatype: 'json',
colNames: ['Order', 'Load', 'Gate Time', 'Stop', 'Customer', 'Status'],
colModel: [
{ name: 'orderNumber', index: 'orderNumber', width: 120, align: "center",
sorttype: "int", key: true },
{ name: 'loadNumber', index: 'loadNumber', width: 100, align: "center",
sorttype: "int" },
{ name: 'latestTime', index: 'latestTime', width: 160, align: "center",
align: "center" },
{ name: 'stopSeq', index: 'stopSeq', width: 80, align: "center",
sorttype: "int" },
{ name: 'customerNumber', index: 'customerNumber', width: 60,
align: "center", sorttype: "int" },
{ name: 'orderStatus', index: 'orderStatus', width: 120, align: "center" }
],
rowNum: 10,
rowList: [10, 20, 30],
jsonReader: { repeatitems: false,
root: function (obj) {
return obj;
},
page: function (obj) { return 1; },
total: function (obj) { return 1; },
records: function (obj) { return obj.length; }
},
pager: '#pager10',
sortname: 'Gate Time',
sortorder: "desc",
gridview: true,
loadonce: true,
viewrecords: true,
multiselect: true,
multikey: 'ctrlKey',
caption: "Order Header",
onSelectRow: function (ids) {
if (ids == null) {
ids = 0;
if (jQuery("#list10_d").jqGrid('getGridParam', 'records') > 0) {
jQuery("#list10_d").jqGrid('setGridParam', { url:
"/cpsb/unprocessedOrders.do?method=getUnprocessedOrderDetails&orderNum=" + ids });
jQuery("#list10_d").jqGrid('setCaption',
"Order Header: " + ids).trigger('reloadGrid');
}
}
else {
jQuery("#list10_d").jqGrid('setGridParam', { url:
"/cpsb/unprocessedOrders.do?method=getUnprocessedOrderDetails&orderNum=" + ids });
jQuery("#list10_d").jqGrid('setCaption',
"Order Details: " + ids).trigger('reloadGrid');
}
},
height: '100%'
});
jQuery("#list10").jqGrid('navGrid','#pager10',
{view:true,add:false,edit:false,del:false,searchtext:"Filter"},
{},{},{},{multipleSearch:true});
$("#list10").jqGrid('hideCol', 'cb');
2nd grid for order details
jQuery("#list10").jqGrid('reloadGrid');
jQuery("#list10_d").jqGrid({
height: 100,
url: "/cpsb/unprocessedOrders.do?method=getUnprocessedOrderDetails&orderNum=",
datatype: "json",
colNames: ['Order', 'SKU', 'UPC', 'Item Description', 'Quantity Ordered',
'Teach in Hold?'],
colModel: [
{ name: 'orderNumber', index: 'orderNumber', width: 55 },
{ name: 'sku', index: 'sku', width: 55 },
{ name: 'upc', index: 'upc', width: 40, align: "right" },
{ name: 'itemDescription', index: 'itemDescription', width: 150,
align: "right" },
{ name: 'quantityOrdered', index: 'quantityOrdered', width: 150,
align: "right", sortable: false, search: false },
{ name: 'teachInId', index: 'teachInId', width: 150,
align: "right", editable: true, edittype: "checkbox",
formatter: 'checkbox', editoptions: { value: "true:false"} }],
rowNum: 5,
rowList: [5, 10, 20],
jsonReader: { repeatitems: false,
root: function (obj) {
return obj;
},
page: function (obj) { return 1; },
total: function (obj) { return 1; },
records: function (obj) { return obj.length; }
},
pager: '#pager10_d',
sortname: 'SKU',
loadonce: true,
viewrecords: true,
sortorder: "asc",
multiselect: true,
multikey: 'ctrlKey',
caption: "Order Detail",
height: '100%'
}).navGrid('#pager10_d', { view: true, add: false, edit: false, del: false },
{}, {}, {}, { multipleSearch: true });
$("#list10_d").jqGrid('hideCol', 'cb');
jQuery("#ms1").click(function () {
var s;
s = jQuery("#list10_d").jqGrid('getGridParam', 'selarrrow');
alert(s);
});
Edit: I am able view different records once I refresh the page...But after one selection other selection don't work
edit2: after debugging i saw that I am appending the orderNum parameter correctly but this is not making any call to the action class....any idea? thanks!
It seems to me that the answer on your main problem you find here: JqGrid Reload not working.
Because you use loadonce:true in both grids, the datatype in every grid will be changed from "json" to "local" after the first load. It seems to me that you should just remove loadonce:true for the second (detailed) grid. If you do want use loadonce:true for example for local sorting or local paging, then you should reset datatype to "json" in the same call jQuery("#list10_d").jqGrid('setGridParam',{url:"...", datatype: "json"}); .

Resources