【发布时间】:2019-11-15 08:13:57
【问题描述】:
我使用带有 MySQL 数据库和 jquery datatable 插件的 localhost 服务器制作了一个电子应用程序。如何将 MySQL 数据库中的数据加载到 datatable 中?
我已经能够在控制台中打印数据库中的数据,但我不知道如何使用我拥有的数据填充datatable
index.html 表格
<section role="main" id="main">
<div class="with-padding">
<table class="table responsive-table" id="sorting-advanced">
<thead>
<tr>
<th scope="col"><input type="checkbox" name="check-all" id="check-all" value="1"></th>
<th scope="col">Text</th>
<th scope="col" width="15%" class="align-center hide-on-mobile">Date</th>
<th scope="col" width="15%" class="align-center hide-on-mobile-portrait">Status</th>
<th scope="col" width="15%" class="hide-on-tablet">Tags</th>
<th scope="col" width="60" class="align-center">Actions</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="6">
6 entries found
</td>
</tr>
</tfoot>
<tbody>
<tr>
<th scope="row" class="checkbox-cell"><input type="checkbox" name="checked[]" id="check-1" value="1"></th>
<td id="name"></td>
<td id="date"></td>
<td id="status">Enabled</td>
<td><small class="tag">User</small> <small class="tag">Client</small> <small class="tag green-bg">Valid</small></td>
<td class="low-padding align-center"><a href="#" class="button compact icon-gear">Edit</a></td>
</tr>
</tbody>
</table>
</div>
</section>
数据表脚本
<script>
var table = $('#sorting-advanced');
table.dataTable({
'aoColumnDefs': [{
'bSortable': false,
'aTargets': [0, 5]
}],
'sPaginationType': 'full_numbers',
'sDom': '<"dataTables_header"lfr>t<"dataTables_footer"ip>',
'fnInitComplete': function(oSettings) {
// Style length select
table.closest('.dataTables_wrapper').find('.dataTables_length select').addClass('select blue-gradient glossy').styleSelect();
tableStyled = true;
}
});
</script>
preload.js
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers", function(err, result, fields) {
if (err) throw err;
let name = [this is what am getting no][1]document.getElementById("name");
let date = document.getElementById("date");
let status = document.getElementById("status");
console.log(result);
for (var i = 0; i < result.length; i++) {
name.innerHTML += result[i].name + "<br/>";
date.innerHTML += result[i].address + "<br/>";
status.innerHTML += "enable" + "<br/>";
}
});
});
我想从datatable中的数据库中加载所有数据
【问题讨论】:
-
您想使用哪种技术来满足此要求? PHP 或其他一些服务器端语言?单独使用jQuery,这可能是不可能的
-
我在 node js 环境中工作,有没有办法使用 JavaScript 解决这个问题?如果没有,那么我将如何在电子应用程序中使用 PHP 来实现这一点?谢谢
-
您的数据库在哪里?在任何服务器上?那么使用一些服务器端语言可能会更容易
-
我在我的电脑上使用 localhost 服务器,XAMPP
-
我已经编辑了我的代码,我现在已经能够显示它们,但是所有数据都出现在一个表格行中。我已经包含了我得到的和想要得到的截图。谢谢。
标签: jquery html mysql datatables electron