Search in the middle of a column by default in jqGrid with toolbar search
Asked Answered
N

3

4

After reading the jqGrid wiki (and taking example from: Case insensitive search in jqGrid including hidden fields), I cannot find what I want to do.

Is there any search option to enable a search anywhere in a column (automatically wildcarded).

If the column contains "Apple Iphone" I would be able to find it by using the search "iphone".

The SQL equivalent would be select * from table where lower(columnX) like '%iphone%';

Nardi answered 31/3, 2011 at 12:42 Comment(2)
Could you include in your question more information? For example: do you use local searching (datatype:local or loadonce:true) or you implement all on the server? Which searching you use: Toolbar searching, Single Value searching or Advanced searching? Do you need to implement searching on hidden column?Glassworks
Hello, I use the toolbar search, without hidden fields. I implement all in the html part; I do no have any web-server as I do a simple-to-use xml-to-html formatting. If I understand properly, there is not a simple parameter to set but a function to implement from scratch. Thanks for your support.Anglophile
G
17

Since you use toolbar searching the solution of your problem seems to be simple. You should:

  1. include ignoreCase:true to the jqGrid parameters
  2. include defaultSearch:'cn' option to the call of filterToolbar. For example: $("#list").jqGrid('filterToolbar', {defaultSearch:'cn'}).
  3. If you use any select elements in the searching toolbar (stype:'select') you should include in the list of searchoptions the sopt options which begin with 'eq': stype:'select', searchoptions: {sopt:['eq','ne']} for example.
Glassworks answered 4/4, 2011 at 11:32 Comment(13)
the missing parameter was your second point. Thanks for the clarification and solution.Anglophile
I am using datatype: local. $("#myGrid").jqGrid('filterToolbar', {defaultSearch:'cn'}) doesnt work. Does it work only for json?Gayomart
@RustinCohle: It should work. If it doesn't work in your code then you have some another problem. You should post more full code which you use or better the demo, which reproduces your problem.Glassworks
@Glassworks thanks. It works, my bad. I have searched through various forms. For some reason I cant post a question myself. I have two issues: 1: with local datatype, while searching on a date column with filter toolbar, jqgrid takes Y-m-d format default, my format is m/d/Y, jqgrid cant recognize it. 2: I have used a custom formatter to change a boolean column to Yes/No, now on searching via filter toolbar, it searches for database value instead of formatted text. Please help. Thanks.Gayomart
I am using Tony's 4.6.0 forkGayomart
@RustinCohle: It's important to distinguish the format of input data (in the input data parameter) from the format of displayed data. One uses typically formatter and formatoptions to customize the format of displayed data. It's important to post the definition of colModel for the column with the date and to post an example of input data (which you use in the input data parameter). Which options of filterToolbar you use? In case of custom formatter one should don't forget to define unformat function too and to define searchoptions with sopt, which starts from "eq".Glassworks
@Glassworks Thanks. For date Column I am using newformat as m\d\Y and formatter as date to customize displayed data. and sorttype as date. I am sorry, I am unable to post a sample. In filter toolbar only two options searchonenter: false and default filter as cn. Now filter toolbar searches only Y-m-d format.Gayomart
@RustinCohle: You should include searchoptions: { sopt: ["eq", ...] } because you don't want to use "cn" as the search operation in the column. Moreover you should not explain what you use, but just include exact column definition. It's better to create the demo on jsfiddle.netGlassworks
@Glassworks Thanks. Here in this jsfiddle.net/ejnrtocw/63 on searching the date column, I am using cn, its still looking for Y-m-d though. backslashes are not recognized.Gayomart
@Glassworks I am looking for both cn and eq in searching date.Gayomart
@RustinCohle: Sorry, but I wrote you multiple times about the importance of the usage of searchoptions: { sopt: ["eq", ...] }. Simple adding of searchoptions: { sopt: ["eq"] } in your demo solves the problem. See jsfiddle.net/ejnrtocw/64Glassworks
@Glassworks Thankyou. I know. but I want the 'contains' search as well. Suppose user want to search by year alone. if he types 2016, it should list all 2016 year dates. I want both eq and cn.Gayomart
@RustinCohle: You can't do this in the old jqGrid 4.6.0. The old version have fixed code, which generates filtering statements. The code for Date type and 'contains' operation is not correct like you can see yourself. I implemented custom filtering in free jqGrid fork. See the wiki article for more details. The demo created for the issue provides the solution, which you can easy modify to youth.Glassworks
L
1
$(document).ready(function() {
  colNamesData = [ 'Description']

  {name:'description',index:'description', width:130, sorttype:"text", search:true, editable:true, edittype:"textarea", editoptions: {rows:"5",cols:"25",maxlength:"255"}, stype:'text', searchoptions:{sopt:['cn', 'nc', 'bw', 'bn', 'ew', 'en']}},

$("#description_table").jqGrid({
      datatype: "local", 
      height: "auto",
      autowidth: true,
      ignoreCase: true,
      colNames: colNamesData, 
      colModel: colModelHash,
      pager: '#pager',
      rowNum:10,
      rowList:[10,25,50,100],
      sortname: 'date',
      sortorder: 'desc',
      viewrecords: true,
      editurl:"/url_name.json", 
      caption: 'Description'
 data:<%= raw @jqgrid_table.to_json %>
   });

   jQuery("#description_table").jqGrid('navGrid','#pager',{del:false,add:true,edit:false},{}, {modal: true,afterSubmit:processAddEdit,recreateForm:true, afterComplete:reloadJqGrid}, {modal: true}, {multipleSearch:true});  

Now if your text contains "here i go" and if you search "go", it will surely search, it works for me.

Give a try and reply if it doesn't.

Lantana answered 31/3, 2011 at 13:15 Comment(0)
P
1
$("#list").jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "cn"});

In the above example the **defaultSearch : "cn"** is used to search using any substring of item you want to search. Removing defaultSearch : "cn" starts search beginning with the substring.

Papaya answered 13/5, 2013 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.