dongqiao2077 2014-09-29 06:59
浏览 482

添加DataTables的复选框

i'm using Datatables to make web page which i should use to control a MySQL Table (a really ordinary task). There's one thing that i don't know why it doesn't work. I want to add a column to my table contaninig checkboxes, so later i can use those with some javascript code to do some operations on the rows. Anyway here's my code:

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>Process manager</title>

<!-- Bootstrap core CSS -->
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="css/bootstrap/navbar-static-top.css" rel="stylesheet">

<!-- DataTables bootstrap CSS -->
<link href="css/dataTables/dataTables.bootstrap.css" rel="stylesheet">

</head>

<body>
    <table id="processManager" class="table table-striped table-bordered">
        <thead>
            <tr>
                <th></th>
                <th>process</th>
                <th>Resource</th>
                <th>Category</th>
                <th>Format</th>
                <th>Automaticity</th>
                <th>Process_type</th>
                <th>Access</th>
                <th>Source</th>
                <th>A</th>
                <th>B</th>
                <th>status_A</th>
                <th>status_B</th>
                <th>time_A</th>
                <th>time_B</th>
                <th>exec_A</th>
                <th>exec_B</th>
                <th>period</th>
                <th>overtime</th>
                <th>param</th>
                <th>last_update</th>
                <th>last_triples</th>
                <th>error</th>
                <th>description</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th></th>
                <th>process</th>
                <th>Resource</th>
                <th>Category</th>
                <th>Format</th>
                <th>Automaticity</th>
                <th>Process_type</th>
                <th>Access</th>
                <th>Source</th>
                <th>A</th>
                <th>B</th>
                <th>status_A</th>
                <th>status_B</th>
                <th>time_A</th>
                <th>time_B</th>
                <th>exec_A</th>
                <th>exec_B</th>
                <th>period</th>
                <th>overtime</th>
                <th>param</th>
                <th>last_update</th>
                <th>last_triples</th>
                <th>error</th>
                <th>description</th>
            </tr>
        </tfoot>
    </table>
    <script src="js/jquery/jquery-1.11.1.min.js"></script>
    <script src="js/bootstrap/bootstrap.min.js"></script>
    <script src="js/dataTables/jquery-1.10.1.dataTables.min.js"></script>
    <script src="js/dataTables/dataTables.bootstrap.js"></script>
    <script>
    $(document).ready( function loadData() {

        processManager = $('#processManager').DataTable( {
            "ajax": {
                url: "getSingleTable.php",
            },
            "columns": [
            {
                "orderable": false,
                // Create an HTML select with all the versions of the data
                //TODO Correggi +01
                "data": function (row, type, val, meta) {
                    return  '<input type="checkbox"/>';
                }
            },
            { "data": "Process ID" },
            { "data": "Resource" },
            { "data": "Category" },
            { "data": "Format" },
            { "data": "Automaticity" },
            { "data": "Process type" },
            { "data": "Access" },
            { "data": "Source" },
            { "data": "A" },
            { "data": "B" },
            { "data": "Status A" },
            { "data": "Status B" },
            { "data": "Time A" },
            { "data": "Time B" },
            { "data": "Exec A" },
            { "data": "Exec B" },
            { "data": "Period" },
            { "data": "Overtime" },
            { "data": "Parameters" },
            { "data": "Last Update" },
            { "data": "Last Triples" },
            { "data": "Error" },
            { "data": "Description" }

            ],
            "language": {
                "decimal": ",",
                "thousands": "."
            },
            "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
            "order": [[0, 'asc']],
            "pagingType": "full_numbers",
            "processing": true,
            "serverSide": true,
            "stateSave": true
        } );

});

    </script>
</body>
</html>

getSingleTable.php

<?php

// DB table to use
// $table = 'process_manager';
$table = 'process_manager2';

// Table's primary key
// $primaryKey = 'Process';
$primaryKey = 'Process';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array (
        array (
                'db' => 'process',
                'dt' => 'Process ID' 
        ),
        array (
                'db' => 'Resource',
                'dt' => 'Resource' 
        ),
        array (
                'db' => 'Category',
                'dt' => 'Category' 
        ),
        array (
                'db' => 'Format',
                'dt' => 'Format' 
        ),
        array (
                'db' => 'Automaticity',
                'dt' => 'Automaticity' 
        ),
        array (
                'db' => 'Process_type',
                'dt' => 'Process type' 
        ),
        array (
                'db' => 'Access',
                'dt' => 'Access' 
        ),
        array (
                'db' => 'Source',
                'dt' => 'Source' 
        ),
        array (
                'db' => 'A',
                'dt' => 'A' 
        ),
        array (
                'db' => 'B',
                'dt' => 'B' 
        ),
        array (
                'db' => 'status_A',
                'dt' => 'Status A' 
        ),
        array (
                'db' => 'status_B',
                'dt' => 'Status B' 
        ),
        array (
                'db' => 'time_A',
                'dt' => 'Time A' 
        ),
        array (
                'db' => 'time_B',
                'dt' => 'Time B' 
        ),
        array (
                'db' => 'exec_A',
                'dt' => 'Exec A' 
        ),
        array (
                'db' => 'exec_B',
                'dt' => 'Exec B' 
        ),
        array (
                'db' => 'period',
                'dt' => 'Period' 
        ),
        array (
                'db' => 'overtime',
                'dt' => 'Overtime' 
        ),
        array (
                'db' => 'param',
                'dt' => 'Parameters' 
        ),
        array (
                'db' => 'last_update',
                'dt' => 'Last Update' 
        ),
        array (
                'db' => 'last_triples',
                'dt' => 'Last Triples' 
        ),
        array (
                'db' => 'error',
                'dt' => 'Error' 
        ),
        array (
                'db' => 'description',
                'dt' => 'Description' 
        ) 
);

// SQL server connection information
$sql_details = array (
        'user' => 'USER',
        'pass' => 'PASS',
        'db' => 'DB',
        'host' => 'IP' 
);

require ('ssp.class.php');

echo json_encode ( SSP::simple ( $_GET, $sql_details, $table, $primaryKey, $columns ) );

Instead of "USER", "PASS", "DB" and "IP" i put the real values of course. Everything works fine if i don't try to put the checkboxes. As you can see i'm trying to put checkboxes in the first column, before "process", wich should be the only column that is not present in the MySQL table. Anyone know what's wrong ?

  • 写回答

3条回答 默认 最新

  • duanli0453 2014-09-29 07:08
    关注

    You should put the function into render parameter not data parameter. data is for a column name which you don't need there.

          "orderable": false,
                // Create an HTML select with all the versions of the data
                //TODO Correggi +01
                "render": function (row, type, val, meta) {
                    return  '<input type="checkbox"/>';
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机