Larry
Larry

Reputation: 573

jqgrid search: How to specify search column?

I am using jqgrid on EF4 MVC3 (C#). I based search on this @Oleg 's solution, which works fine and fits for my needs.

I have the following columns defined in my grid:

          ...
          { name: 'Stato', index: 'StatoTicketID', width: 20, align: 'left', sorttype: 'int', searchoptions: { sopt: ['eq']} },
          { name: 'StatoTicketID', index: 'StatoTicketID', width: 20, align: 'left', sorttype: 'int', hidden: true, searchoptions: { sopt: ['eq']} },
          ...

As you can see, the column Stato is ordered by the index StatoTicketID (hidden integer field) and ordering works fine.

PROBLEM

When I try to search a value of Stato, the filter is passed on index StatoTicketID as string, while I'd like to search by Stato values. So I get an exception inside the controller which specifies that I cannot convert String type to Int32.

Does exist a way to specify on which column apply search, when index is on a different column, like in my case?

EDIT & WORKAROUND: For now I solved my problem with the following workaround.

        (inside foreach (Rule rule in rules) of FilterObjectSet by Oleg)
         ....

         if (rule.field == "StatoTicketID")
            {
                rule.field = "StatoTicket.Stato";
                propertyInfo = typeof(T).GetProperty("stringfield"); // where stringfield is a text type column of my model
            }

I realize very well that is not an elegant solution, I expect a kind response by you, to know how to implement the required behaviour directly from jqGrid, please.

Thanks in advance

Upvotes: 0

Views: 1170

Answers (1)

Oleg
Oleg

Reputation: 222017

It seems to me that you chosen too complex way. I would just send to the client (to the jqGrid) only the Stato and have to StatoTicketID at all. From the design point of your the StatoTicketID is the part of the server implementation and the client should not depend from this.

If you have Unique Constrain (or unique index) on Stato column of the StatoTickets table you would very quickly find the StatoTicketID whenever you as need. So the jqGrid can contain and "know" only about Stato and have no information about the StatoTicketID as the implementation detail.

One more way to solve the problem is the usage of formatter: 'select' in the column Stato. It's important to understand that in the case the mapping between Stato text and the StatoTicketID should be loaded before the grid is created. In the case the Stato column should have the properties like

formatter: 'select', edittype: 'select', editoptions: {value: '12:Stato1;24:Stato2'},
stype: 'select', searchoptions: {value: ':All;12:Stato1;24:Stato2'}

One can't use dataUrl in the case instead of value in the editoptions.

As the result you will be able to fill column Stato with the StatoTicketID data, but the corresponding texts will displayed by jqGrid.

I recommend you better to implement the first way with pure Stato text. All the problems you would be able to solve only on the server part.

Upvotes: 3

Related Questions