I need to set the default column selection on a jqGrid single-search dialog.
The options available are described on the jqGrid wiki
To set the default search "type" option, I re-ordered the "sopt" array with the value I need ("contains", "cn") first in the array and set this on the navGrid
search options. Despite browsing the source code I have not been able to work out which property might affect the initial field selection. It always defaults to the first column in my colModel
.
My code is:
$('#tableid').jqGrid({
colNames: ['ID', 'Membership#', 'Join Date', 'Email', 'Name', 'Address', 'Postcode'],
colModel: [
{name:'ID', index:'ID', hidden:true },
{name:'MEMID', index:'MEMD', width:90 },
{name:'JOINDATE', index:'JOINDATE', width:70 },
{name:'EMAIL', index:'EMAIL', width:150, align:"right" },
{name:'NAME', index:'NAME', width:120, align:"right" },
{name:'ADDRESS', index:'ADDRESS', width:250, align:"right" },
{name:'POSTCODE', index:'POSTCODE', width:80, align:"right" }
],
// etc. ...
});
$("#tableid").jqGrid('navGrid', '#pager',
{ /* parameters */
edit:false, add:false, del:false, searchtext:'Find ', refreshtext:'Refresh '
},
{ /* edit options */ },
{ /* add options */ },
{ /* delete options */ },
{ /* search options */
multipleSearch:false,
multipleGroup:false,
showQuery:false,
top: 190,
left: 200,
caption: "Search for members...",
closeAfterSearch: false,
sopt: ['cn','nc','eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en'],
},
{ /* view options */ }
);
When the user clicks on "Find" I would like the initial default search dialog to be presented with "Name", "contains" selected.
It's a good question! jqGrid contains the option
columns
which can be used to implement your requirements, but the usage of the option is not simple. So I made the demo for you.The option
columns
of searching dialog is not documented probably because it's not really user friendly. The optioncolumns
can contains array of items ofcolModel
. Exactly the items in the same order will be used in construction of the drop-down select with column names. Per default jqGrid fillcolumns
with all items ofcolModel
which don't havesearch: false
property. For hidden columns (havinghidden: true
) it will be tested additionallysearchoptions.searchhidden
property (see the part of the source code). So the optioncolumns
will be filled internally per default. On the other side one can overwrite the optioncolumns
to have custom order of searching fields.The code which you included in the text of your question produced the following searching dialog
After filling option
columns
you can change it to for example the followingThe corresponding demo is here. The most important parts of the code are below
UPDATED: There are small bug in Single Value Searching (
multipleSearch: true
not set) and setting ofcolumns
option. In the answer I describe how the bug can be fixed. Alternatively you can usemultipleSearch: true
option and specifyfilters
with default searching rule inpostData
(see the same answer).