PHP / JQUERY - 为列数据创建超链接

Posted

技术标签:

【中文标题】PHP / JQUERY - 为列数据创建超链接【英文标题】:PHP / JQUERY - Creating hyperlink for a column's data 【发布时间】:2017-10-12 00:05:31 【问题描述】:

我正在使用 datatables jQuery 插件来归档我的 Web 表单。我的 javascript 如下:

<script type="text/javascript" language="javascript" src="js/jquery.js"</script>
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" src="jquery-ui.js"</script>
<script type="text/javascript" language="javascript" >
$(document).ready(function() 
var dataTable = $('#employee-grid').DataTable( 
"scrollY": 500,
"scrollX": true,
"processing": true,
"serverSide": true,
"ajax":
url :"employee-grid-data.php", // json datasource
type: "post",  // method  , by default get
error: function()  // error handling
$(".employee-grid-error").html("");
$("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
$("#employee-grid_processing").css("display","none");

);
);
</script>

桌子运行良好。问题是,我想放一个超链接:

 `<a href="admin_change_details.php?changeid=' . $row['change_id'] . '"title=Details>' . $row['change_id'] . '</a>` 

在“更改 ID”字段中的表格数据上,以便用户查看更多详细信息。 JSON数据存储文件如下:

<?php
include 'config.php'; 
$requestData= $_REQUEST;
$columns = array( 
0 =>'change_id',
    1 =>'customer_name', 
    2 => 'change_requestor',
    3=> 'date_cr_raised',
    4=> 'person_to_complete_change',
    5=> 'cpm_ticket',
    6=> 'out_of_hours',
    7=> 'category_of_change',
    8=> 'requestor_impact',
    9=> '',
    10=> 'approval_disposition',
    11=> 'approval_date',
    12=> 'approval_impact',
    13=> 'approval_name',
    14=> 'approval_expiry_date',
    15=> 'status_requested',
    16=> 'date_status_requested',
    17=> '',
    18=> 'review_change_success',
    19=> 'review_change_date',
    20=> 'review_nable',
    21=> 'review_date',
    22=> 'review_by',
    23=> '',
    24=> 'closed',
    25=> 'closed_by',
    26=> 'closed_date'
);

$sql = "SELECT * ";
$sql.=" FROM change_request_tbl";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get data");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  

$sql = "SELECT * ";
$sql.=" FROM change_request_tbl WHERE 1=1";
if( !empty($requestData['search']['value']) )    
    $sql.=" AND ( change_id LIKE '".$requestData['search']['value']."%' ";    
    $sql.=" OR customer_name LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR change_requestor LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR date_cr_raised LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR person_to_complete_change LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR cpm_ticket LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR out_of_hours LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR category_of_change LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR requestor_impact LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR approval_disposition LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR approval_impact LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR approval_name LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR approval_date LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR approval_expiry_date LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR status_requested LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR date_status_requested LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR review_date LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR review_change_date LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR review_by LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR review_change_success LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR review_nable LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR closed LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR closed_by LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR closed_date LIKE '".$requestData['search']['value']."%' )";


$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get data");
$totalFiltered = mysqli_num_rows($query); 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";

$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get data");

$data = array();
while( $row=mysqli_fetch_array($query) )   
    $nestedData=array(); 
    $nestedData[] = $row["change_id"];
    $nestedData[] = $row["customer_name"];
    $nestedData[] = $row["change_requestor"];
    $nestedData[] = $row["date_cr_raised"];
    $nestedData[] = $row["person_to_complete_change"];
    $nestedData[] = $row["cpm_ticket"];
    $nestedData[] = $row["out_of_hours"];
    $nestedData[] = $row["category_of_change"];
    $nestedData[] = $row["requestor_impact"];
    $nestedData[] = $row[""];
    $nestedData[] = $row["approval_disposition"];
    $nestedData[] = $row["approval_date"];
    $nestedData[] = $row["approval_impact"];
    $nestedData[] = $row["approval_name"];
    $nestedData[] = $row["approval_expiry_date"];
    $nestedData[] = $row["status_requested"];
    $nestedData[] = $row["date_status_requested"];
    $nestedData[] = $row[""];
    $nestedData[] = $row["review_change_success"];
    $nestedData[] = $row["review_change_date"];
    $nestedData[] = $row["review_nable"];
    $nestedData[] = $row["review_date"];
    $nestedData[] = $row["review_by"];
    $nestedData[] = $row[""];
    $nestedData[] = $row["closed"];
    $nestedData[] = $row["closed_by"];
    $nestedData[] = $row["closed_date"];

    $data[] = $nestedData;

$json_data = array(
            "draw"            => intval( $requestData['draw'] ),  
            "recordsTotal"    => intval( $totalData ),  
            "recordsFiltered" => intval( $totalFiltered ), 
            "data"            => $data   
            );
echo json_encode($json_data);  // send data as json format
?>

我的问题是,我能否通过 JSON 数据存储生成超链接,还是必须使用 JavaScript?

【问题讨论】:

【参考方案1】:

在创建 JSON 之前,您可以在将 change_id 存储在数组中时尝试以下操作:

$nestedData[] = '<a href="your link goes here">.'$row["change_id"].'<a>';

【讨论】:

以上是关于PHP / JQUERY - 为列数据创建超链接的主要内容,如果未能解决你的问题,请参考以下文章

jquery如何进行超链接

JQuery根据Text of Anchor标签创建超链接

将列数据作为超链接(dataTable JQUERY)

将列数据作为超链接(dataTable JQUERY)

jquery中的dataTable表格控件中如何插入超链接或者按钮?

如何用jquery中获取超链接中传的值