javascript - 使用ajax从数据库加载数据时将复选框添加到数据表

标签 javascript php jquery ajax datatables

在使用 ajax 从 db 绑定(bind)数据时,我无法将复选框添加到我的 DataTable。如何添加带有服务器端数据加载的复选框?

我的 jQuery:

var table = $('#example').DataTable({
        "ajax": "getBusperOrder.php",
        "bPaginate": true,
        "retrieve": true,
        "bProcessing": true,
        "pageLength": 10,
        "columns": [{
                mData: 'district'
            }, {
                mData: 'deponame'
            }, {
                mData: 'busname'
            }, {
                mData: 'bonnetnumber'
            }, {
                mData: 'routename'
            }, {
                mData: 'bustype'
            }, {
                mData: 'status'
            }
        ],
    });

HTML:

<table id="example">
    <thead>
        <tr>
            <th>District</th>
            <th>Depo Name</th>
            <th>Bus Number</th>
            <th>Bonnet Number</th>
            <th>Route Name</th>
            <th>Bus Type</th>
            <th>Action</th>
        </tr>
    </thead>
</table>

gerBusperOrder.php

<?php
require('database/db.php');
$sql = "select * from bus as B left join depo as D on B.depoid=D.depoid left join district as DS on D.district=DS.id left join bustype as BS on B.bustypeid=BS.bustypeid left join route as R on B.routeid=R.routeid LEFT JOIN bustype as BT on B.bustypeid=BT.bustypeid WHERE B.busid IN(SELECT busid from bus where busid NOT IN (SELECT DISTINCT(bus_id) from advt_book_side AS ABS INNER JOIN booking as B on ABS.booking_number=B.bookingnumber WHERE B.todate>CURDATE() GROUP BY bus_id HAVING COUNT(sides_id)=4))";
$resultset = mysqli_query($db, $sql) or die("database error:" . mysqli_error($db));
$data = array();
while ($rows = mysqli_fetch_assoc($resultset)) {
    $data[] = $rows;
}
$results = array(
    "sEcho" => 1,
    "iTotalRecords" => count($data),
    "iTotalDisplayRecords" => count($data),
    "aaData" => $data
);
echo json_encode($results);
?>

我需要在每个带有 id 的 td 的第一列添加一个复选框

最佳答案

您可以雇用 columns.render为此目的的选项:

"columns": [{
        mData: 'district'
        render: (_,__,rowData) => `<input type="checkbox" value="${rowData.busid}">${rowData.busid}</input>`
    },
    ...
]

关于javascript - 使用ajax从数据库加载数据时将复选框添加到数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57570064/

相关文章:

删除许可证的javascript缩小?

php - Laravel 使用 belongstoMany 从 ID 获取模型

php - 尝试通过使用 foreach 循环使用 MYSQL 选择一行数据

jquery - <td> 中的 div 100% 高度

javascript - 在 jQuery 切换方法中保存先前的值

php - jquery ajax 即使发生更新也总是出错

javascript - 当我想使用外部 "_this is undefined"关键字时出现 "this"错误

javascript - 来自 URL 的正则表达式匹配数据在有两个匹配项时仅返回 1 个匹配项

php - 来自已加载页面的 Javascript 响应未显示在 <span> 中,尽管我可以在 Firebug 中看到它

javascript - 为什么 Jquery 无法访问我页面上导入的元素