Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

struts2 - jqGrid: conditionally hide / show column content **per row**

Is there any way to target a column in a jqGrid (in my case, Struts2-jQuery-Grid Plugin 3.7.0) on certain rows only ?

For example, I want to show the content of the 2nd column only if the 1th column value is Movie:

 _______________________________________________________
|       COL 1       |               COL 2               |
|===================|===================================|
|   Movie           | bla bla yada yada                 |
|-------------------|-----------------------------------|
|   Song            |                                   |
|-------------------|-----------------------------------|
|   Clip            |                                   |
|-------------------|-----------------------------------|
|   Movie           | foo or bar that is the question...|
|-------------------|-----------------------------------|
|   Game            |                                   |
|___________________|___________________________________|

I've tried to use conditional OGNL expressions in hidden and cssClass fields of the GridColumnTag, but immediately noticed they evaluate once against the whole column, not at every "iteration".

As a workaround, I can manually hide the columns in that rows with javascript after the grid is populated, but that is a hack, and I'm wondering if there is a clean solution to conditionally "do something" on a columns per-row.

NOTE: I can't simply erase the content from the source List (as it would be obvious) because COL 2 in my real case is a Boolean, represented as checkbox.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are many ways how you can implement the requirement. One of the most easy is the setting of color: transparent; CSS on the cells which you need to hide. For example you define the CSS rule

.hiddenCell { color: transparent; }

and you assign the class hiddenCell to specified cells of the "COL 2". You can use cellattr property of "COL 2" for it:

cellattr: function (rowId, val, item) {
    if (item.sent) {
        return " class='hiddenCell'";
    }
}

The demo demonstrates the approach. Disadvantage of the approach - the hidden text still exist on HTML page and so one can examine it if required.

Another way is the usage of custom formatters. For example you can define the following formatter callback

formatter: function (cellValue, options, item) {
    return item.sent ? "" : $.jgrid.htmlEncode(cellValue);
}

The demo demonstrates the second approach. Disadvantage of the approach - it's a little difficult to combine usage of custom formatter with another formatter (like formatter: "select" in the example above). Nevertheless one can do it too:

formatter: function (cellValue, options, item, action) {
    return item.sent ?
        "" :
        $.fn.fmatter.call(
            this,
            "select",
            cellValue,
            options,
            item,
            action);
}

like the next demo.

If you loads the data from the server then probably the best choice would be to modify the input data (the input data for the column "COL 2") inside of beforeProcessing callback. See the answer or this one for code examples.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...