2016-06-13 31 views
0

我有一個填充數據網格表的腳本。我想讓每個顯示的行的超鏈接。製作數據網格表中每個行的超鏈接

超鏈接看起來應該像<a href"index.php?id=(id of the row)">

我怎麼做我目前的腳本。

這裏是我的腳本:

<?php 
// initilize all variable 
$params = $columns = $totalRecords = $data = array(); 
$params = $_REQUEST; 

//define index of column 
$columns = array( 
    0 => 'id', 
    1 => 'name', 
); 
$where = $sqlTot = $sqlRec = ""; 

// check search value exist 
if(!empty($params['search']['value'])) { 
    $where .=" WHERE "; 
    $where .=" name LIKE '".$params['search']['value']."%'"; 
} 

// getting total number records without any search 
$sql = "SELECT id, name FROM `customers`"; 
$sqlTot .= $sql; 
$sqlRec .= $sql; 

//concatenate search sql if value exist 
if(isset($where) && $where != '') { 
    $sqlTot .= $where; 
    $sqlRec .= $where; 
} 

$sqlRec .= " ORDER BY ". $columns[$params['order'][0]['column']]." ".$params['order'][0]['dir']." LIMIT ".$params['start']." ,".$params['length']." "; 
$queryTot = mysqli_query($conn, $sqlTot) or die("database error:". mysqli_error($conn)); 
$totalRecords = mysqli_num_rows($queryTot); 
$queryRecords = mysqli_query($conn, $sqlRec) or die("error to fetch customers data"); 

//iterate on results row and create new index array of data 
while($row = mysqli_fetch_row($queryRecords)) { 
    $data[] = $row; 
} 

$json_data = array(
"draw"   => intval($params['draw']), 
"recordsTotal" => intval($totalRecords), 
"recordsFiltered" => intval($totalRecords), 
"data"   => $data // total data array 
); 

echo json_encode($json_data); // send data as json format 
?> 

編輯1:

我在這裏顯示的數據:

<table id="employee_grid" class="display" width="100%" cellspacing="0"> 
<thead> 
    <tr> 
    <th>ID</th> 
    <th>Name</th> 
    </tr> 
</thead> 
</table> 

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#employee_grid').DataTable({ 
    "bProcessing": true, 
    "serverSide": true, 
    "ajax":{ 
    url :"get.php", // json datasource 
    type: "post", // type of method ,GET/POST/DELETE 
    error: function(){ 
    $("#employee_grid_processing").css("display","none"); 
    } 
    } 
    }); 
}); 
</script> 
+0

仍然沒有修復 – John

回答

0

我通過改變javascript來解決它。

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#employee_grid').DataTable({ 
    "bprocessing": true, 
    "serverSide": true, 
     "ajax": { 
      "url": "post1.php", 
      "type": "POST", 
      "error": function(){ 
       $("#employee_grid_processing").css("display","none"); 
      } 
     }, 
     "columnDefs": [ { 
     "targets": 0, 
     "render": function (data, type, full, meta) { 
       return '<a href="http://www.example.com/'+data+'">Link</a>'; 
       } 
      } 
     ]    
    }); 
}); 
</script> 

此代碼將與文本Link和第一列的原始結果替換第一列將在超鏈接中使用。

0

我認爲你必須編寫自定義mRender得到此鏈接。並在您的表頭

$(document).ready(function() { 
    $('#employee_grid').DataTable({ 
    "bprocessing": true, 
    "serverSide": true, 
     "ajax": { 
      "url": "get.php", 
      "type": "POST", 
      "error": function(){ 
       $("#employee_grid_processing").css("display","none"); 
      } 
     }, 
     "columns": [ 
      { "data": "id" }, 
      { "data": "name" }, 
      { "data": "id", "render": function (data) { 
       return '<a href="http://domain.com/'+data+'">Link</a>'; 
       } 
      } 
     ]    
    }); 
}); 

PS請從您的代碼中刪除我的舊建議增加一個額外的 <th>Link</th>

+0

我沒有得到超鏈接。表格未更改 – John

+0

數據網格中的行仍然沒有超鏈接 – John

+0

請參閱我的第一篇文章中的編輯1' – John