what are colnames and colmodel in jqgrid - jqgrid

Can you explain what are these colnames and colmodel in jqgrid ?
Where are getting used ? defining columns , then ...how these 2 colnames / colmodels are inter related and all ??
and can please point why sorting is not happening in my below code:
<script type="text/javascript">
$('#grid').jqGrid({
url: '#(Url.Action("LoadIssues","Home"))',
datatype: 'json',
colNames: ['Category', 'Description', 'Issue_Title', 'LOGGED_BY', 'Notes', 'Priority', 'Status'],
mtype: 'GET',
colModel: [
{ name: 'Category', width: 100 },
{ name: 'Description', width: 100 },
{ name: 'Issue_Title', width: 100 },
{ name: 'LOGGED_BY', width: 100 },
{ name: 'Notes', width: 100 },
{ name: 'Priority', width: 100 },
{ name: 'Status', width: 100 }
],
jsonReader: {
id: 'id',
repeatitems: false
},
rowNum: 10,
rowList: [5, 10, 20, 30],
gridview: true,
pager: '#gridpager',
sortname: 'Description',
sortorder: "desc",
viewrecords: true,
shrinkToFit: true,
width: $('#gridContainer').width(),
height: 200,
hidegrid: false,
gridComplete: function () {
$('#gridContainer').find('.ui-jqgrid-titlebar').hide();
}
})

In a nutshell, colNames defines the names of your jqGrid columns on the page, and colModel specifies options for each column (name in the dataset, width, etc).
The documentation has more information:
colModel Array which describes the parameters of the columns.This is the most important part of the grid. For a full description of all valid values see colModel API.
colNames
An array in which we place the names of the columns. This is the text that appears in the head of the grid (header layer). The names are separated with commas. Note that the number of elements in this array should be equal of the number elements in the colModel array.

Related

jqGrid Edit Grid fails when column value has space

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;
}
}

Select current value instead of text in a grid with inline editing

By default jqGrid considers the text value to set the selected option in a combo box. How can I add a column for the key value and have jqGrid selecting the right option using the key value?
I've been using the custom formatter, but some pages are displaying undefined when the rows are not editable, and when the inline edit modes is enabled, they display the right option.
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('#Grid').jqGrid({
autowidth: true,
datatype: 'json',
height: '100%',
pager: '#pager',
rowNum: 10,
sortname: 'Description',
url: '/AppUrl/Service',
viewrecords: true,
gridComplete: function () { OnGridComplete() },
onSelectRow: function (rowid, status) { grid.EditGridRow(rowid) },
colModel: [
{
name: 'ID',
hidden: true,
key: true,
index: 'ID'
}, {
name: 'ModuleId',
formatter: formatAsDropDown,
label: 'Módulo',
sortable: true,
width: 300,
editable: true,
edittype: 'select',
editoptions: { "value": "1:Modulo 1;2:Modulo 2;3:Modulo 3" },
index: 'ModuleId'
}, {
name: 'Description',
label: 'Description',
sortable: true,
width: 300,
editable: true,
index: 'Description'
}
]
});
function formatAsDropDown(cellvalue, options, rowObject) {
return rowObject.ModuleName;
}
});
</script>

jqGrid - sort not working

