addRowData with jqGrid doesn't work correctly - jqgrid

After I inserted a new row using
my_table.jqGrid('addRowData', selected_rowid+1, newRow, 'after', selected_rowid);
The new row is inserted correctly on the table but when I output the grid data
my_table.getGridParam('data')
the new row is inserted in the very end of the data array. Also, when I click any row after the new row inserted, the rowid was not updated (incremented).
Is there any way to fix this?
The grid definition is this:
my_table.jqGrid({
colNames:['Id', 'Start','End', 'Duration', 'Text','Position','Alignment','Warnings','Errors'],
colModel:[
{name:'rowid',index:'myrowid',hidden: true},
{name:'start',index:'startTime', width:10, formatter: convertTime},
{name:'end',index:'endTime', width:10, formatter: convertTime},
{name:'duration',index:'duration', width:10, formatter: getDuration},
{name:'data',index:'dataText', width:40, align:"middle", sortable:false, editable:true, edittype: 'text'},
{name:'position',index:'position', width:5,editable:true, sortable: false, edittype:'text'},
{name:'alignment',index:'alignment',width:5,editable:true, sortable: false,edittype:'text'},
{name:'warnings',index:'warnings',hidden: true, formatter: warnings},
{name:'errors',index:'errors',hidden: true, formatter: errors},
],
//pager: '#packagePager',
datatype: "local",
width: 700,
height: 800,
data: my.caption_data.rows,
localReader: {
id: "rowid",
repeatitems: true
},
rowNum: length,
viewrecords: true,
cellsubmit:'clientArray',
rownumbers:true,
hoverarrows: true,
multiselect: true,
cellEdit: false,
scrollrows : true,
multiboxonly:true,
autoencode:true,
});
I read a previous post and it seems like i can pass in "undefined" as the rowid. The problem is I want to have the ids sorted correctly. So if I insert a row after rowid of 7, the new row should have rowid of 8 and the rest should all be pushed back by one. Is this possible with jqGrid?

Related

JQGrid drop down selected option

I am having a non-inline edit jqGrid containing an editable drop down column.
I use string as the editoptions.value, e.g. 1:A;A:B.
There is one option having the value same as the key of another option.
Then when I want to edit the record, the selected option is mapped wrongly. I found that this is due to the implementation in createEl function. It check $.trim(sv[1]) === $.trim(vl) to set the selected option also. May I know if there is anyone having the same problem?
I am using jqGrid 4.6.0. My code is as follows. Thanks a lot in advance.
var mydata = [
{ id: '1', col: '1' },
{ id: '2', col: 'A' }
];
var grid = jQuery("#grid");
grid.jqGrid({
data: mydata,
datatype: 'local',
colModel: [
{
name: 'col', width: 100, editable: true, formatter: 'select',
edittype: 'select', editoptions: { value: 'A:B;1:A' }
}
],
editurl: 'clientArray',
height: '100%',
viewrecords: true,
rownumbers: true,
pager: '#pager'
}).navGrid('#pager', { edit: true, add: true, del: false, search: false, refresh: false });

jqGrid with multiselect:true and reloadGrid double checkbox column

The column for multiselect is saved in the grid param and when I reload the grid using a saved state(getGridParam) I get a double column for selection that messes up my column order.
My grid before I load param
After
here is the code :
jQuery("#list2").jqGrid({
url:'jqgrid/server.php?q=2',
datatype: "json",
colNames:['a','UID','Phone', 'Message', 'created','replyed'],
colModel:[
{name:'action',index:'action', width:55,search:false,'sortable':false},
{name:'id',index:'uid', width:55},
{name:'phone_number',index:'phone_number', width:90,'editable':true},
{name:'message',index:'message', width:500,'editable':true},
{name:'created',index:'created', width:80, align:"right",'editable':true,stype:'select',edittype:"select",editoptions: {value:"Yes:Yes;No:No"}},
{name:'replyed',index:'replyed', width:80, align:"right",'editable':true,edittype:"checkbox",editoptions: {value:"Yes:No"}},
],
rowNum:50,
toolbar: true,
height: '100%',
width: 800,
rowList:[10,20,50,100,300,1000],
pager: '#pager2',
sortname: 'uid',
sortable:true,
viewrecords: true,
sortorder: "desc",
caption:"Pilote",
gridview : true,
multiselect: true,
});
function saveStateGrid(){
state = $('#list2').getGridParam();
localStorage.setItem("gridState", JSON.stringify(state));
}
function loadStateGrid(){
state = JSON.parse(localStorage.getItem('gridState'));
jQuery("#list2").GridUnload("#list2");
//state.multiselect = false;
$('#list2').jqGrid(state);
$('#list2').trigger('reloadGrid');
}

jqgrid - sort jqgrid together with json data

I have a jqgrid. I need to pull the json data from jqgrid after the client sorted the grid. Pulled json data should be the sorted version and not the original. How can I do this?
Here is my jqgrid source
jQuery("#myGrid").jqGrid({
datastr: jsonData,
datatype: 'jsonstring',
jsonReader: { repeatitems: false, root: function(obj) { return obj; }},
colNames: [ 'Column 1',
'Column 2'],
colModel: [
{ name: 'prop1', index: 'prop1'},
{ name: 'prop2', index: 'prop2'},
],
headertitles: true,
sortable: true,
pginput: true,
rownumbers: true,
rowNum: 5,
rowList: [5,20, 100, 10000],
pager: '#pager',
width:'600',
height:'300',
shrinkToFit:false,
viewrecords: true,
loadonce: true
});
Currently when I alert(jsonData) in the gridComplete it shows the original json data and not the sorted one.
For those who come across this problem, you can just take the data parameter passed to the loadComplete event of jqGrid. This data is the sorted version of the grid's data source.

