We'll pick up right where we left off with our data table. If you'd like to save your basic example, just save a copy of the file. Then follow these steps to customize the appearance of your data grid:
After flipping to the Roll Your Own tab, you'll see that there are settings for fonts, colors, corners, headers, and so on. Make whatever adjustments you'd like to get the theme looking just the way you'd like. Feel free to play and experiment. If you go too far and get to something you don't like, it's easy to flip back to the Gallery tab and select the prebuilt theme again, stripped of any of your customizations, then start again.
Keep in mind that any of your customizations will be lost if you re-select a prebuilt theme. Once you get something you like, be sure to move on to step 3 to save it.
Since we're only here for a theme, we can go ahead and click on the Deselect all components link near the top of the page.
css
folder that contains our theme folder with a CSS file and images inside it. We've also got a development-bundle
folder, an HTML file, and a js
folder that contains jQuery and a jQuery UI file.All we need out of all of this is our theme. Copy your theme folder to the styles directory of your own project. My theme folder is named cupertino
since that's the theme I chose. If you selected a different theme, your theme folder will be called something else. It will be easy to find, though, as it will be the only folder inside the css
folder.
<head>
section, attach your theme CSS file before the demo_table.css
file we attached in the previous example.<link rel="stylesheet" href="styles/cupertino/jquery-ui-1.8.16.custom.css"/> <link rel="stylesheet" href="styles/demo_table.css"/>
You can read up on styling the DataTables plugin in the documentation Allan Jardine has made available at http://datatables.net/styling/.
Back inside the DataTables plugin files, look inside the css
folder inside the media
folder to find the demo_table_jui.css
file. Copy that to your own styles folder and update your <link>
tag to link to this version of the demo_table.css
instead as follows:
<link rel="stylesheet" href="styles/cupertino/jquery-ui-1.8.16.custom.css"/>
<link rel="stylesheet" href="styles/demo_table_jui.css"/>
dataTable()
method that we want to use jQuery UI. Head back into your scripts.js
file and we'll add a pair of curly brackets and pass a key/value pair to enable jQuery UI styling for our data table:$(document).ready(function(){
$('#book-grid').dataTable({
'bJQueryUI': true
});
});
If you refresh the page in the browser now, you'll see that the data grid is now using a style that's consistent with the widgets we saw on the jQuery UI Themeroller page:
demo_table_jui.css
. There are just a couple of lines to update. First, we'll find line 281 where the colors for the zebra-striping for the table are defined and update those to the colors we'd like to use as follows:tr.odd { background-color: #f1f7fb; } tr.even { background-color: white; }
I'm going with a pale blue for odd rows and white for even rows to match the Cupertino style I selected earlier. Feel free to choose colors that match your own chosen theme.
tr.odd td.sorting_1 { background-color: #d6e7f4; }
tr.even td.sorting_1 { background-color: #e4eff8; }
You can select your own colors that coordinate with your own chosen theme.
Now, if you refresh the page in the browser, you'll see that the zebra-striping pattern of the table fits with our Themeroller theme.
dataTable
method to replace the buttons with pagination numbers as follows:$(document).ready(function(){
$('#book-grid').dataTable({
'sPaginationType': 'full_numbers',
'bJQueryUI': true
});
});
Remember to separate each key/value pair with a comma, but not to place a comma after the last key/value pair.
$(document).ready(function(){
$('#book-grid').dataTable({
'sPaginationType': 'full_numbers',
'bJQueryUI': true,
'bFilter': false
});
});
Refresh the page in the browser and you'll see that the search box is gone.
$(document).ready(function(){
$('#book-grid').dataTable({
'sPaginationType': 'full_numbers',
'bJQueryUI': true,
'bFilter': false,
'aaSorting': [[4, 'desc']]
});
});
The key we're using is called 'aaSorting'
, and the value is the column number and sort direction inside two sets of square brackets. Don't forget that JavaScript starts counting at 0, not 1. So the fifth column in our table is actually column 4. Then, we want the highest number at the top, so we pass 'desc'
for descending order.
Refresh the page in the browser and you'll see that the books are now in order from highest sales to lowest sales. Note also that this default sort order doesn't affect your site visitor's ability to sort the table by any of the other columns in any order they'd like. The site visitor can still interact with your table. We're just re-defining the default view in a way that makes the most sense for the data we're presenting.
What just happened?
We took our basic data grid and took it a step further by customizing the appearance and behavior of the plugin. We learned how to use the jQuery UI Themeroller to create a custom theme for our data grid. Then we learned how to replace the simple pagination buttons with page numbers, disable searching the table, and how to set a default sort for the data grid.
3.144.127.232