使用 jQuery DataTables 向工具栏添加按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32549987/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:53:56  来源:igfitidea点击:

Adding a button to toolbar with jQuery DataTables

jquerydatatables

提问by Abrar Jahin

I am trying to add a button to my toolbar of my datatable. So, my datatableis:

我想一个按钮添加到我的我的工具栏上的数据表。所以,我的数据表是:

var dataTable =  $('#employee-grid').DataTable(
{
    processing: true,
    serverSide: true,
    ajax: "employee-grid-data.php", // json datasource for AJAX Data

    "pagingType": "full_numbers",   //Adding Last and First in Pagination
    stateSave: true,
    "language":{                    //Custom Message Setting
                    "lengthMenu": "Display _MENU_ records per page",    //Customizing menu Text
                    "zeroRecords": "Nothing found - sorry",             //Customizing zero record text - filtered
                    "info": "Showing page _PAGE_ of _PAGES_",           //Customizing showing record no
                    "infoEmpty": "No records available",                //Customizing zero record message - base
                    "infoFiltered": "(filtered from _MAX_ total records)"   //Customizing filtered message
                },
    "lengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],        //For customizing number of data sets per page

});

And what I have done is like this:

我所做的是这样的:

    $(document).ready(function()
    {
        var dataTable =  $('#employee-grid').DataTable(
        {
            processing: true,
            serverSide: true,
            ajax: "employee-grid-data.php", // json datasource for AJAX Data

            "pagingType": "full_numbers",   //Adding Last and First in Pagination
            stateSave: true,
            "language":{                    //Custom Message Setting
                            "lengthMenu": "Display _MENU_ records per page",    //Customizing menu Text
                            "zeroRecords": "Nothing found - sorry",             //Customizing zero record text - filtered
                            "info": "Showing page _PAGE_ of _PAGES_",           //Customizing showing record no
                            "infoEmpty": "No records available",                //Customizing zero record message - base
                            "infoFiltered": "(filtered from _MAX_ total records)"   //Customizing filtered message
                        },
            "lengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],        //For customizing number of data sets per page
            "dom": '<"toolbar">frtip'
        });

        $("div.toolbar").html('<button type="button" id="any_button">Click Me!</button>');
    } );

But I am finding something like this:

但我发现这样的事情:

enter image description here

在此处输入图片说明

But I like to have something like this-

但我喜欢有这样的东西——

enter image description here

在此处输入图片说明

Can anyone please help?

有人可以帮忙吗?

回答by Gyrocode.com

SOLUTION

解决方案

Use the code below:

使用下面的代码:

JavaScript:

JavaScript:

var table = $('#example').DataTable({
   // ... skipped ...
   dom: 'l<"toolbar">frtip',
   initComplete: function(){
      $("div.toolbar")
         .html('<button type="button" id="any_button">Click Me!</button>');           
   }       
});   

CSS:

CSS:

.toolbar {
    float:left;
}

DEMO

演示

See this jsFiddlefor code and demonstration.

有关代码和演示,请参阅此 jsFiddle