我有一个表,如我正在使用电子创建的应用中的以下内容。我正在使用jQuery,DataTables和sql.js: -
<table id="dataTable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Date</th>
<th>Code</th>
<th>Category</th>
<th>Hours</th>
<th>Cost</th>
<th>Billed</th>
<th>Description</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Date</th>
<th>Code</th>
<th>Category</th>
<th>Hours</th>
<th>Cost</th>
<th>Billed</th>
<th>Description</th>
</tr>
</tfoot>
</table>
填充表的以下代码无法正常工作: -
$(document).ready(function() {
var DB = null;
var t = $('#dataTable').DataTable();
$(".bottomMenuContainer").on("click", ".loadButton", function(e) {
var fs = require('fs');
var sql = require('sql.js');
var bfr = fs.readFileSync(__dirname + '/../data/EliteData.db');
DB = new sql.Database(bfr);
var stmt = DB.prepare("SELECT * FROM ProductEntries ORDER BY Category");
while(stmt.step()){
var row = stmt.getAsObject();
t.rows.add([row.Date, row.Code, row.Category, row.Hours, row.Cost, row.Billed, row.Description]).draw(false);
}
});
});
我在第0行,第1列错误消息中获得了一个未知参数'1',并且数据最终在表中显示在表格上,并且在实际数据库中有21个结果,而不是3个结果。p>任何想法发生了什么事?
答案是创建一个数组并将数据推入数组,然后将其添加到表中。然后绘制表。
while(stmt.step()){
var row = stmt.getAsObject();
var result = [];
result.push(row.Date);
result.push(row.Code);
result.push(row.Category);
result.push(row.Hours);
result.push(row.Cost);
result.push(row.Billed);
result.push(row.Description);
t.row.add(result);
}
t.draw();
很高兴看到您已经自己对其进行了排序 @resurgent :-)。虽然我自己玩了一点玩耍,并取消了这样的单行的添加:
let res = db.exec("SELECT * FROM ProductEntries");
table.rows.add(res[0].values).draw();
这是一个工作的JSFIDDLE,感谢您对SQL.JS的介绍,看起来很有趣!