我想将搜索关键字添加到自定义按钮操作元素的 URL,以便将关键字作为 GET 参数传递。
我正在像这样初始化数据表:
var table = $('#list').DataTable( {
dom: 'lBfrtip',
buttons: [
{
text: 'Export to PDF',
className: 'export-to-pdf',
action: function ( e, dt, button, config ) {
window.open('generate_pdf.php?case_id=<?= $case_id? ?>','_blank');
}
并尝试使用以下方法将关键字附加到 URL:
// append keyword search values to pdf url
$('#list').on('search.dt', function() {
var keyword = $('.dataTables_filter input').val();
//FAILS HERE
var export_pdf_url = $(".export-to-pdf").attr("href");
// remove keywords if they already exist
removeURLParameter(export_pdf_url, 'keyword');
// append filter value to filtered pdf href
$(".export-to-pdf").attr("href", export_pdf_url + '&keyword='+keyword);
});
这似乎失败了,因为操作函数实际上并没有为按钮分配 href 属性。
如有任何关于如何动态修改 Action 函数或其他方法的建议,我们将不胜感激。
最佳答案
使用search()
在不带参数调用时返回当前应用的全局搜索的 API 方法。
然后在action
选项的回调函数中直接生成URL。
例如:
buttons: [
{
text: 'Export to PDF',
className: 'export-to-pdf',
action: function ( e, dt, button, config ) {
var query = dt.search();
window.open('generate_pdf.php?case_id=<?= $case_id? ?>&keyword=' + encodeURIComponent(query), '_blank');
}
}
],
关于javascript - 修改数据表按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55388790/