我希望将搜索关键字添加到自定义按钮操作元素的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);
}); 这似乎失败了,因为action函数实际上并没有将按钮分配给href属性。
任何关于如何动态修改操作函数或其他方法的建议都将不胜感激。
发布于 2019-03-28 02:05:31
使用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');
}
}
],https://stackoverflow.com/questions/55388790
复制相似问题