jqGrid: How to load only rows that an attribute is set to true

后端 未结 2 883
没有蜡笔的小新
没有蜡笔的小新 2020-12-21 19:49

I have a JSON Object as the following:

{
  \"rows\": [
  {
    \"id\":1,
    \"name\": \"Peter\",
    \"hasData\": true,
  },
  {
    \"id\":2,
    \"name\":         


        
相关标签:
2条回答
  • 2020-12-21 20:41

    I suppose that you load the data from the server using datatype: "json" in combination with loadonce: true option. The solution is very easy if you use free jqGrid fork of jqGrid. Free jqGrid allows to sort and to filter the data, returned from the server, before displaying the first page of data. One need to add forceClientSorting: true to force the applying the actions by jqGrid and postData.filters with the filter, which you need, and the option search: true to apply the filter:

    $("#grid").jqGrid({
        ...
        datatype: "json",
        postData: {
            // the filters property is the filter, which need be applied
            // to the data loaded from the server
            filters: JSON.stringify({
                groupOp: "AND",
                groups: [],
                rules: [{field: "hasData", op: "eq", data: "true"}]
            })
        },
        loadonce: true,
        forceClientSorting: true,
        search: true,
        // to be able to use "hasData" property in the filter one has to
        // include "hasData" column in colModel or in additionalProperties
        additionalProperties: ["hasData"],
        ...
    });
    

    See the demo https://jsfiddle.net/OlegKi/epcz4ptq/, which demonstrate it. The demo uses Echo service of JSFiddle to simulate server response.

    0 讨论(0)
  • 2020-12-21 20:42

    I can recommend you another solution (in case you use Guriddo jqGrid) , which can be used with any datatype and any settings. The idea is to use beforeProcessing event to filter the needed data.

    For this purpose we assume that the data is like described from you. Here is the code:

    $("#grid").jqGrid({
    ...
            beforeProcessing : function (data, st, xhr) {
    
                var test= data.rows.filter(function (row) {
                    if(row.hasData == true ) { // true is not needed but for demo
                        return true;
                    } else {
                        return false;
                    }
                });
                data.rows = test;
                return true;
            }
    ...
    });
    

    I suppose the script will work in free jqGrid in case you use them

    0 讨论(0)
提交回复
热议问题