【问题标题】:Datatables Server-Side - Hidden field数据表服务器端 - 隐藏字段
【发布时间】:2017-03-20 14:41:05
【问题描述】:

我正在用 Laravel 5.4 编写一些东西,并且有一个用户表,我希望在服务器端实现它,因为该表太大而无法加载到内存中。

我希望在服务器端进程中隐藏记录的 ID。目前服务器脚本是:

$table = 'users';
$primaryKey = 'id';
$columns = array(
    array('db' => 'id', 'dt' =>0),
    array( 'db' => 'name', 'dt' => 1 ),
    array( 'db' => 'email',  'dt' => 2 ),
    array( 'db' => 'directline',   'dt' => 3 ),
    array( 'db' => 'active', 'dt' => 4)
);
// SQL server connection information
$sql_details = array(
    'user' => 'XXXX',
    'pass' => 'XXXX',
    'db'   => 'XXXX',
    'host' => 'localhost'
);   
require( 'ssp.class.php' ); 
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

我的观点目前显示:

<table id="thetable" class="table" align="center">
                                        <thead>
                                            <tr>
                                                <th>id</th>
                                                <th>Name</th>
                                                <th>Email</th>
                                                <th>direct line</th>
                                                <th>active</th>
                                            </tr>
                                        </thead>

                                    </table>

文件数据表的脚本是

$(document).ready(function() {
        $('#thetable').DataTable( {
            dom: 'lBfrtip',
                "iDisplayLength": 20,
                "lengthMenu": [ 10, 20,30, 50, 75, 100,200 ],
                buttons: [
                    'copy',  'print',
                    {extend: 'excel',
                        filename: 'users', footer:true},
                    {extend: 'pdf',
                        filename:  'users'},
                    {extend:'csvHtml5',
                        filename: 'users'},
                    {extend: 'collection',
                        text: 'columns',
                        buttons:['columnsVisibility'] }
                ],
                columnDefs: [
                    {'orderable':false, "targets":2    },
                    {"targets": [4], 
                        "render": function ( data, type, full, meta ) {

                            if(data == 1) 
                                {return '<span style="color: green"><i class="fa fa-check"> </i></span>'; }
                            else
                                {return '<span style="color: red"><i class="fa fa-times"> </i></span>'; }
                            }


                    },
                    ],
            "processing": true,
            "serverSide": true,
            "ajax": "{{ asset('/ajax/server_users.php') }}"
        } );
    } );

基本上我想隐藏 ID 列。

【问题讨论】:

    标签: php datatables hidden-field


    【解决方案1】:

    使用columns.visible 选项来启用或禁用指定列的显示。

    例如:

    'columnDefs': [
       { 'targets': 0, 'visible': false },
       // ... skipped ...
    ]
    

    【讨论】:

      【解决方案2】:

      我在 datatables 网站上稍微挖掘了一下,发现它被添加到服务器端脚本返回的项目中:

      array(
              'db' => 'id',
              'dt' => 'DT_RowId',
              'formatter' => function( $d, $row ) {
                  // Technically a DOM id cannot start with an integer, so we prefix
                  // a string. This can also be useful if you have multiple tables
                  // to ensure that the id is unique with a different prefix
                  return 'row_'.$d;
              }
          ),
      

      这会自动将字段 id 添加为可以使用的 DT_RowId,因此可以从表本身中删除它,但可以引用链接等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-24
        相关资源
        最近更新 更多