我正在开发特定用户的历史记录,并且希望使用dataTables完成。但是,我找不到使行或特定单元格可单击的方式。我需要使用单独的点击打开单独的链接。任何帮助,将不胜感激。提前致谢 !!!
编辑::如果我单击一行,则需要该行的所有数据,这不是问题。我可以的 我需要知道的是使用该特定行数据发出$ .ajax()请求。我认为这可以。但是,非常高兴知道如何在单击行时在新选项卡中打开链接。
$(document).ready(function() {
var dataSet = [
[]
];
$.ajax({
type: 'POST',
url: "webservices/view_patient_medical_history.php",
async: false,
//data: {'log_id': data},
success: function(response) {
dataSet = JSON.parse(response);
}
});
// var dataSet_arr = jQuery.makeArray(dataSet['responseText']);
$('#patient_medical_history').DataTable({
data: dataSet,
columns: [{
title: "Patient ID",
class: "center"
}, {
title: "Current Medications",
class: "center"
}, {
title: "Allergies",
class: "center"
}, {
title: "Diabetes",
class: "center"
}, {
title: "Asthma",
class: "center"
}, {
title: "Arthritis",
class: "center"
}, {
title: "High Blood Pressure",
class: "center"
}, {
title: "Kidney Problem",
class: "center"
}, {
title: "Liver Problem",
class: "center"
}, {
title: "Heart Problem",
class: "center"
}, {
title: "Other Problems",
class: "center"
}, {
title: "Present Problem",
class: "center"
}, {
title: "Last Updated",
class: "center"
}],
"scrollX": true,
//"paging": false,
"info": false,
//"lengthMenu": false,
dom: 'lBfrtip',
buttons: [
'copy', 'pdf', 'print'
]
/*"paging": false,
"info": false,
dom: 'Bfrtip',
buttons: [
'excel', 'pdf', 'print'
]*/
});
$('th').css("white-space", "nowrap");
});
要激活单元格上的点击,您必须使用委托的事件处理程序-这将适用于任何dataTable:
$('.dataTable').on('click', 'tbody td', function() {
//get textContent of the TD
console.log('TD cell textContent : ', this.textContent)
//get the value of the TD using the API
console.log('value by API : ', table.cell({ row: this.parentNode.rowIndex, column : this.cellIndex }).data());
})
检索被点击的行的数据可以通过以下方式完成
$('.dataTable').on('click', 'tbody tr', function() {
console.log('API row values : ', table.row(this).data());
})
如果要通过AJAX发送行内容,则应将数组转换为对象,然后将其包含为data
:
$('.dataTable').on('click', 'tbody tr', function() {
var data = table.row(this).data().map(function(item, index) {
var r = {}; r['col'+index]=item; return r;
})
//now use AJAX with data, which is on the form [ { col1 : value, col2: value ..}]
$.ajax({
data: data,
url: url,
success: function(response) {
...
}
})
如果只想在单元格中使用普通链接,则target: _blank
可以使用render
:
columns: [
{ title: "Patient ID", class: "center", render: function(data, type, full, meta) {
return '<a href="showdata/id?'+data+'" target=_blank>Show patient</a>'
}
},
首先,请确保更改dataTable初始化的代码,以将其保存到这样的变量中
var oPatientMedicalHistory = $('#patient_medical_history').DataTable({
//your stuff
});
然后,您可以将click事件分配给所有这样的行
编辑:正如Gyrocode.com指出的那样,我们应该使用委托事件处理程序,因为我们在分页时会动态创建tr。因此,代码应如下所示。
$('#patient_medical_history tbody').on('dblclick','tr', function() {
var currentRowData = oPatientMedicalHistory.row(this).data();
// alert(currentRowData[0]) // wil give you the value of this clicked row and first index (td)
//your stuff goes here
});
这必须帮助您。
您需要在数据表的cells(td)上单击添加事件处理程序,并且必须定义要在该事件处理程序中处理的操作。
是浏览和玩耍的绝佳来源。
我们正在使用:
rowCallback: function (row: Node /*, data: any[] | Object, index: number */) {
// Intercept clicking of datatable links to avoid a full page refresh
$('a', row).click(function (e) {
e.preventDefault()
// const href = $(this).attr('href')
// parent.router.navigate([href])
})
// Navigate using anchor in cell to make entire cell clickable
$('td', row).click(function (/* e */) {
const anchor = $(this).find('a:first')[0]
if (anchor) {
const href = $(anchor).attr('href')
parent.router.navigate([href])
}
})
return row
}
不确定这是否是最好的方法,但确实可以。愿主祝福你:)
抱歉,这是TypeScript,但是它很容易转换为JS。
在我这边,使用row: this.parentNode.rowIndex
没有用。我this.parentNode
改用像魅力一样的东西
本文地址:http://jquery.askforanswer.com/ruheshishujubiaoxinghuodanyuangekedianji.html
文章标签:datatable , datatables , jquery
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
文章标签:datatable , datatables , jquery
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!