Lorenzo
Lorenzo

Reputation: 29427

jqgrid Search with complex objects

my colModel object is a json serialized object that represent the following MovingRecord class

public class CompanyData {
    public string Code { get; set; }
    public string Segment { get; set; }
    public string CompetenceArea { get; set; }
}

public class MovingRecord {
    public int MovingRecordID { get; set; }
    public CompanyData FromCompanyData { get; set; }
    public CompanyData ToCompanyData { get; set; }
    public float FTE { get; set; }
}

In the jqGrid configuration I have the following colModel:

colModel: [
    { name: 'FromCompanyData.Code', index: 'FromCompanyData.Code', width: 70, sortable: false, align: 'left', search: true, template: colTextTemplate },
    { name: 'FromCompanyData.Segment', index: 'FromCompanyData.Segment', width: 170, sortable: false, align: 'left', search: true, template: colTextTemplate },
    { name: 'FromCompanyData.CompetenceArea', index: 'FromCompanyData.CompetenceArea', width: 170, sortable: false, align: 'left', search: true, template: colTextTemplate },
    { name: 'ToCompanyData.Code', index: 'ToCompanyData.Code', width: 70, sortable: false, align: 'left', search: true, template: colTextTemplate },
    { name: 'ToCompanyData.Segment', index: 'ToCompanyData.Segment', width: 170, sortable: false, align: 'left', search: true, template: colTextTemplate },
    { name: 'ToCompanyData.CompetenceArea', index: 'ToCompanyData.CompetenceArea', width: 170, sortable: false, align: 'left', search: true, template: colTextTemplate },
    { name: 'FTE', index: 'FTE', width: 60, sortable: false, align: 'right', search: true, formatter: 'number', template: colFloatTemplate }
],

This works exactly as expected. Unfortunately the problem is when I try to search on those field. Because of the nature of the record itself the members FromCompanyData/ToCompanyData can be nullable objects.

The search is activated with the following code:

grid.jqGrid('navButtonAdd', '#' + pagerID, {
    caption: "", buttonicon: "ui-icon-search", position: "last", title: "Advanced search filters",
    onClickButton: function () {
        grid.jqGrid('searchGrid', 
            { sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge', 'bw', 'cn'], closeOnEscape: true, multipleSearch: true, closeAfterSearch: true, recreateFilter: true });
    }
});

when I try to search I always receive the following error "{Field Name} : the field is required".

Any help on how to solve this problem? Thanks

EDIT: As per Oleg comment, this is the definition of colTextTemplate

var colTextTemplate = {
    sorttype: 'text',
    align: 'left',
    search: true,
    stype: 'text',
    searchoptions: { sopt: ['eq', 'ne', 'bw', 'cn'] },
    searchrules: { required: true }
};

I have tried to remove the required: true option and experienced a different behaviour. If I remove this option the original error disappear but the built filter does not include the data field even if I fill it with some text. For example, the filter is like the following one

{
    "_search":true,
    "nd":1391184286612,
    "rows":9999,
    "page":1,
    "sidx":"MovingRecordID",
    "sord":"asc",
    "filters":"{\"groupOp\":\"AND\",\"rules\":[{\"field\":\"FromCompanyData.Code\",\"op\":\"eq\",\"data\":\"\"}]}","searchField":"","searchString":"","searchOper":""}

Upvotes: 0

Views: 498

Answers (1)

Oleg
Oleg

Reputation: 221997

The last update makes all still not quite clear for me. I think that you should remove required: true property from searchrules (or set to required: false). The problem with empty data seems to me another problem. It's a bug in the current implementation of jqGrid. I posted here my suggetion to fix the problem. Here are the most important parts of required changes. The main idea is: triggering of change event (see the line) before usage of the data from the searching dialog. setTimeout should be removed additionally (see here) to use the current data from the searching dialog. The referenced pull request is merged to the main code of jqGrid on github about two months before, but the last released version 4.5.4 contains still the bug. I recommend you to make described modifications in your copy of jquery.jqGrid.src.js. I hope it will solve your problem.

Upvotes: 1

Related Questions