JQgrid get number of rows (loadonce:true)

I have a jqgrid with inline client-side search. When the database returns 0 records I am trying to hide the grid and show a custom message. The problem is that now when I use the filters the same thing happens. I need to get the number of rows from the back-end response and none of the below lines work. When the page loads I get 5 and 5, and when I filter I get 0 and 0. How can this be achieved?
.jqGrid('getGridParam', 'records')
.jqGrid('getGridParam', 'reccount')
var contratsAC=$("#ContratsAC");
contratsAC.jqGrid({
url:'<?php echo base_url().'rest/AC_Rest/get_contrats/'?>',
mtype : "post",
datatype: "json",
colNames:['Nr dossier','Type','Nom','Statut','Date creation','Date derniere maj','Commentaires','Auteur'],
colModel:[
{name:'nr_dossier',index:'nr_dossier',search:false, align:"center"},
{name:'type',index:'type',search:false, align:"center"},
{name:'nomClient',index:'nomClient',search:false, align:"center"},
{name:'statut',index:'statut',search: true, sortable: false, width:180, stype:'select',
searchoptions:{ value:statuts}, editable: false},
{name:'date_cre',index:'date_cre',search:false, align:"center"},
{name:'dern_date_maj',index:'dern_date_maj',search:false, align:"center"},
{name:'commentaires',index:'commentaires',search:false, align:"center"},
{name:'auteur',index:'auteur',search:false, align:"center"}
],
rowNum:10,
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
id: "nr_dossier",
userdata: "userdata"
},
width: 960,
height: "100%",
rowList:[10,20,30],
pager: '#pager1',
sortname: 'nr_dossier',
viewrecords: true,
rownumbers: true,
gridview: true,
loadonce: true,
loadComplete: function(data){
alert(contratsAC.jqGrid('getGridParam', 'records'));
alert(contratsAC.jqGrid('getGridParam', 'reccount'));
if (0==contratsAC.jqGrid('getGridParam', 'records'))
{
contratsAC.jqGrid('GridUnload');
$("#contratsACNoDataMessage").html('<span>Aucun enregistrement a afficher.</span>');
}
else $("#ContratsACContainer").show();
},
pagination:true,
}).navGrid('#pager1',
{add: false,
edit:false,
del:false});
contratsAC.jqGrid('filterToolbar',{searchOnEnter:true,stringResult: true});
You may try getting the length of the data array holded by the grid:
$("#ContratsAC").jqGrid('getGridParam', 'data').length;
The below will give you all the rows - initial data source based
contratsAC.jqGrid('getGridParam', 'records');
To get the number of rows on the current page, lets say after filtering, use
contratsAC.jqGrid('getGridParam', 'reccount');
To get all the records across all pages after filtering, use below
loadComplete: function (gridData) {
var isSearchPerformed = $grid.getGridParam("postData")._search;
if (isSearchPerformed) {
$("#spanFilterTotal").text(gridData.records);
}

jqgrid columns not sorting when clicked

I have the following block of code:
$("#searchlist").jqGrid({
url:'./searchlibrary',
datatype: 'json',
mtype: 'POST',
postData: {type: function(){return $('select[name="searchtype"]').val();},
criteria: function(){return getSearchData();}
},
colNames:['Resource Name','Unit', 'Topic','Document Type','Content Type','Select'],
colModel :[
{name:'resourceName', index:'resourceName', width:380, align:'left'},
{name:'unit', index:'unitID', width:40, align:'center',sortable:true},
{name:'topic', index:'topicID', width:220, align:'center',sortable:true},
{name:'docType', index:'docTypeID', width:97, align:'center',sortable:true},
{name:'contentType', index:'contentTypeID', width:97, align:'center',sortable:true},
{name: 'select', index:'resourceID', width:55, align: "center", sortable: false, editable: true, edittype: "checkbox", editoptions: { value:"Yes:No", defaultValue:"No" }, formatter:"checkbox",formatoptions: {disabled : false}}
],
rowNum:20,
sortname: 'resourceName',
sortorder: 'asc',
viewrecords: true,
gridview: true,
width:878,
height:251,
loadComplete: function(data){
initCheckboxes();
$('input[type="checkbox"]').click(function(ev){
initCheckboxes();
});
}
});
The data loads just fine, however when I click the various column headers they don't sort. The loading box shows up briefly over the data, but the columns never actually re-sort. The only column that the sort works on is the very first column. Any help would be greatly appreciated.
The index property you set for other columns are different compared to the column name. When you sort a particular column jQGrid passes the value you set at the index as the sort parameter (sidx).
$("#searchlist").jqGrid({
...
colModel :[
{name:'resourceName', index:'resourceName', width:380, align:'left'},
{name:'unit', index:'unit', width:40, align:'center',sortable:true},
{name:'topic', index:'topic', width:220, align:'center',sortable:true},
{name:'docType', index:'docType', width:97, align:'center',sortable:true},
{name:'contentType', index:'contentType', width:97, align:'center',sortable:true},
{name: 'select', index:'select', width:55, align: "center", sortable: false, editable: true, edittype: "checkbox", editoptions: { value:"Yes:No", defaultValue:"No" }, formatter:"checkbox",formatoptions: {disabled : false}}
],
...
});

Resources