【问题标题】:Jquery datatable present detailsJquery 数据表显示详细信息
【发布时间】:2017-12-16 21:42:30
【问题描述】:

我有一个由具有详细信息的 JSON 返回驱动的 jQuery DataTable,它单击行并展开从我的 JSON 加载列表的详细信息模板,为此我遵循 example

我想知道如何在不隐藏的情况下加载detail,就好像它是datatable行的序列一样,不能有展开和折叠的选项,detail必须始终可见

Column1| Column2| Column3|
Value1   Value2   Value3
  Detail1| Detail2| Detail3|
  ValueD1   ValueD2   ValueD3 

更新 - JSON

$scope.list = [
            {
                id:1,
                name: "Ze",
                listDetail: [
                    {
                        id:1,
                        description: "lt"
                    },
                    {
                        id:2,
                        description: "lt 3"
                    },
                ]
            },
            {
                id:2,
                name: "Ze 2",
                listDetail: [
                    {
                        id:3,
                        description: "lt 1"
                    },
                    {
                        id:4,
                        description: "lt 4"
                    },
                ]
            }
        ];

【问题讨论】:

  • 我会说你做了一个普通的数据表,但只是为了你的详细行,你向 td 添加一个属性:colspan=nbOfColumn 并将你的详细信息放在 td 中。
  • 我开始研究这个 angularjs、jquery 等世界,我尝试搜索colspan=nbOfColumn。但我没有找到任何可以帮助我的具体内容,你有任何例子吗?
  • 你能给我一个你的 JSON 的例子吗?你在服务器端工作吗?你是生成 JSON 的人吗?
  • 我编辑了帖子并添加了 JSON 返回。是的,这个 JSON 值来自服务器
  • 每个普通行下的明细行数是固定的吗?

标签: javascript jquery angularjs json datatable


【解决方案1】:

在这里,它应该可以工作:

这里是一个应该使用这样的 JSON 的解决方案:

  • json

{ "draw": 2, "recordsTotal": 3, "recordsFiltered": 3, "data": [{ "type": "normal", "description": "Zenaida", "name": "Frank", "age": "Software Engineer", "location": "New York" }, { "type": "detail", "detail": "first detail" }, { "type": "detail", "detail": "second detail" }, { "type": "detail", "detail": "third detail" }, { "type": "normal", "description": "zerrz", "name": "paul", "age": "Software breaker", "location": "Lodon" }, { "type": "detail", "detail": "third detail" }, { "type": "normal", "description": "Zenaida", "name": "Frank", "age": "Software Engineer", "location": "New York" }, { "type": "detail", "detail": "second detail" }, { "type": "detail", "detail": "third detail" }] }

  • js

$('#example').dataTable({ "processing": true, "serverSide": true, "ajax": "YourAjaxSource", "columns": [ //You have to manually bind your column since the first value is not to be displayed { "data": "details" }, { "data": "description" }, { "data": "name" }, { "data": "age" }, { "data": "location" } ], "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { switch (aData[0]) { //first value indicates if its a detail row or a normal row case 'normal': // if its a normal row, just display the cells without the first detail one $(nRow).find('td')[0].style.display = 'none'; break; case 'detail': //else display none of the cells except the detail one with take up 5 cells $(nRow).find('td')[1].style.display = 'none'; $(nRow).find('td')[2].style.display = 'none'; $(nRow).find('td')[3].style.display = 'none'; $(nRow).find('td')[4].style.display = 'none'; $(nRow).find('td')[0].colSpan = '5'; //Your number of column break; } } });

它的工作方式是,从技术上讲,每一行都有细节 + 正常单元格。

绘制时检查每一行的行类型,并仅显示所需的单元格(因为其他单元格是空的)

最后对于detail行我给了一个属性colspan的单元格数来允许它取行的所有宽度。

我还没有测试过,所以 gl&hf ;) 如果您有任何问题,请随时发表评论。

并根据您的需要进行调整。

【讨论】:

  • 感谢您的回答,我一直倾向于按照您的示例进行操作,但没有成功。从您的示例中可以看出,JSON 返回与我的 JSON 返回有些不同,我在另一个列表中有一个列表,这意味着我的主要内容包含一个包含详细内容的列表。你给我示例的方式我无法弄清楚如何使它工作,因为你的 JSON 格式与我的不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 2015-12-06
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 2016-10-25
  • 1970-01-01
相关资源
最近更新 更多