Matrix表格中存在不一致的IndexSizeError

时间:2017-01-12 10:31:00

标签: javascript

我试图在我的应用程序中复制this解决方案,数据是从MS SQL数据库中提取的,如果时间在ASC中排序它完美地工作,但如果在DESC基础中排序,我得到{{1 }}错误,我在thisthis等其他问题中读到了此错误,但未找到答案。

以下是我的代码,感谢您解决。 thaks



IndexSizeError

var tbody = document.querySelector('#completions').querySelector('tbody');
var headers = ['Date', 'FFS', 'SHF', 'SF', 'SCRP'];
var cache = {};
var date;
var completions = [
   {'Date':'11','FFS':'3,207'},{'Date':'11','SF':'1,501'},{'Date':'10','SHF':'603'},
   {'Date':'10','FFS':'4,643'},{'Date':'9','FFS':'2,352'},{'Date':'9','SHF':'603'},
   {'Date':'8','FFS':'4,008'},{'Date':'8','SF':'754'},{'Date':'7','FFS':'798'},
   {'Date':'6','FFS':'3,955'},{'Date':'6','SCRP':'503'},{'Date':'6','SF':'1,501'},
   {'Date':'6','SHF':'603'},{'Date':'5','FFS':'3,146'},{'Date':'5','SF':'1,503'},
   {'Date':'4','FFS':'5,375'},{'Date':'4','SF':'751'},{'Date':'4','SHF':'1,206'},
   {'Date':'3','FFS':'2,295'},{'Date':'3','SF':'752'},{'Date':'2','FFS':'6,300'},
   {'Date':'2','SF':'1,503'},{'Date':'1','FFS':'2,250'},{'Date':'1','SF':'1,506'},
   {'Date':'0','FFS':'2,236'}
,]; 
  
completions.forEach(function(completion, rIndex){
        date = completion['Date'];
        if (!cache[date]) {
        cache[date] = 'yes';
        var tr = tbody.insertRow(rIndex);
            tr.id = 'id'+date;
            headers.forEach(function(){ tr.insertCell(); })
            tbody.appendChild(tr);
        } 
        var row = tbody.querySelector('#id'+date).rowIndex - 1;
        headers.forEach(function(header,pIndex){
                if(completion[header]) 
                     tbody.rows[row].cells[pIndex].innerHTML = completion[header];
        })
});                

table { border-collapse: collapse; width: 600px; }
table, th, td { font-family: 'Segoe UI Light'; border: 1px solid black; }
th, td { width: 200px;}
th { background-color: #4CAF50; color: white; }
tr:hover { background-color: #4CAF50; color: white; }
td { text-align: right; }
td:first-child { text-align: left; }
tr:nth-child(even) {background-color: #f2f2f2}
tr:hover {background-color: yellow; color: red;}

h1 { font-family: 'Segoe UI Light'; font-size: 24px; font-style: normal;
     font-variant: normal; font-weight: 500; line-height: 26.4px; }
                  




1 个答案:

答案 0 :(得分:0)

虽然我仍然不明白发生了什么,以及为什么当我对ASC进行排序时一切顺利,而在进行DESC排序时问题就开始了,但我发现this回答有帮助,所以我改变了我插入的方法表格末尾的行:

var tr = tbody.insertRow(rIndex);

为:

var tr = tbody.insertRow(-1);

并正确显示我的结果。