DataTables服务器端处理,在具有两个变量的列中生成链接


DataTables Server-Side processing, generate a link in a column with two variables

我在一个有大型数据库的web中工作,需要使用服务器端的数据表处理。该脚本生成一个包含用户姓名、姓氏和工作的表,在最后一列中有一个下拉的boostrap按钮,该按钮具有不同的选项可供用户使用。(删除、编辑、查看配置文件…)问题是,我不知道如何生成按钮链接,因为其中一些选项有一个带有两个变量的链接,例如,delete.php?id=$id&user=$user

HTML

<table id="tabla_valoraciones" class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>User ID</th>
                        <th>User</th>
                        <th>Surname</th>
                        <th>Job</th>
                        <th>Company</th>
                        <th>Company Phone</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>    
                </tbody>
            </table>

JavaScript

$('#tabla_valoraciones').dataTable({
        "order": [[ 0, "desc" ]],
        "columnDefs": [
         {
                "render": function ( data, type, row ) {
                    return row[3]+', '+data;
                },
                "targets": 2
            },
            {
                "render": function ( data, type, row ) {
                    return data+' en '+row[5].toUpperCase();
                },
                "targets": 4
            },
            {
                "targets": [ 0 ],
                "visible": false,
                "searchable": false
            },
            {
                "targets": [ 1 ],
                "visible": false,
                "searchable": false
            },
            {
                "targets": [ 3 ],
                "visible": false,
                "searchable": false
            },
            {
                "targets": [ 5 ],
                "visible": false,
                "searchable": false
            }
        ],
        processing: true,
        serverSide: true,
        ajax: {
            url: 'php/procesado_valoraciones.php',
            dataType: 'json'
        }   
    });

PHP(pricesado_varraciones.PHP)

session_start();
// DB table to use
$table = 'valoraciones';
//where conditions
$where="estado=2";

// Table's primary key
$primaryKey = 'id';
// 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' => 'id', 'dt' => 0 ),
    array( 'db' => 'id_user', 'dt' => 1 ),
    array( 'db' => 'name', 'dt' => 2 ),
    array( 'db' => 'surname', 'dt' => 3 ),
    array( 'db' => 'job_name',  'dt' => 4 ),
    array( 'db' => 'company_name',  'dt' => 5 ),
    array( 'db' => 'company_phone',   'dt' => 6 ),
    array( 
            'db' => 'id', 
            'dt' => 7,
            'formatter' => function( $d, $row ) {
                $buttons='<div class="btn-group">
                                                        <button class="btn btn-default" type="button">Acciones</button>
                                                        <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button">
                                                        <span class="caret"></span>
                                                        <span class="sr-only">Toggle Dropdown</span>
                                                        </button>
                                                        <ul class="dropdown-menu" role="menu">
                                                            <li>
                                                                <a href="assess-experience.php?id='.$d.'&c="><i class="fa fa-check-circle fa-lg" title="Assess" alt="assess"></i> Assess</a>
                                                            </li>';
                                                           if($_SESSION["privilege"]==1):
                                                            $buttons.='<li>
                                                                <a href="delete-experience.php?id='.$d.'&c=" onclick="return confirmDelete;">
                                                <i class="fa fa-trash" alt="Delete" title="Delete Experience" ></i> Borrar</a>
                                                            </li>';
                                                            endif;
                                                        $buttons.='</ul>
                                                    </div>';
                return $buttons;
            }
       )
);
// SQL server connection information

$sql_details = array(
    'user' => 'root',
    'pass' => '****',
    'db'   => '****',
    'host' => 'local_host'
);

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */
require( 'ssp.class.php' );
/*echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);*/
echo json_encode(
    SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns, null, $where )
);

一切都很好,只是我不知道你是如何把用户id放在表的最后一列,在url:中

<a href="assess-experience.php?id='.$d.'&c='**$user_id**'"><i class="fa fa-check-circle fa-lg" title="Assess" alt="assess"></i> Assess</a>

如果有人帮助我,我真的会感激的!

提前谢谢,如果我的英语不正确,我很抱歉!

我解决了!

解决方案是使用$row变量。

PHP(Procesado_valoraciones.PHP)

$columns = array(
    array( 'db' => 'id', 'dt' => 0 ),
    array( 'db' => 'id_user', 'dt' => 1 ),
    array( 'db' => 'name', 'dt' => 2 ),
    array( 'db' => 'surname', 'dt' => 3 ),
    array( 'db' => 'job_name',  'dt' => 4 ),
    array( 'db' => 'company_name',  'dt' => 5 ),
    array( 'db' => 'company_phone',   'dt' => 6 ),
    array( 
            'db' => 'id', 
            'dt' => 7,
            'formatter' => function( $d, $row ) {
                $buttons='<div class="btn-group">
                                                        <button class="btn btn-default" type="button">Acciones</button>
                                                        <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button">
                                                        <span class="caret"></span>
                                                        <span class="sr-only">Toggle Dropdown</span>
                                                        </button>
                                                        <ul class="dropdown-menu" role="menu">
                                                            <li>
                                                                <a href="assess-experience.php?id='.$row[0].'&c='.$row[1].'"><i class="fa fa-check-circle fa-lg" title="Assess" alt="assess"></i> Assess</a>
                                                            </li>';
                                                           if($_SESSION["privilege"]==1):
                                                            $buttons.='<li>
                                                                <a href="delete-experience.php?id='.$row[0].'&c='.$row[1].'" onclick="return confirmDelete;">
                                                <i class="fa fa-trash" alt="Delete" title="Delete Experience" ></i> Delete</a>
                                                            </li>';
                                                            endif;
                                                        $buttons.='</ul>
                                                    </div>';
                return $buttons;
            }
       )
);

就我个人而言,我不会在服务器端代码中构建html。我只需要传递原始数据并在mRender中进行格式化。类似这样的东西:

"render": function ( data, type, row ) {
        return '<a href=''assess-experience.php?id='' + row[0] + '&c=' + row[7] +'''><i class=''fa fa-check-circle fa-lg'' title=''Assess'' alt=''assess''></i> Assess</a>';
 },

您可能想要从"$row"中获取"user_id"。。像这样的东西:

<a href="assess-experience.php?id='.$d.'&c='.$row->user_id.'"><i class="fa fa-check-circle fa-lg" title="Assess" alt="assess"></i> Assess</a> 

<a href="assess-experience.php?id='.$d.'&c='.$row["user_id"].'"><i class="fa fa-check-circle fa-lg" title="Assess" alt="assess"></i> Assess</a>