【问题标题】:add checkbox to datatable while loading data from database using ajax使用ajax从数据库加载数据时向数据表添加复选框
【发布时间】:2019-08-20 08:57:15
【问题描述】:

在使用 ajax 绑定来自 db 的数据时,我无法将复选框添加到我的 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);
?>

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

【问题讨论】:

  • 您的表中没有 id 列。是否也应该与复选框一起动态生成?

标签: javascript php jquery ajax datatables


【解决方案1】:

您可以为此使用columns.render 选项:

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

【讨论】:

  • 但是我需要从那里设置复选框的值,我尝试了 {render: mData => ''},和 {render: mData => ''+id}.
  • 没有。我得到了另一个值,例如,{ mData: 'district' } , { mData: 'deponame' }, 。我需要将 { mData: 'busid' } 分配给复选框
  • @Linu: 如果你需要将源数据对象的另一个属性的值放入单元格内容中,你可以引用整行数据,就像我上面给出的例子一样
【解决方案2】:

请找到这个答案。您可以从服务器端本身填充复选框

var table = $('#example').DataTable({
        "processing": false,
        "serverSide": true,
        "order": [],
        "ajax": {
            "url": "getBusperOrder.php",
            "type": "POST"
        }

在你需要添加的HTML中

<table id="example">
<thead>
    <tr>
        <th></th>
        <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>

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)) {
    $row = array();
    $row[] = '<div class="table-checkbox table-checkbox-data"><input type="checkbox" value="'. $rows['id'] .'"></div>';
   //insert other columns in $row array
    $data[] = $rows;
}
$output = array(
            "recordsTotal" => count($data),,
            "recordsFiltered" => count($data),,
            "data" => $data,
        );

    echo json_encode($output);
?>

【讨论】:

  • 但是,我怎样才能在数据表上显示这个复选框?
  • Datatable 会根据列数来处理。很高兴其他解决方案对您有用。
猜你喜欢
  • 2017-10-09
  • 1970-01-01
  • 2013-01-03
  • 2014-08-14
  • 2019-06-30
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 2014-11-16
相关资源
最近更新 更多