如何将 DataTables 插件添加到使用 PHP 创建的 table
How can I add DataTables plugin to table created with PHP
我通过从我的数据库中读取数据创建了一个 table,它正确显示了它,但是当我想添加 DataTables 插件时它不起作用。
这是我尝试过的:
<?php
include_once 'classes/baza.class.php';
include_once 'classes/prijava_odjava.class.php';
$naziv = "Ispis";
include '_header.php'
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://datatables.net/download/build/nightly/jquery.dataTables.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<link href="http://datatables.net/download/build/nightly/jquery.dataTables.css" rel="stylesheet" type="text/css">
<nav class="large-6 row">
<table id='tablica'>
<?php
$baza = new Baza();
$upit = "SELECT * FROM user";
$rezultat = $baza->selectDB($upit);
//echo "";
echo "<tr><th>ID</th><th>Korisničko ime</th><th>Lozinka</th><th>Email</th><th>Last update</th><th>Aktivacijski kod</th><th>Aktiviran</th></tr>";
while ($row = mysqli_fetch_assoc($rezultat)) {
echo "<tr><td>" . $row['id_user']
. "</td><td> " . $row['username']
. "</td><td> " . $row["password"]
. "</td><td> " . $row["email"]
. "</td><td> " . $row['last_update']
. "</td><td> " . $row['aktivacijski_kod']
. "</td><td> " . $row['aktiviran']
. "</td><td></td></tr>";
}
//echo ""; //Close the table in HTML
?>
</table>
</nav>
<script src="js/vendor/jquery.js"></script>
<script src="js/foundation.min.js"></script>
<script>
$(document).foundation();
$(document).ready(function () {
$("#tablica").DataTable({
"bSort": true,
"bPaginate": true,
"bFilter": true
});
});
</script>
<?php include '_footer.php'; ?>
此代码仍显示数据库中的数据,但未使用 DataTable 插件。它就像它甚至不存在。
编辑:
添加了 thead 和 tbody 但我有同样的问题。
echo "<thead><tr><th>ID</th><th>Korisničko ime</th><th>Lozinka</th><th>Email</th><th>Last update</th><th>Aktivacijski kod</th><th>Aktiviran</th></tr></thead>";
echo "<tbody>";
while ($row = mysqli_fetch_assoc($rezultat)) {
echo "<tr><td>" . $row['id_user']
. "</td><td> " . $row['username']
. "</td><td> " . $row["password"]
. "</td><td> " . $row["email"]
. "</td><td> " . $row['last_update']
. "</td><td> " . $row['aktivacijski_kod']
. "</td><td> " . $row['aktiviran']
. "</td></tr>";
}
echo "</tbody>";
根据https://datatables.net/manual/installation
For DataTables to be able to enhance an HTML table, the table must be
valid, well formatted HTML, with a header (thead) and a body (tbody).
An optional footer (tfoot) can also be used.
您必须使用 <thead>
和 <tbody>
更新您的 html
我通过从我的数据库中读取数据创建了一个 table,它正确显示了它,但是当我想添加 DataTables 插件时它不起作用。
这是我尝试过的:
<?php
include_once 'classes/baza.class.php';
include_once 'classes/prijava_odjava.class.php';
$naziv = "Ispis";
include '_header.php'
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://datatables.net/download/build/nightly/jquery.dataTables.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<link href="http://datatables.net/download/build/nightly/jquery.dataTables.css" rel="stylesheet" type="text/css">
<nav class="large-6 row">
<table id='tablica'>
<?php
$baza = new Baza();
$upit = "SELECT * FROM user";
$rezultat = $baza->selectDB($upit);
//echo "";
echo "<tr><th>ID</th><th>Korisničko ime</th><th>Lozinka</th><th>Email</th><th>Last update</th><th>Aktivacijski kod</th><th>Aktiviran</th></tr>";
while ($row = mysqli_fetch_assoc($rezultat)) {
echo "<tr><td>" . $row['id_user']
. "</td><td> " . $row['username']
. "</td><td> " . $row["password"]
. "</td><td> " . $row["email"]
. "</td><td> " . $row['last_update']
. "</td><td> " . $row['aktivacijski_kod']
. "</td><td> " . $row['aktiviran']
. "</td><td></td></tr>";
}
//echo ""; //Close the table in HTML
?>
</table>
</nav>
<script src="js/vendor/jquery.js"></script>
<script src="js/foundation.min.js"></script>
<script>
$(document).foundation();
$(document).ready(function () {
$("#tablica").DataTable({
"bSort": true,
"bPaginate": true,
"bFilter": true
});
});
</script>
<?php include '_footer.php'; ?>
此代码仍显示数据库中的数据,但未使用 DataTable 插件。它就像它甚至不存在。
编辑: 添加了 thead 和 tbody 但我有同样的问题。
echo "<thead><tr><th>ID</th><th>Korisničko ime</th><th>Lozinka</th><th>Email</th><th>Last update</th><th>Aktivacijski kod</th><th>Aktiviran</th></tr></thead>";
echo "<tbody>";
while ($row = mysqli_fetch_assoc($rezultat)) {
echo "<tr><td>" . $row['id_user']
. "</td><td> " . $row['username']
. "</td><td> " . $row["password"]
. "</td><td> " . $row["email"]
. "</td><td> " . $row['last_update']
. "</td><td> " . $row['aktivacijski_kod']
. "</td><td> " . $row['aktiviran']
. "</td></tr>";
}
echo "</tbody>";
根据https://datatables.net/manual/installation
For DataTables to be able to enhance an HTML table, the table must be valid, well formatted HTML, with a header (thead) and a body (tbody). An optional footer (tfoot) can also be used.
您必须使用 <thead>
和 <tbody>