Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the position of buttons in DataTables

I have the following codes:

var dataSet = [
  ["Airi Satou", "Accountant", "Tokyo", "5407", "2008/11/28", "$162,700"],
  ["Angelica Ramos", "Chief Executive Officer (CEO)", "London", "5797", "2009/10/09", "$1,200,000"],
  ["Gavin Joyce", "Developer", "Edinburgh", "8822", "2010/12/22", "$92,575"],
  ["Jennifer Chang", "Regional Director", "Singapore", "9239", "2010/11/14", "$357,650"],
  ["Brenden Wagner", "Software Engineer", "San Francisco", "1314", "2011/06/07", "$206,850"],
  ["Fiona Green", "Chief Operating Officer (COO)", "San Francisco", "2947", "2010/03/11", "$850,000"],
  ["Shou Itou", "Regional Marketing", "Tokyo", "8899", "2011/08/14", "$163,000"],
  ["Martena Mccray", "Post-Sales support", "Edinburgh", "8240", "2011/03/09", "$324,050"],
  ["Unity Butler", "Marketing Designer", "San Francisco", "5384", "2009/12/09", "$85,675"]
];

$(document).ready(function() {
  $('#example').DataTable({
    dom: 'Blfrtip',
    data: dataSet,
    buttons: ['copy', 'excel'],
    columns: [{
      title: "Name"
    }, {
      title: "Position"
    }, {
      title: "Office"
    }, {
      title: "Extn."
    }, {
      title: "Start date"
    }, {
      title: "Salary"
    }]
  });
});
<link href="https://cdn.datatables.net/r/dt/jq-2.1.4,dt-1.10.9,b-1.0.3,b-flash-1.0.3/datatables.min.css" rel="stylesheet"/>
<script src="https://cdn.datatables.net/r/dt/jq-2.1.4,dt-1.10.9,b-1.0.3,b-flash-1.0.3/datatables.min.js"></script>

<table id="example" class="display" width="100%">
<tfoot><tr></tr></tfoot>
</table>

What I want to do is to change the button location to the center, like this:

enter image description here

How can achieve that?

like image 235
neversaint Avatar asked Sep 10 '15 01:09

neversaint


People also ask

What is a button option data type?

The elements of that array are button-options data types, as defined here. As a string the value given is used a the button label (i.e. the text that the end user sees inside the button), and the activation function is set to call submit () automatically (it is the most commonly used function for a button!).

How do I append a DataTable to a jQuery?

Use jQuery appendTo (), like this: //create DataTable first, then... There’s also a prependTo () that you can consider.

How are the buttons configured individually?

How those buttons are each individually configured is defined by this button-options data type. Each button configuration can be defined and configured in two different ways: string - A simple string which will be used as the label for the button and the activation function will trigger a call to submit ().


1 Answers

SOLUTION

You can use the following CSS rule. Please note that it will target all tables on the page. Use more specific rule to target one table only.

.dataTables_wrapper .dt-buttons {
  float:none;  
  text-align:center;
}

Also in order for it to work you need to use dom: 'lfBrtip'. See dom option for more details.

DEMO

var dataSet = [
  ["Airi Satou", "Accountant", "Tokyo", "5407", "2008/11/28", "$162,700"],
  ["Angelica Ramos", "Chief Executive Officer (CEO)", "London", "5797", "2009/10/09", "$1,200,000"],
  ["Gavin Joyce", "Developer", "Edinburgh", "8822", "2010/12/22", "$92,575"],
  ["Jennifer Chang", "Regional Director", "Singapore", "9239", "2010/11/14", "$357,650"],
  ["Brenden Wagner", "Software Engineer", "San Francisco", "1314", "2011/06/07", "$206,850"],
  ["Fiona Green", "Chief Operating Officer (COO)", "San Francisco", "2947", "2010/03/11", "$850,000"],
  ["Shou Itou", "Regional Marketing", "Tokyo", "8899", "2011/08/14", "$163,000"],
  ["Martena Mccray", "Post-Sales support", "Edinburgh", "8240", "2011/03/09", "$324,050"],
  ["Unity Butler", "Marketing Designer", "San Francisco", "5384", "2009/12/09", "$85,675"]
];

$(document).ready(function() {
  $('#example').DataTable({
    dom: 'lfBrtip',
    data: dataSet,
    buttons: ['copy', 'excel'],
    columns: [{
      title: "Name"
    }, {
      title: "Position"
    }, {
      title: "Office"
    }, {
      title: "Extn."
    }, {
      title: "Start date"
    }, {
      title: "Salary"
    }]
  });
});
.dataTables_wrapper .dt-buttons {
  float:none;  
  text-align:center;
}
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/dt/jq-2.1.4,jszip-2.5.0,dt-1.10.9,b-1.0.3,b-flash-1.0.3,b-html5-1.0.3/datatables.min.css"/>
 
<script type="text/javascript" src="https://cdn.datatables.net/r/dt/jq-2.1.4,jszip-2.5.0,dt-1.10.9,b-1.0.3,b-flash-1.0.3,b-html5-1.0.3/datatables.min.js"></script>

<table id="example" class="display" width="100%">
<tfoot><tr></tr></tfoot>
</table>
like image 110
Gyrocode.com Avatar answered Nov 04 '22 02:11

Gyrocode.com