0

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

Community
  • 1
  • 1
Larry
  • 573
  • 5
  • 14
  • 31

1 Answers1

3

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you very much Oleg! I'll try the second way. I understand and I share your concerns regarding the solution I adopted, probably because I have not specified a detail: `StatoTicketID` allows me to place an order on the basis of 'priority' given to the `Stato` field, so a pure text sort order is no good in my case. I am going to try the second way: I think it's the best solution in my case. I'll keep you updated! Thank you very much! – Larry Feb 16 '12 at 09:33
  • @Larry: You are welcome! If you use `datatype: 'json'` without `loadonce: true` you implement the sorting of the server side so you can continue to use `name: 'Stato', index: 'StatoTicketID'` even after the usage of pure text values in the `'Stato'` column. If you will choose the second way you will be careful with the data which will be posted to the server in case of editing the `'Stato'` column. Let me know whether you will implement all till the end solution. – Oleg Feb 16 '12 at 09:44
  • THX. I tried to implement the first solution, but it still does not work. I already used datatype: 'json' and loadonce: false...pheraps must I modify your solution? I am trying the second one...I'll keep you updated, THX! – Larry Feb 16 '12 at 09:58
  • THX Oleg! It works fine now! I followed your precious hints. I implemented as you suggested, changing the static text with dynamic one, using `surl`. THX! – Larry Feb 16 '12 at 19:29
  • 1
    @Larry: You are welcome! I am glad to know that the problem is solved now. – Oleg Feb 16 '12 at 19:32