$("#grid").jqGrid({
datatype: 'local',
mtype: 'GET',
loadui: 'block',
altRows: true,
altclass: "myAltRow",
multiselect: false,
recordpos: "right",
pagerpos: "center",
pager: $('#gridt_summarypager'),
pginput: false,
rowNum: 100,
recordtext: "Showing {0} - {1} of {2}",
viewrecords: true,
sortname: 'Project',
sortorder: 'asc',
colNames: ['ProjectID', '<%: Project %>', '<%: ProjectTitle %>' , 'ProjectItemID', '<%: usProjectItem %>', 'Hours To Authorise', 'Hours Not Posted', 'Hours Rejected', 'Last 12 Months'],
colModel: [
{ name: 'ProjectID', index: 'ProjectID', width: 0, hidden: true},
{ name: 'Project', index: 'Project', width: 90, align: 'left', formatter: htmlEncodedString },
{ name: 'ProjectTitle', index: 'ProjectTitle', width: 90, align: 'left', formatter: htmlEncodedString },
{ name: 'ProjectItemID', index: 'ProjectItemID', width: 0, hidden:true },
{ name: 'ProjectItem', index: 'ProjectItem', width: 100, align: 'left', formatter: htmlEncodedString },
{ name: 'HoursToAuthorise', index: 'HoursToAuthorise', width: 125, align: 'right', formatter: timesheetsProjectToAuthoriseQueryFormat },
{ name: 'HoursNotPosted', index: 'HoursNotPosted', width: 125, align: 'right', formatter: timesheetsProjectUnpostedQueryFormat<% if (!(bool)ViewData["PostingEnabled"]) { %>, hidden: true <% } %> },
{ name: 'HoursRejected', index: 'HoursRejected', width: 125, align: 'right', formatter: timesheetsProjectRejectedQueryFormat },
{ name: 'HoursSubmitted12Months', index: 'HoursSubmitted12Months', width: 125, align: 'right', formatter: timesheetsProjectYearQueryFormat }],
imgpath: '../../Scripts/css/ui-lightness/images',
height: 145,
shrinkToFit: false,
hoverrows: false,
loadError: function (xhr, st, err) {
if (xhr.status == 200) {
window.location = '<%= loginPage %>';
}
else if (xhr.status == 500) {
$('#grid_summary_errors').html(xhr.statusText);
}
},
beforeSelectRow: function(rowid, e) {
/* disable row selection */
return false;
},
onSortCol: function (index, columnIndex, sortOrder) {
var col = $("#grid_summarygrid").getGridParam('colNames');
var label = "Ordered by " + col[columnIndex] + " " + sortOrder + "ending";
$("#gridsort").text(label);
}
});
$("#grid").setGridParam({ url: '<%= Url.Action(dataMethod, controllerName)%>?qid=xxx', page: 1, datatype: "json" })
.trigger('reloadGrid');
Currently using jqGrid 4.4.1 and it loads data fine but once sort is applied it updates the sort label buy grid data is not sorted. What is going on? Any help most appreciated...
If you set url and change datatype of grid to "json" then your server code is responsible for sorting of data like for paging too. If you want to load all data for the grid at once and want that jqGrid do sorting and paging for you then you should use loadonce: true option.
I recommend you additionally to include gridview: true option in jqGrid, replace pager: $('#gridt_summarypager') to pager: '#gridt_summarypager', remove not existing parameter imgpath and consider to use autoencode: true option of jqGrid which makes HTML encoding of strings in all columns which not contains custom formatter.
{
name: "name",
index:"name",
**sortable: true,**
editable: true,
**sorttype: 'text',**
key: true
},
Make sure that sorttype complies with the fields data type. for example if the grid field "name" contains int numbers, then you should have sorttype: 'int'.
Also as mentioned by Oleg, you need to set loadonce to true.
pager: "#pager",
gridview: true,
rowNum: 5,
loadonce:true,
multiSort: true,
rownumbers: true,
viewrecords: true,
rowList: [5, 10, 15],
include sortable: true in column as well as jqgrid property.
Chaning this parameter value worked for me.
loadonce: true

set radiobutton in loadComplete of grid

