JqGrid - Using formatDisplayField Option

2019-03-06 16:06发布

问题:

I'm with a problem using jqgrid with some options, better that write is show you an image:

So, when I'm construction the table body (with ajax call) I'm passing an hidden field. When something is grouped, I want to show that hidden field where 'undefined' word is (group title).

Is there any solution using formatDisplayField?

What I have is something like:

groupingView : {
                    groupField : ['cpv'],
                    groupCollapse : true,
                    groupOrder: ['desc'],
                    plusicon: 'ui-icon-circle-plus',
                    minusicon: 'ui-icon-circle-minus',
                    formatDisplayField: [
                    function (value) { // Should be cpv_parent (hidden by default but sent to jggrid when instantiate)
console.log(value); // Contain CPV Grouped
console.log($(this)); // Contain [table#ajaxTable.ajaxTable.ui-jqgrid-btable, context: table#ajaxTable.ajaxTable.ui-jqgrid-btable, constructor: function, init: function, selector: "", jquery: "1.7.2"…]
                        //return String(displayValue).substring(0, 5);
                    }
                    ],
                    isInTheSameGroup: function (x, y) {
                        return String(x).substring(0, 5) === String(y).substring(0, 5);
                    }
                }

EDIT As required, heres a sample data form what i'm using (from last try (using userData)):

{"total":1,"page":1,"totalrecords":3,"userdata":{"98513295":"98000000-3"},"rows":[{"tipoConcurso":"Ajuste Directo (Regime Geral)","createdOn":"2014-04-23 16:19:56","valor":15000,"cpv":98513295,"cpvParent":"98000000-3"},{"tipoConcurso":"Ajuste Directo (Regime Geral)","createdOn":"2013-10-01 16:05:08","valor":15000,"cpv":98513295,"cpvParent":"98000000-3"},{"tipoConcurso":"Ajuste Directo (Regime Geral)","createdOn":"2013-09-03 17:34:39","valor":15000,"cpv":98513295,"cpvParent":"98000000-3"}]}

Thanks @Oleg :)

Thank you all in advance :)

Regards, Marcelo

回答1:

It's a little difficult to understand your question because you display some abstract numbers only. I understand you so. You make grouping by cpv filed, but you need to display another field cpvParent which can be get based on the cpv value. One need just to have a map which get cpvParent from cpv. I don't recommend to add any hidden columns which are more expensive.

So I suggest that you change the data

{
    "total": 1,
    "page": 1,
    "totalrecords": 3,
    "rows": [
        {
            "tipoConcurso": "Ajuste Directo (Regime Geral)",
            "createdOn": "2014-04-23 16:19:56",
            "valor": 15000,
            "cpv": 98513295,
            "cpvParent": "98000000-3"
        },
        {
            "tipoConcurso": "Ajuste Directo (Regime Geral)",
            "createdOn": "2013-10-01 16:05:08",
            "valor": 15000,
            "cpv": 98513295,
            "cpvParent": "98000000-3"
        },
        {
            "tipoConcurso": "Ajuste Directo (Regime Geral)",
            "createdOn": "2013-09-03 17:34:39",
            "valor": 15000,
            "cpv": 98513295,
            "cpvParent": "98000000-3"
        }
    ]
}

to the following:

{
    "total": 1,
    "page": 1,
    "totalrecords": 3,
    "userdata": {
        "98513295": "98000000-3",
        "97123456": "97000000-2"
    }
    "rows": [
        {
            "tipoConcurso": "Ajuste Directo (Regime Geral)",
            "createdOn": "2014-04-23 16:19:56",
            "valor": 15000,
            "cpv": 97123456,
            "cpvParent": "98000000-3"
        },
        {
            "tipoConcurso": "Ajuste Directo (Regime Geral)",
            "createdOn": "2013-10-01 16:05:08",
            "valor": 15000,
            "cpv": 98513295
        },
        {
            "tipoConcurso": "Ajuste Directo (Regime Geral)",
            "createdOn": "2013-09-03 17:34:39",
            "valor": 15000,
            "cpv": 98513295
        }
    ]
}

Inside of formatDisplayField callback you can get userData parameter (the name of parameter is userData, but the JSON data have to have userdata in other case). Now the userData[value] will get you "98000000-3" by key "98513295":

groupingView: {
    groupField: ["cpv"],
    ...
    formatDisplayField: [
        function (value) {
            var userData = $(this).jqGrid("getGridParam", "userData");
            return userData[value];
        }
    ]
}

Such way will work quickly and you need just prepare the data on the server side in the above format.