dtlc84438 2018-11-19 02:00
浏览 179
已采纳

我需要添加到动态DataTables的超链接

I have a table that is pulling data from my database and displaying it from a database table. I want to add hyperlinks to the dog name. All of the documentation I have found shows how to do it with static data but not dynamic example below:

var responseObj = [
    { "information": "A1", "weblink": "http://www.microsoft.com" },
    { "information": "A2", "weblink": "http://www.yahoo.com" },
    { "information": "A3", "weblink": "http://www.google.com" },
    { "information": "A4", "weblink": "http://www.duckduckgo.com" }
];

$('#example').dataTable({
   "data": responseObj,
   "columns": [
      { "data": "information" }, 
      { 
         "data": "weblink",
         "render": function(data, type, row, meta){
            if(type === 'display'){
                data = '<a href="' + data + '">' + data + '</a>';
            }

            return data;
         }
      } 
   ]
});

I need to be able to select the data and have it go to my link. This is my current code.

$(document).ready( function () {
    var dataTable=$('#example').DataTable({

        "processing": true,
        "serverSide": true,
        "ajax": {
            url:"fetch.php",
            type:"post"
        }

    });

} );
</script

In my fetch.php I have this.

$request = $_REQUEST;
$col = array (
    1 => 'Dog_Name',
    0 => 'Dog_Number',
    2 => 'Breed',
    3 => 'Sex'
    );

    $sql ="SELECT * FROM dog_profiles";

    $query=mysqli_query($conn,$sql);

    $totalData=mysqli_num_rows($query);

    $totalFilter=$totalData;

//Search

$sql =("SELECT * FROM dog_profiles");
if(!empty($request['search']['value'])){
    $sql.=" WHERE (Dog_Number Like '".$request['search']['value']."%' ";
    $sql.=" OR Dog_Name Like '".$request['search']['value']."%' ";
    $sql.=" OR Breed Like '".$request['search']['value']."%' ";
    $sql.=" OR Sex Like '".$request['search']['value']."%' )";
}
$query=mysqli_query($conn,$sql);
$totalData=mysqli_num_rows($query);

//Order
$sql.=" ORDER BY ".$col[$request['order'][0]['column']]."   ".$request['order'][0]['dir']."  LIMIT ".
    $request['start']."  ,".$request['length']."  ";

$query=mysqli_query($conn,$sql);


$data = array();



while($row=mysqli_fetch_array($query)){
    $subdata=array();
    $subdata[]=$row[0]; //dog number
    $subdata[]=$row[1]; //name
    $subdata[]=$row[2]; //breed
    $subdata[]=$row[3]; //sex 
    $data[]=$subdata;
}


$json_data=array(
    "draw"              =>  intval($request['draw']),
    "recordsTotal"      =>  intval($totalData),
    "recordsFiltered"   =>  intval($totalFilter),
    "data"              =>  $data

);
echo json_encode($json_data);

Any insight would be helpful I am quite new to using Datatables.

  • 写回答

1条回答 默认 最新

  • dsjgk330337 2018-11-19 06:12
    关注

    I think you need to update the code in while loop.

    Just try the below code,

    while($row=mysqli_fetch_array($query)){
        $subdata=array();
        $subdata[]=$row[0]; //dog number
        $subdata[]="<a href='".$row[1]."'>".$row[1]."</a>";
        $subdata[]=$row[2]; //breed
        $subdata[]=$row[3]; //sex 
        $data[]=$subdata;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容