$("#addressList").jqGrid({
url: '/Storage/Shipping/GetCustomerAddresses?q=2&Customerid=' + $("#saveCustomerID").val(),
datatype: "Json",
jsonReader: {
root: "Data.rows",
page: "Data.page",
total: "Data.total",
records: "Data.records",
repeatitems: true,
userdata: "userdata",
cell: "cell"
},
colNames: ['', 'Line 1', 'Line 2', 'City', 'State'],
colModel: [
{ name: 'myradio', width: 30, fixed: true, align: 'center', resizable: false, sortable: false,
formatter: function (cellValue, option) {
return '<input type="radio" name="radio_' + option.gid + '" />';
}
},
{ name: 'Line1', index: 'Line1', width: 250 },
{ name: 'Line2', index: 'Line2', width: 250 },
{ name: 'City', index: 'City', width: 210 },
{ name: 'State', index: 'State', width: 75 }
],
page: 1,
rowNum: 50,
rowList: [20, 50, 100],
pager: '#pager',
viewrecords: true,
grouping: false,
caption: "Addresses",
mtype: "POST",
width: "100%",
height: "100%",
loadonce: true,
sortable: false,
beforeSelectRow: function (rowid, e) {
var radio = $(e.target).closest('tr').find('input[type="radio"]');
radio.attr('checked', 'checked');
$("#saveCustomerAddressID").val(rowid.toString());
return true; // allow row selection
},
loadComplete: function () {
var grid_ids = $("#addressList").jqGrid('getDataIDs');
for (var i = 0; i < grid_ids.length; i++) {
if ($("#saveCustomerAddressID").val() == grid_ids[i]) {
{
$("#addressList").jqGrid('setSelection', grid_ids[i], true);
}
}
}
}
//, postdata: { CustomerID: $("#saveCustomerID").val() }
});
The above code sets the selection correctly in loadcomplete. The $("#saveCustomerAddressID").val() is the rowid fro the JSON Data.
The radiobutton is set in the beforeSelectRow. I know the row number and grid column of of the radiobutton to be set, but how do you set the radiobutton?
if you have row number and grid column, then it shouldn't be difficult. get the id(css) of that column where you have your radio button(check developer tools for this). now lets say that radio button is in a column name "Demo"
so the id would look like something like this i suppose
var demo= $("'#'+rowid+'Demo'")//check developer tools for confirmation
demo.attr('checked', 'checked');
$("#saveCustomerAddressID").val(rowid.toString());//continue with you loadComplete code
and for set selection use setSelection method of jqgrid after this continue with your code of loadcomplete

jqGrid has missing id in checkbox

new to jqGrid. I'm displaying a grid, readonly so far, but now with a checkbox in one column. However, the checkbox is not produced with id or name attributes - which according to the documentation it will be. Any pre-requisites to get the id's? Ideally I'd like an id on teh checkbox that is like the prefix + rowid.
Code is:
jQuery("#grdProducts").jqGrid(
{
url: '<%: Url.Action( "GetPageByCode", "Products" ) %>',
datatype: "xml",
mtype: 'POST',
colNames: [ 'Code', 'Language', 'Type', 'Name', 'Price £', 'Web', 'Dimensions', 'Actions' ],
colModel:
[
{ name: 'code', index: 'ProductCode', width: 120, key: true },
{ name: 'language', index: 'Category1', width: 100 },
{ name: 'type', index: 'Category2', width: 140 },
{ name: 'name', index: 'Name', width: 240 },
{ name: 'price', index: 'PriceGross', width: 60, align: "right" },
{ name: 'web', width: 32, align: "center",
editable:true, edittype:'checkbox', editoptions: {value:"True:False" },
formatter: "checkbox", formatoptions: {disabled : false } },
{ name: 'dimensions', width: 120, sortable:false },
{ name: 'actions', width: 120, sortable:false }
],
height:660,
rowList:[ 20,50,100 ],
pager: '#pgrProducts',
sortname: 'ProductCode',
// speed improvement - as long as certain features not needed
gridview: true,
// shows the 'showing 1 - n of total' in the pager, ... and the text to display
viewrecords: true,
recordtext: "showing products {0} - {1} of {2}",
// alternate colour rows
altRows:true, altclass: 'gridAltRow',
// shows sort arrows in titles
viewsortcols: [ true ],
// the grid outer caption
caption: 'Products by product code',
// configurable page size
rowNum: <%: Model.PageSize %>,
// double click on a row goes to the edit row page
ondblClickRow: function( rowid )
{
document.location.href = '/Product/Edit/' + rowid;
}
}
);
} );
Why you need to have id attribute in the checkbox? If you need to get checkbox it will be very easy in your case because there are only one checkbox in the row, so you can use
$('#' + rowid + ' input:checkbox')
If you need to detect 'click' event for example to the checkbox you can find the corresponding code in the demo from the answer.

Resources