那么伙计们,我怎么能在我的专栏中放一个按钮:
Protcolo Assunto 行动
001 lalalalala print
我使用datatable服务器端:
<table id="datatable_fixed_column" class="table table-striped table-bordered" width="100%">
<thead>
<tr>
<th>Protocolo</th>
<th>Assunto</th>
<th>Action</th>
</tr>
</thead>
</table>
<script type="text/javascript">
$(document).ready(function() {
var otable = $('#datatable_fixed_column').DataTable({
"processing": true,
"serverSide": true,
"ajax": "server_processing/protocolos.php",
"order": [[ 0, "asc" ]],
"columnDefs": [
{ "width": "10%", "targets": 0 },
{ "width": "70%", "targets": 1 },
{ "width": "20%", "targets": 2 }
],
columns: [
{ data: "protocolo" },
{ data: "assunto" },
{
data: null,
defaultContent: '<form method="post" action=""><input type="hidden" id="idcontrib" name="idcontrib" value="id_protocolo"><button type="submit" class="btn btn-warning btn-xs" name="edit_contrib">Editar</button> <button type="submit" class="btn btn-danger btn-xs" name="exc_contrib">Excluir</button></form>'
}
],
"sDom": "<'dt-toolbar'<'col-xs-6'f><'col-xs-6'<'toolbar'>>r>"+
"t"+
"<'dt-toolbar-footer'<'col-xs-6'i><'col-xs-6'p>>"
});
$("div.toolbar").html('<div class="text-right"><img src="img/logo2.png" alt="SmartAdmin" style="width: 111px; margin-top: 3px; margin-right: 10px;"></div>');
})
</script>
protocolos.php
<?php
$table = 'tbl_protocolos_teste';
$primaryKey = 'id_protocolo';
$columns = array(
array( 'db' => 'protocolo', 'dt' => 0 ),
array( 'db' => 'assunto', 'dt' => 1 ),
array( 'db' => 'id_protocolo', 'dt' => 2 )
);
$sql_details = array(
'user' => 'root',
'pass' => '$#$#$#',
'db' => 'bbbb',
'host' => 'localhost'
);
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
ssp.class.php
<?php
class SSP {
static function data_output ( $columns, $data )
{
$out = array();
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
$row = array();
for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
$column = $columns[$j];
// Is there a formatter?
if ( isset( $column['formatter'] ) ) {
$row[ $column['dt'] ] = $column['formatter']( $data[$i][ $column['db'] ], $data[$i] );
}
else {
$row[ $column['dt'] ] = $data[$i][ $columns[$j]['db'] ];
}
}
$out[] = $row;
}
return $out;
}
static function limit ( $request, $columns )
{
$limit = '';
if ( isset($request['start']) && $request['length'] != -1 ) {
$limit = "LIMIT ".intval($request['start']).", ".intval($request['length']);
}
return $limit;
}
static function order ( $request, $columns )
{
$order = '';
if ( isset($request['order']) && count($request['order']) ) {
$orderBy = array();
$dtColumns = self::pluck( $columns, 'dt' );
for ( $i=0, $ien=count($request['order']) ; $i<$ien ; $i++ ) {
// Convert the column index into the column data property
$columnIdx = intval($request['order'][$i]['column']);
$requestColumn = $request['columns'][$columnIdx];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['orderable'] == 'true' ) {
$dir = $request['order'][$i]['dir'] === 'asc' ?
'ASC' :
'DESC';
$orderBy[] = '`'.$column['db'].'` '.$dir;
}
}
$order = 'ORDER BY '.implode(', ', $orderBy);
}
return $order;
}
static function filter ( $request, $columns, &$bindings )
{
$globalSearch = array();
$columnSearch = array();
$dtColumns = self::pluck( $columns, 'dt' );
if ( isset($request['search']) && $request['search']['value'] != '' ) {
$str = $request['search']['value'];
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
if ( $requestColumn['searchable'] == 'true' ) {
$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$globalSearch[] = "`".$column['db']."` LIKE ".$binding;
}
}
}
// Individual column filtering
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
$requestColumn = $request['columns'][$i];
$columnIdx = array_search( $requestColumn['data'], $dtColumns );
$column = $columns[ $columnIdx ];
$str = $requestColumn['search']['value'];
if ( $requestColumn['searchable'] == 'true' &&
$str != '' ) {
$binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
$columnSearch[] = "`".$column['db']."` LIKE ".$binding;
}
}
// Combine the filters into a single string
$where = '';
if ( count( $globalSearch ) ) {
$where = '('.implode(' OR ', $globalSearch).')';
}
if ( count( $columnSearch ) ) {
$where = $where === '' ?
implode(' AND ', $columnSearch) :
$where .' AND '. implode(' AND ', $columnSearch);
}
if ( $where !== '' ) {
$where = 'WHERE '.$where;
}
return $where;
}
static function simple ( $request, $sql_details, $table, $primaryKey, $columns )
{
$bindings = array();
$db = self::sql_connect( $sql_details );
// Build the SQL query string from the request
$limit = self::limit( $request, $columns );
$order = self::order( $request, $columns );
$where = self::filter( $request, $columns, $bindings );
// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,
"SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", self::pluck($columns, 'db'))."`
FROM `$table`
$where
$order
$limit"
);
// Data set length after filtering
$resFilterLength = self::sql_exec( $db,
"SELECT FOUND_ROWS()"
);
$recordsFiltered = $resFilterLength[0][0];
// Total data set length
$resTotalLength = self::sql_exec( $db,
"SELECT COUNT(`{$primaryKey}`)
FROM `$table`"
);
$recordsTotal = $resTotalLength[0][0];
/*
* Output
*/
return array(
"draw" => intval( $request['draw'] ),
"recordsTotal" => intval( $recordsTotal ),
"recordsFiltered" => intval( $recordsFiltered ),
"data" => self::data_output( $columns, $data )
);
}
static function sql_connect ( $sql_details )
{
try {
$db = @new PDO(
"mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
$sql_details['user'],
$sql_details['pass'],
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
}
catch (PDOException $e) {
self::fatal(
"An error occurred while connecting to the database. ".
"The error reported by the server was: ".$e->getMessage()
);
}
return $db;
}
static function sql_exec ( $db, $bindings, $sql=null )
{
// Argument shifting
if ( $sql === null ) {
$sql = $bindings;
}
$stmt = $db->prepare( $sql );
//echo $sql;
// Bind parameters
if ( is_array( $bindings ) ) {
for ( $i=0, $ien=count($bindings) ; $i<$ien ; $i++ ) {
$binding = $bindings[$i];
$stmt->bindValue( $binding['key'], $binding['val'], $binding['type'] );
}
}
// Execute
try {
$stmt->execute();
}
catch (PDOException $e) {
self::fatal( "An SQL error occurred: ".$e->getMessage() );
}
// Return all
return $stmt->fetchAll();
}
static function fatal ( $msg )
{
echo json_encode( array(
"error" => $msg
) );
exit(0);
}
static function bind ( &$a, $val, $type )
{
$key = ':binding_'.count( $a );
$a[] = array(
'key' => $key,
'val' => $val,
'type' => $type
);
return $key;
}
static function pluck ( $a, $prop )
{
$out = array();
for ( $i=0, $len=count($a) ; $i<$len ; $i++ ) {
$out[] = $a[$i][$prop];
}
return $out;
}
}
我的问题,
第一
值protocolo和assunto 它们没有被加载
{ data: "protocolo" },
{ data: "assunto" },
二
如何在隐藏字段中加载id_protocolo值?
<input type="hidden" id="idcontrib" name="idcontrib" value="id_protocolo">
答案 0 :(得分:0)
您正在使用jQuery&#34; DataTables&#34;插入。事实上,你的jQuery和服务器代码看起来都像是基于文档中的示例代码(www.datatables.net)。
我要检查的第一件事是你的jQuery是否甚至与你的服务器通话。它甚至看到&#34; server_processing / protocolos.php&#34;?
执行此操作的最佳方法是将代码扩展回最小行数 - 只是为了测试连接是否正在发生。在文档中,他们有一些像这样的JavaScript:
$(document).ready(function() { $('#example').dataTable( { "processing": true, "serverSide": true, "ajax": "scripts/server_processing.php" } ); } );
我会从这开始尝试获得一个'#34; hello world&#34;从服务器脚本返回。在您知道任何类型的数据从服务器返回之前,很难说出代码中可能有什么用处。
答案 1 :(得分:0)
解决
SCRIPT
$(document).ready(function() {
var otable = $('#datatable_fixed_column').DataTable({
"processing": true,
"serverSide": true,
"ajax": "server_processing/contribuintes.php",
"order": [[ 2, "asc" ]],
"columnDefs": [
{ "width": "5%", "targets": 0 },
{ "width": "10%", "targets": 1 },
{ "width": "15%", "targets": 2 },
{ "width": "20%", "targets": 3 },
{ "width": "10%", "targets": 4 },
{ "width": "20%", "targets": 5 },
{ "width": "5%", "targets": 6 },
{ "render": actionlinks,
"data": null,
"targets": [7], "width": "15%", "targets": 7 },
],
});
function actionlinks(data, type, full) {
return '<form method="post" action=""><input type="hidden" id="idcontrib" name="idcontrib" value="' + full[0] + '"><button type="submit" class="btn btn-warning btn-xs" name="edit_contrib">Editar</button> <button type="submit" class="btn btn-danger btn-xs" name="exc_contrib">Excluir</button> <button type="submit" class="btn btn-info btn-xs" name="ativa_contrib">Reativar</button></form> ';
}
表
<table id="datatable_fixed_column" class="table table-striped table-bordered" width="100%">
<thead>
<tr>
<th>ID</th>
<th>CPF</th>
<th>Nome</th>
<th>Sobrenome</th>
<th>Celular</th>
<th>email</th>
<th>status</th>
<th>Ação</th>
</tr>
</thead>
</table>
服务器端
$table = 'tbl_contribuintes';
$primaryKey = 'id_contrib';
$columns = array(
array( 'db' => 'id_contrib', 'dt' => 0 ),
array( 'db' => 'cpf', 'dt' => 1 ),
array( 'db' => 'fnome', 'dt' => 2 ),
array( 'db' => 'lnome', 'dt' => 3 ),
array( 'db' => 'celular1', 'dt' => 4 ),
array( 'db' => 'email', 'dt' => 5 ),
array( 'db' => 'id_status', 'dt' => 6 ),
);
$sql_details = array(
'user' => 'root',
'pass' => 'XXX',
'db' => 'XXX',
'host' => 'localhost'
);
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
安德森·托雷斯