jQuery Jqgrid - 对行级数据进行分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12200621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jqgrid - grouping row level data
提问by varaprakash
With jqgrid, is it possible to group the row level data as in the attached image? Basically I wanted to split the data for a particular row into multiple rows from certain columns onwards..
使用jqgrid,是否可以像附加图像中那样对行级数据进行分组?基本上我想将特定行的数据从某些列开始分成多行..
Example
例子
回答by Oleg
I suggest you to use cellattr
to set rowspan
attribute on some cells or set style="display:none"
to hide another unneeded cells. The idea is the same as with colspan
from the answer.
我建议您使用cellattr
设置rowspan
某些单元格的属性或设置style="display:none"
隐藏其他不需要的单元格。这个想法与colspan
来自答案的相同。
As the result you can create the following grid (see the demo)
因此,您可以创建以下网格(请参阅演示)
or another one (see another demo)
或另一个(见另一个演示)
The problem with the grids is in another jqGrid features like sorting, paging, hovering and selection. Some from the features one can implement with additional efforts, but another one are more difficult to implement.
网格的问题在于另一个 jqGrid 功能,如排序、分页、悬停和选择。有些功能可以通过额外的努力实现,但另一些则更难实现。
The code which I used in the demo is the following:
我在演示中使用的代码如下:
var mydata = [
{ id: "1", country: "USA", state: "Texas", city: "Houston", attraction: "NASA", zip: "77058", attr: {country: {rowspan: "5"}, state: {rowspan: "5"}} },
{ id: "2", country: "USA", state: "Texas", city: "Austin", attraction: "6th street", zip: "78704", attr: {country: {display: "none"}, state: {display: "none"}} },
{ id: "3", country: "USA", state: "Texas", city: "Arlinton", attraction: "Cowboys Stadium", zip: "76011", attr: {country: {display: "none"}, state: {display: "none"}} },
{ id: "4", country: "USA", state: "Texas", city: "Plano", attraction: "XYZ place", zip: "54643", attr: {country: {display: "none"}, state: {display: "none"}} },
{ id: "5", country: "USA", state: "Texas", city: "Dallas", attraction: "Reunion tower", zip: "12323", attr: {country: {display: "none"}, state: {display: "none"}} },
{ id: "6", country: "USA", state: "California", city: "Los Angeles", attraction: "Hollywood", zip: "65456", attr: {country: {rowspan: "4"}, state: {rowspan: "4"}} },
{ id: "7", country: "USA", state: "California", city: "San Francisco", attraction: "Golden Gate bridge", zip: "94129", attr: {country: {display: "none"}, state: {display: "none"}} },
{ id: "8", country: "USA", state: "California", city: "San Diego", attraction: "See world", zip: "56653", attr: {country: {display: "none"}, state: {display: "none"}} },
{ id: "9", country: "USA", state: "California", city: "Anaheim", attraction: "Disneyworld", zip: "92802", attr: {country: {display: "none"}, state: {display: "none"}} }
],
arrtSetting = function (rowId, val, rawObject, cm) {
var attr = rawObject.attr[cm.name], result;
if (attr.rowspan) {
result = ' rowspan=' + '"' + attr.rowspan + '"';
} else if (attr.display) {
result = ' style="display:' + attr.display + '"';
}
return result;
};
$("#list").jqGrid({
datatype: 'local',
data: mydata,
colNames: ['Country', 'State', 'City', 'Attraction', 'Zip code'],
colModel: [
{ name: 'country', width: 70, align: 'center', cellattr: arrtSetting },
{ name: 'state', width: 80, align: 'center', cellattr: arrtSetting },
{ name: 'city', width: 90 },
{ name: 'attraction', width: 120 },
{ name: 'zip', index: 'tax', width: 60, align: 'right' }
],
cmTemplate: {sortable: false},
rowNum: 100,
gridview: true,
hoverrows: false,
autoencode: true,
ignoreCase: true,
viewrecords: true,
height: '100%',
caption: 'Grid with rowSpan attributes',
beforeSelectRow: function () {
return false;
}
});
I used in the above code additional attr
property placed together with the input data. It's just an example. I wanted to make the implementation of cellattr
function more simple. You can use the same idea and to place the information needed for cellattr
in any other format.
我在上面的代码中使用了attr
与输入数据放在一起的附加属性。这只是一个例子。我想让cellattr
函数的实现更简单。您可以使用相同的想法并以cellattr
任何其他格式放置所需的信息。
回答by pistipanko
This is my solution for JSON data:
这是我对 JSON 数据的解决方案:
var prevCellVal = { cellId: undefined, value: undefined };
$("#list").jqGrid({
url: 'your WS url'
datatype: 'json',
mtype: "POST",
ajaxGridOptions: {
contentType: "application/json"
},
colNames: ['Country', 'State', 'City', 'Attraction', 'Zip code'],
colModel: [
{ name: 'country', width: 70, align: 'center',
cellattr: function (rowId, val, rawObject, cm, rdata) {
var result;
if (prevCellVal.value == val) {
result = ' style="display: none" rowspanid="' + prevCellVal.cellId + '"';
}
else {
var cellId = this.id + '_row_' + rowId + '_' + cm.name;
result = ' rowspan="1" id="' + cellId + '"';
prevCellVal = { cellId: cellId, value: val };
}
return result;
}
},
{ name: 'state', width: 80, align: 'center' },
{ name: 'city', width: 90 },
{ name: 'attraction', width: 120 },
{ name: 'zip', index: 'tax', width: 60, align: 'right' }
],
cmTemplate: {sortable: false},
rowNum: 100,
gridview: true,
hoverrows: false,
autoencode: true,
ignoreCase: true,
viewrecords: true,
height: '100%',
caption: 'Grid with rowSpan attributes',
beforeSelectRow: function () {
return false;
},
gridComplete: function () {
var grid = this;
$('td[rowspan="1"]', grid).each(function () {
var spans = $('td[rowspanid="' + this.id + '"]', grid).length + 1;
if (spans > 1) {
$(this).attr('rowspan', spans);
}
});
}
});
This example is for a single column, but with few corrections it can be used also for multiple columns.
这个例子是针对单列的,但经过很少的修正,它也可以用于多列。
回答by Zorkind
Hey there "pistipanko"
嘿,“pistipanko”
I have made a change in your solution, i think it worked better.
我对您的解决方案进行了更改,我认为效果更好。
cellattr: function(rowId, val, rawObject, cm, rdata) {
var result;
var cellId = this.id + '_row_' + rawObject[3] + grid.getGridParam('page');
if (prevCellVal.cellId == cellId) {
result = ' style="display: none"';
}
else {
result = ' rowspan="' + rawObject[6] + '"';
prevCellVal = { cellId: cellId, value: rawObject[3] };
}
return result;
}
I am making the grouping with the value of another collumn that's why the rawObject[3]
And i am using a rowspan value returned from the application in the rawObject[6]
我正在使用另一个列的值进行分组,这就是为什么rawObject[3]
我使用从应用程序返回的 rowspan 值rawObject[6]
Works great.
效果很好。
Hope it helps :)
希望能帮助到你 :)