Php 添加自定义字段jQuery数据表
我正在使用jQuery数据表填充数据。我使用服务器端方法,我需要它,因为我正在获取十万条记录。但是,我不知道如何添加自定义字段。比如说Php 添加自定义字段jQuery数据表,php,jquery,laravel,datatables,Php,Jquery,Laravel,Datatables,我正在使用jQuery数据表填充数据。我使用服务器端方法,我需要它,因为我正在获取十万条记录。但是,我不知道如何添加自定义字段。比如说 |Action| ________ http://localhost/qms/public/customer/1/edit 哪个1应该是ID 因为在datatables上,您只声明需要的列表,所以我不知道如何添加自定义表 我目前有: 我需要把行动列编辑这些客户。我使用的是Laravel 5.1 HTML视图: <table id="CustomerLi
|Action|
________
http://localhost/qms/public/customer/1/edit
哪个1
应该是ID
因为在datatables上,您只声明需要的列表,所以我不知道如何添加自定义表
我目前有:
我需要把行动列编辑这些客户。我使用的是Laravel 5.1
HTML视图:
<table id="CustomerList" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th colspan="7"> <center>Customer Information<center></th>
<!-- <th colspan="2"> <center>Actions<center></th> -->
</tr>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Phone Number</th>
<th>Country</th>
<th>Postcode</th>
<!-- <th>Edit</th>
<th>Delete</th> -->
</tr>
</thead>
<tbody>
</tbody>
</table>
客户信息
身份证件
名字
姓
性别
电话号码
国家
邮政编码
阿贾克斯:
$(文档).ready(函数(){
$.fn.dataTable.ext.legacy.ajax=true;
$('#CustomerList')。数据表({
“处理”:对,
“服务器端”:正确,
“ajax”:“api/customer/all”,
“分页”:正确,
“搜索”:没错,
“排序”:正确,
} );
var tt=new$.fn.dataTable.TableTools($('#CustomerList').dataTable());
$(tt.fncainer()).insertBefore('div.dataTables_wrapper');
});
控制器:
public function apiGetCustomers()
{
/*=================================================================*/
/*
* Script: DataTables server-side script for PHP and PostgreSQL
* Copyright: 2010 - Allan Jardine
* License: GPL v2 or BSD (3-point)
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array('id', 'firstname', 'lastname', 'gender', 'phone_num', 'country', 'postcode' );
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "phone_num";
/* DB table to use */
$sTable = "customers";
/* Database connection information */
$gaSql['user'] = "postgres";
$gaSql['password'] = "postgres";
$gaSql['db'] = "qms";
$gaSql['server'] = "localhost";
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* DB connection
*/
$gaSql['link'] = pg_connect(
" host=".$gaSql['server'].
" dbname=".$gaSql['db'].
" user=".$gaSql['user'].
" password=".$gaSql['password']
) or die('Could not connect: ' . pg_last_error());
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_GET['iDisplayLength'] )." OFFSET ".
intval( $_GET['iDisplayStart'] );
}
/*
* Ordering
*/
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc').", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE This assumes that the field that is being searched on is a string typed field (ie. one
* on which ILIKE can be used). Boolean fields etc will need a modification here.
*/
$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" )
{
if($aColumns[$i] != 'id') // Exclude ID for filtering
{
$sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string( $_GET['sSearch'] )."%' OR ";
}
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ")";
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i]." ILIKE '%".pg_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
$sQuery = "
SELECT ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$sQuery = "
SELECT $sIndexColumn
FROM $sTable
";
$rResultTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$iTotal = pg_num_rows($rResultTotal);
pg_free_result( $rResultTotal );
if ( $sWhere != "" )
{
$sQuery = "
SELECT $sIndexColumn
FROM $sTable
$sWhere
";
$rResultFilterTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
$iFilteredTotal = pg_num_rows($rResultFilterTotal);
pg_free_result( $rResultFilterTotal );
}
else
{
$iFilteredTotal = $iTotal;
}
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = pg_fetch_array($rResult, null, PGSQL_ASSOC) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
/* Special output formatting for 'version' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
// Free resultset
pg_free_result( $rResult );
// Closing connection
pg_close( $gaSql['link'] );
}
公共函数apiGetCustomers()
{
/*=================================================================*/
/*
*脚本:用于PHP和PostgreSQL的DataTables服务器端脚本
*版权:2010-艾伦怡和
*许可证:GPL v2或BSD(3点)
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*易设置变量
*/
/*应读取并发送回DataTables的数据库列数组。请在
*要插入非数据库字段(例如计数器或静态图像)
*/
$aColumns=数组('id','firstname','lastname','gender','phone_num','country','postcode');
/*索引列(用于快速准确的表基数)*/
$sIndexColumn=“电话号码”;
/*要使用的数据库表*/
$sTable=“客户”;
/*数据库连接信息*/
$gaSql['user']=“postgres”;
$gaSql['password']=“postgres”;
$gaSql['db']=“qms”;
$gaSql['server']=“localhost”;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*如果您只想在PHP服务器端使用DataTables的基本配置,那么
*无需在此行下方进行编辑
*/
/*
*数据库连接
*/
$gaSql['link']=pg_connect(
“主机=”.$gaSql['server']。
“dbname=”.$gaSql['db']。
“user=”.$gaSql['user']。
“password=”.$gaSql['password']
)或死亡('无法连接:'.pg_last_error());
/*
*寻呼
*/
$sLimit=“”;
如果(isset($\u GET['iDisplayStart'])&&&$\u GET['IDisplaySlength']!='-1')
{
$sLimit=“LIMIT”.intval($_GET['iDisplayLength'])“OFFSET”。
intval($_GET['iDisplayStart']);
}
/*
*订购
*/
如果(isset($\u GET['iSortCol\u 0']))
{
$sOrder=“订购人”;
对于($i=0;$i$i过滤总计,
“aaData”=>array()
);
而($aRow=pg_fetch_数组($rResult,null,PGSQL_ASSOC))
{
$row=array();
对于($i=0;$i)
解决方案
- 在HTML中,在表标题中为操作添加一列
- 在DataTables中,初始化选项添加到第8列的目标(
“targets”:7
,零基索引),并使用选项生成该列的内容
在函数中,您可以使用row
变量访问该行的数据。由于您在PHP脚本中返回数组数组,因此可以使用row[0]
(第1列,从零开始的索引)访问您的ID
var table=$('#CustomerList')。数据表({
“处理”:对,
“服务器端”:正确,
“ajax”:“api/customer/all”
“columnDefs”:[
{
“目标”:7,
“呈现”:函数(数据、类型、行、元){
返回“”;
}
}
]
});
演示
有关代码和演示,请参阅
为自定义字段添加标题,如上所述EditDelete
用于添加自定义字段的内容。由于您使用的是“数组数组”作为数据源,因此必须采用以下方式:
小型演示->
var table = $('#example').DataTable({
data : data.data,
columns : [
null,
null,
null,
null,
null,
null,
null,
{ render : function() {
return '<button>edit</button>'
}
},
{ render : function() {
return '<button>delete</button>'
}
}
]
})
var table=$('#示例').DataTable({
数据:data.data,
栏目:[
无效的
无效的
无效的
无效的
无效的
无效的
无效的
{render:function(){
返回“编辑”
}
},
{render:function(){
返回“删除”
}
}
]
})
谢谢您的回复。我会先更新这一条,因为@Gyrocode.com的答案更简单。谢谢!提示:从问题中删除数据库的用户名和密码
var table = $('#CustomerList').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "api/customer/all"
"columnDefs": [
{
"targets": 7,
"render": function(data, type, row, meta){
return '<a href="/qms/public/customer/' + row[0] + '/edit">Edit</a>';
}
}
]
});
var table = $('#example').DataTable({
data : data.data,
columns : [
null,
null,
null,
null,
null,
null,
null,
{ render : function() {
return '<button>edit</button>'
}
},
{ render : function() {
return '<button>delete</button>'
}
}
]
})