gpt4 book ai didi

javascript - 使用动态列填充 jquery 数据表

转载 作者:行者123 更新时间:2023-12-02 23:17:24 26 4
gpt4 key购买 nike

我正在使用数据表,我想用动态列填充表。列将根据“顺序”属性显示。行通过属性“Id_Col”映射到列,“RowIdx”对应于行的索引。我想填充数据表选项:“列”和“数据”我的 json 对象如下:

希望得到您的帮助。预先感谢您!

{
"Col": [
{
"Id_Col": 1,
"Col_Name": "Col 1",
"Order": 1
},
{
"Id_Col": 2,
"Col_Name": "Col 2",
"Order": 2
},
{
"Id_Col": 3,
"Col_Name": "Col 3",
"Order": 3
},
{
"Id_Col": 4,
"Col_Name": "Col 4",
"Order": 4
}
],
"Row": [
{
"Id_Col": 1,
"RowIdex": 1,
"Val": "Row 1 col 1"
},
{
"Id_Col": 2,
"RowIdex": 1,
"Val": "Row 1 col 2"
},
{
"Id_Col": 3,
"RowIdex": 1,
"Val": "Row 1 col 3"
},
{
"Id_Col": 4,
"RowIdex": 1,
"Val": "Row 1 col 4"
},
{
"Id_Col": 1,
"RowIdex": 2,
"Val": "Row 2 col 1"
},
{
"Id_Col": 2,
"RowIdex": 2,
"Val": "Row 2 col 2"
},
{
"Id_Col": 3,
"RowIdex": 2,
"Val": "Row 2 col 3"
},
{
"Id_Col": 4,
"RowIdex": 2,
"Val": "Row 3 col 4"
},
{
"Id_Col": 1,
"RowIdex": 3,
"Val": "Row 3 col 1"
},
{
"Id_Col": 2,
"RowIdex": 3,
"Val": "Row 3 col 1"
},
{
"Id_Col": 3,
"RowIdex": 3,
"Val": "Row 3 col 2"
},
{
"Id_Col": 4,
"RowIdex": 3,
"Val": "Row 3 col 3"
}
]
}

最佳答案

如果Order是列位置,Id_Col是列引用,我们可以映射这些Col和Row对象来重新排序和重构以满足Datatable对象/数组源。

您可以引用这里: https://datatables.net/examples/data_sources/js_array.html

请随意运行以下示例演示:

var rawdata = { Col:
[ { Id_Col: 1, Col_Name: 'Col 1', Order: 5 },
{ Id_Col: 5, Col_Name: 'Col 5', Order: 1 },
{ Id_Col: 2, Col_Name: 'Col 2', Order: 2 },
{ Id_Col: 3, Col_Name: 'Col 3', Order: 3 },
{ Id_Col: 4, Col_Name: 'Col 4', Order: 4 } ],
Row:
[ { Id_Col: 1, RowIdex: 1, Val: 'Row 1 col 1' },
{ Id_Col: 2, RowIdex: 1, Val: 'Row 1 col 2' },
{ Id_Col: 3, RowIdex: 1, Val: 'Row 1 col 3' },
{ Id_Col: 4, RowIdex: 1, Val: 'Row 1 col 4' },
{ Id_Col: 1, RowIdex: 2, Val: 'Row 2 col 1' },
{ Id_Col: 2, RowIdex: 2, Val: 'Row 2 col 2' },
{ Id_Col: 3, RowIdex: 2, Val: 'Row 2 col 3' },
{ Id_Col: 4, RowIdex: 2, Val: 'Row 3 col 4' },
{ Id_Col: 1, RowIdex: 3, Val: 'Row 3 col 1' },
{ Id_Col: 2, RowIdex: 3, Val: 'Row 3 col 1' },
{ Id_Col: 3, RowIdex: 3, Val: 'Row 3 col 2' },
{ Id_Col: 4, RowIdex: 3, Val: 'Row 3 col 3' },
{ Id_Col: 5, RowIdex: 1, Val: 'Row 1 col 5' },
{ Id_Col: 5, RowIdex: 2, Val: 'Row 2 col 5' },
{ Id_Col: 5, RowIdex: 3, Val: 'Row 3 col 5' }
] };

var newCol=[];
var newData=[];
var colId_to_OrderIndex=[];

for (var i = 0; i < rawdata.Col.length; i++){
var ColOrderIndex = rawdata.Col[i].Order-1;
var ColId = rawdata.Col[i].Id_Col;
var ColName = rawdata.Col[i].Col_Name;

newCol[ColOrderIndex] = {
"title" : ColName
};

colId_to_OrderIndex[ColId] = ColOrderIndex;
}

for (var i = 0; i < rawdata.Row.length; i++){
var RowOrderIndex = rawdata.Row[i].RowIdex-1;
var RowColId = rawdata.Row[i].Id_Col;
var ColVal = rawdata.Row[i].Val;
var MapColId = colId_to_OrderIndex[RowColId];

if(newData[RowOrderIndex]===undefined){
newData[RowOrderIndex]=[];
}

newData[RowOrderIndex][MapColId]= ColVal;
}

console.log(rawdata);
console.log(newCol);
console.log(newData);

$(document).ready(function() {
$('#example').DataTable( {
data: newData,
columns: newCol
} );
} );
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>


<table id="example" class="display" width="100%"></table>

关于javascript - 使用动态列填充 jquery 数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57112640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com