jquery DataTables按钮

时间:2016-03-25 19:01:59

标签: jquery datatables

我在创建自定义按钮和编辑buttons.dom.button属性时遇到问题。这是我正在使用的代码;

$(document).ready(function() {

function buildTable(tableName) {
   return $('#'+tableName).DataTable( {
        dom: 'ifrt',
        paging: false,
        lengthChange: true,
        responsive: true,
        columnDefs: [
            {
                "targets": [ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 ],
                "visible": false,
                "searchable": false
            },
            { 
                "orderable": false, 
                "targets": [0, 3, 4, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22] 
            }           
        ],

        buttons: [

            'excel',
            {
                extend: 'columnToggle',
                columns: 0,
                text: 'show/hide pics'
            }            
        ],

        buttons: {
           dom: {
              button:{
                 tag: 'li'
              }

           }
        }


    });
}

var tablesMen = buildTable('menTable');

$('#menTable_wrapper').prepend('<div class="dropdown"><button class=btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">dropdown<span class="caret"></span></button><ul class="dropdown-menu"></ul></div>');

tablesMen.buttons().container().appendTo($('.dropdown-menu'));

当我添加

时,按钮会重置为默认值(excel,pdf,copy等)
buttons: {
           dom: {
              button:{
                 tag: 'li'
              }

           }
        }

我希望这是有道理的。

1 个答案:

答案 0 :(得分:1)

你有一个名为&#34;按钮&#34;用

声明

buttons: [然后立即将其替换为对象buttons: {

EDIT2 :这是您的功能,我将其修改为包含dom属性,并添加了自定义按钮作为示例:

  function buildTable(tableName) {
    return $('#' + tableName).DataTable({
      dom: 'Bfrtip',
      paging: false,
      lengthChange: true,
      responsive: true,
      columnDefs: [{
        "targets": [1, 2, 3],
        "visible": true,
        "searchable": false
      }, {
        "orderable": false,
        "targets": [0, 4, 5]
      }],
      buttons: {
        dom: {
          button: {
            tag: 'li'
          }
        },
        buttons: [{
          text: 'My button',
          action: function(e, dt, node, config) {
            alert('Button activated');
          }
        }, {
          extend: 'excel',
          text: 'Save current page',
          exportOptions: {
            modifier: {
              page: 'current'
            }
          }
        }]
      }
    });
  }

编辑:注意你在字符串中也缺少引号,这里是更正的:

$('#menTable_wrapper').prepend('<div class="dropdown"><button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">dropdown<span class="caret"></span></button><ul class="dropdown-menu"></ul></div>');