如何根据多列分组折叠和展开表格。
例如我有这样的表
---------------------------------------------------------------
location | size | cont_no | price | depot | cond |
---------------------------------------------------------------
USA | XX | 123 | 230 | SED | LK |
USA | YY | 343 | 330 | ASD | HK |
UAE | XX | 233 | 230 | SED | LK |
IND | ZZ | 123 | 230 | SAD | FK |
IND | XX | 213 | 430 | ASD | KK |
IND | YY | 433 | 870 | GFD | FK |
USA | YY | 865 | 230 | SED | LK |
UAE | XX | 976 | 430 | SED | HK |
USA | ZZ | 342 | 230 | CCD | HK |
UAE | XX | 132 | 445 | SED | KK |
UAE | ZZ | 064 | 323 | YYD | LK |
IND | YY | 452 | 130 | ITG | HK |
---------------------------------------------------------------
这就是我需要对上表进行分组的方法
-------------------------------
location | XX | YY | ZZ |
-------------------------------
UAE | 3 | 0 | 1 |
USA | 1 | 2 | 1 |
IND | 1 | 2 | 1 |
-------------------------------
我想基于位置和大小列进行分组,例如:美国有3个XX和0个YY和1个ZZ,
然后当我点击我要展开的行并显示那些3 XX和0 YY和1 ZZ其他四列cont_no,价格,仓库,cond
请有人帮助我或给我一些建议或链接以供参考。
谢谢
答案 0 :(得分:1)
可以按Row details示例中所示完成。
技巧是在向DataTables提供数据之前预处理数据并执行计算和用JavaScript分组。这取决于数据来自何处,静态表或Ajax请求。如果您在服务器上生成数据,也可以在服务器端完成。
基本上,JSON格式的结果数据可能如下所示。这将简化DataTables中子行的处理。
[
{
"location": "UAE",
"XX": 2,
"YY": 0,
"ZZ": 1,
"details": [
["UAE","XX","123","230","SED","LK"],
// more records for the same location
]
},
// more locations
]
答案 1 :(得分:1)
我认为这是你想要做的!
检查以下问题并回答
DataTables hidden row details example - the table header is misplaced (test case attached)
JSFIDDLE Sample 1
JSFIDDLE Sample 2
答案 2 :(得分:0)
您可以通过其他库来破解自己的方式。值得付出努力吗??
,或者您可以使用 Tabulator 。具有多列分组。
示例:
//load data as json
var tableData = [
{id:1, name:"Billy Bob", age:"12", gender:"male", height:1, col:"red", dob:"", cheese:1},
{id:2, name:"Mary May", age:"1", gender:"female", height:2, col:"blue", dob:"14/05/1982", cheese:true},
{id:3, name:"Christine Lobowski", age:"42", height:0, col:"green", dob:"22/05/1982", cheese:"true"},
{id:4, name:"Brendon Philips", age:"125", gender:"male", height:1, col:"orange", dob:"01/08/1980"},
{id:5, name:"Margret Marmajuke", age:"16", gender:"female", height:5, col:"yellow", dob:"31/01/1999"},
{id:6, name:"Billy Bob", age:"12", gender:"male", height:1, col:"red", dob:"", cheese:1},
]
var table = new Tabulator("#example-table", {
height:"311px",
layout:"fitColumns",
groupBy:function(data){
return data.gender + " - " + data.age; //groups by data and age
},
autoColumns:true,
});
table.setData(tableData);
<script src="https://unpkg.com/tabulator-tables@4.2.7/dist/js/tabulator.min.js"></script>
<link href="https://unpkg.com/tabulator-tables@4.2.7/dist/css/tabulator.min.css" rel="stylesheet"/>
<div id="example-table"></div>
唯一可以按多列进行分组的表库是制表符AFAIK。
这是其他表库。
-------- group by -------
single column | multi column
tabulator : yes | yes
bootstrap-table : yes | no
datatables.net : yes | no
dynatable.js : no | no
制表符具有bootstrap,简单的白色主题:
了解更多: