根据json创建的2d数组动态填充表
Populate table dynamically by given 2d array created from json
我创建了这个函数,可以用于任何二维数组:
GenerateTableByArray: function(tableData){
var body = document.getElementsByTagName("body")[0];
var table = document.createElement('table');
var tableBody = document.createElement('tbody');
tableData.forEach(function(rowData) {
//creating rows for the table
var row = document.createElement('tr');
row.style.backgroundColor = "#EEF4EA";
//creating cell for every row
rowData.forEach(function(cellData) {
var cell = document.createElement('td');
cell.appendChild(document.createTextNode(cellData));
row.appendChild(cell);
});
tableBody.appendChild(row);
});
//appending the generated table into the html body (with border as attribute)
table.appendChild(tableBody);
table.setAttribute("border", "1");
document.body.appendChild(table);
}
对于我传递的任何二维数组,这个函数都可以正常工作。
我遇到的问题是用json对象的特定值填充数组。
对象:
var DashboardJSON = {
"data": [
{
"name": "Person1",
"date": "2010-10-08",
"pct": 81.25
},
{
"name": "Person1",
"date": "2010-10-09",
"pct": 56.25
},
{
"name": "Person2",
"date": "2010-09-11",
"pct": 82.22
}
]
}
我需要让它看起来像这样:
|2010-10-08|2010-10-09|2010-09-11
------------------------------------------
Person1 | 81.25 | 56.25 |
Person2 | | | 82.22
下面是我创建二维数组的函数:
Main: function () {
//First i get the unique names
var uniqueNames = {};
var distinctNames = [];
for( var i in DashboardJSON.data ){
if( typeof(uniqueNames[DashboardJSON.data[i].name]) == "undefined"){
distinctNames.push(DashboardJSON.data[i].name);
}
uniqueNames[DashboardJSON.data[i].name] = 0;
}
//Then the unique dates
var uniqueDates = {};
var distinctDates = [];
for( var i in DashboardJSON.data ){
if( typeof(uniqueDates[DashboardJSON.data[i].date]) == "undefined"){
distinctDates.push(DashboardJSON.data[i].date);
}
uniqueDates[DashboardJSON.data[i].date] = 0;
}
var ROWS = distinctNames.length+1;
var COLS = distinctDates.length+1;
//creating two-dimensional array
var my2dArray = new Array(ROWS);
for (var i = 0; i < my2dArray.length; i++) {
my2dArray[i] = new Array(COLS);
}
//filling the array with data
for(key in DashboardJSON.data){
for(var i = 0; i < ROWS; i++){
for(var j = 0; j < COLS; j++){
//first cell is empty
if(i==0 && j==0){
my2dArray[i][j]="";
}
//the column titles are filled from the dates array
if(i==0 && j>0){
my2dArray[i][j+1]=distinctDates[j-1];
}
//the row titles are filled with the names array
if(j==0 && i>0 ){
my2dArray[i][j]=distinctNames[i-1];
//HERE i need to populate the array with the point for each person by date but it doesn't work
} else if (i!=0 && j!=0){
if(DashboardJSON.data[key].name == distinctNames[i-1]){
my2dArray[i][j]=DashboardJSON.data[key].pct;
console.log(my2dArray[i][j]);
}
}
}
}
}
//calling the function to generate my table
GenerateTableByArray(my2dArray);
}
由于某些原因,它不能正确工作,并且只使用person的最后一个点而不是所有点填充我的表。:/
我只需要用javascript来解决这个问题
您还需要在内循环中添加日期检查。修改如下:
if(DashboardJSON.data[key].name == distinctNames[i-1]){
my2dArray[i][j]=DashboardJSON.data[key].pct;
console.log(my2dArray[i][j]);
}
变成这样:
if(DashboardJSON.data[key].name == distinctNames[i-1]
&& DashboardJSON.data[key].date == my2dArray[0][j]){
my2dArray[i][j]=DashboardJSON.data[key].pct;
console.log(my2dArray[i][j]);
}
我想它能成功。
(对于害怕符号$,里面没有jQuery,只有JS)
var el = document.getElementById('dbg');
var $l = function(val){
el.innerHTML = el.innerHTML + '<div class="line"><pre>' + val + '</pre></div>';
}
$l('Testing sample : you can change the ordering');
var jdata = [
{
"name": "Person1",
"date": "2010-10-08",
"pct": 81.25
},
{
"name": "Person1",
"date": "2010-10-09",
"pct": 56.25
},
{
"name": "Person2",
"date": "2010-09-11",
"pct": 82.22
}
,
{
"name": "Person3",
"date": "2010-09-11",
"pct": 82.22
},
{
"name": "Person4",
"date": "2010-10-11",
"pct": 82.22
},
{
"name": "Person2",
"date": "2010-10-11",
"pct": 82.22
}
];
//jdata.sort(function(a,b){return a.pct < b.pct});
var t = { byDate : {} , byName : {}};
jdata.forEach(function(v){
var curDate = t.byDate[v.date]= t.byDate[v.date] || {};
var curName = t.byName[v.name]= t.byName[v.name] || {};
curName[v.date] = v;
});
//HERE YOU CAN CHANGE ORDERING
var COLS = Object.keys(t.byDate).sort(function(a,b){return a > b});
var ROWS = Object.keys(t.byName).sort(function(a,b){return a > b});
var my2dArray = [];
ROWS.forEach(function( rowLabel ,rowNum){
var currentRowNum = rowNum + 1;
if(rowNum===0) my2dArray[ rowNum ] = []; // colLabels
my2dArray[ currentRowNum ] = [];
COLS.forEach(function(colLabel , colNum ){
var dataValue = '-';
if(t.byName[rowLabel] && t.byName[rowLabel][colLabel]) dataValue = t.byName[rowLabel][colLabel]['pct'];
if( rowNum ===0 && colNum===0){
my2dArray[ rowNum ][colNum] = "";
} else if ( rowNum ===0 && colNum>0){
my2dArray[ rowNum ][colNum] = colLabel;
}
if ( colNum===0){
my2dArray[ currentRowNum ][colNum] = rowLabel;
} else {
my2dArray[ currentRowNum ][colNum] = dataValue;
};
});
});
$l(JSON.stringify(my2dArray , null , ' ') )
.line{
border:solid 1px #CCC;
padding:3px;
margin:3px;
}
<div id='dbg'></div>
相关文章:
- 动态填充两个下拉菜单
- 如何在Highchart中动态填充数据
- 动态填充Bootstrap选择选择器:change event dos'不起作用
- 根据手机上是否存在文件,在jQuery mobile中动态填充列表视图
- 如何在js中实现树结构类型的表单动态填充
- 动态填充复选框
- 无法使用国家/地区选择中的状态动态填充下拉列表
- 动态填充的选择列表
- Jquery Selected plugin - 由 Ajax 动态填充列表
- 在 angularjs 中动态填充的 orderby,不使用输入的第二个变量
- '使用文本区域中的字符串动态填充下拉菜单
- 读取一个动态填充的txt文件
- 动态填充字段的Jquery验证
- 如何使用服务器的JSON响应动态填充dust模板
- 使用jquery动态填充下拉列表
- 如何动态填充DataTables选择菜单
- 使用jquery解析json以构建动态填充的select
- 在AngularJS中动态填充下拉列表
- Jquery Chosen插件-通过Ajax动态填充列表时显示加载图标
- 使用数据库返回的数据动态填充下拉列表