将列数据设为超链接(dataTable JQUERY)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30489307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
make column data as hyperlink (dataTable JQUERY)
提问by Undisputed007
I am trying to make a column as hyperlink with datatable but no success.
我正在尝试将一列作为带有数据表的超链接,但没有成功。
function successCallback(responseObj){
函数成功回调(responseObj){
$(document).ready(function() {
$('#example').dataTable( {
"data":responseObj ,
"bDestroy": true,
"deferRender": true ,
"columns": [
{ "data": "infomation" },
{ "data": "weblink" },
]
} );
} );
}
}
I need the weblink to display the link and be an hyperlink in that column so users can click and be redirected to another page. I looked into renderbut with less information there on links, i can't succeed to do it.
我需要网络链接来显示链接并成为该列中的超链接,以便用户可以单击并重定向到另一个页面。我研究了渲染,但链接上的信息较少,我无法成功。
I also looked into this examplebut it wasn't very helpful.
我也研究了这个例子,但它不是很有帮助。
回答by Gyrocode.com
Use columns.render
API method to dynamically produce content for a cell.
使用columns.render
API 方法为单元格动态生成内容。
$('#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;
}
}
]
});
See this examplefor code and demonstration.
有关代码和演示,请参阅此示例。
回答by Adarsh Madrecha
If you are looking to add link based on other column data then can use the below approach.
如果您希望根据其他列数据添加链接,则可以使用以下方法。
$('#example').dataTable({
"data": responseObj,
"columns": [
{ "data": "information" },
{
"data": "weblink",
"render": function(data, type, row, meta){
if(type === 'display'){
data = '<a href="' + row.myid + '">' + data + '</a>';
}
return data;
}
}
]
});
I have just changed the render function. data
refers to only current column data, while row
object refers to entire row of data. Hence we can use this to get any other data for that row.
我刚刚更改了渲染功能。data
仅指当前列数据,而row
对象指的是整行数据。因此,我们可以使用它来获取该行的任何其他数据。
回答by ozz
$('#example').dataTable( {
"columnDefs": [ {
"targets": 0,
"data": "download_link",
"render": function ( data, type, full, meta ) {
return '<a href="'+data+'">Download</a>';
}
} ]
} );
From the documentation. It is quite clear and straightforward to me, what is it specifically that you do do not understand? What errors do you see?
从文档中。对我来说很清楚和直截了当,你不明白的具体是什么?你看到了什么错误?
For a more complete example, see here
有关更完整的示例,请参见此处