how to customize angular-datatables' style

All we need is an easy explanation of the problem, so here it is.

I’m new to angular, trying to use angular-datatables library http://l-lin.github.io/angular-datatables/#/angularWay, but don’t know how to control the style of the table, cause they are all angular directives, is it possible I can touch the HTML elements inside? like the example below, how can I remove the text next to search box? Also I’ve read API, couldn’t find how to hide the datatables_info on the buttom.

enter image description here

enter image description here


update

maybe I can hide them through CSS, but seems it’s impossible to add placeholder to the input element

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Search box text

You can do this in various ways, also by manipulating the injected DOM elements – but the “correct” way would be to alter the language settings. The default language object literal is

var lang = {
    "decimal":        "",
    "emptyTable":     "No data available in table",
    "info":           "Showing _START_ to _END_ of _TOTAL_ entries",
    "infoEmpty":      "Showing 0 to 0 of 0 entries",
    "infoFiltered":   "(filtered from _MAX_ total entries)",
    "infoPostFix":    "",
    "thousands":      ",",
    "lengthMenu":     "Show _MENU_ entries",
    "loadingRecords": "Loading...",
    "processing":     "Processing...",
    "search":         "Search:",
    "zeroRecords":    "No matching records found",
    "paginate": {
        "first":      "First",
        "last":       "Last",
        "next":       "Next",
        "previous":   "Previous"
    },
    "aria": {
        "sortAscending":  ": activate to sort column ascending",
        "sortDescending": ": activate to sort column descending"
    }
}

Change search to "" and include lang as language option :

.withOption('language', lang)

Hide the datatables_info at the bottom

You can disable the table information summary completely by omitting the i flag from the dom option. The default dom setting is lfrtip, so simply

.withDOM('lfrtp')

See both solutions in action here -> http://plnkr.co/edit/3WqPj1IW1h3zK37hF4dv?p=preview


add placeholder to the input element

The injected search box is located at .dataTables_filter input. You can use angular.element() or document.querySelector() to manipulate such DOM elements. To add a placeholder to the search box

.withOption('initComplete', function() {
   angular.element('.dataTables_filter input').attr('placeholder', 'Search ...');
})

add ng-bind or ng-click on the ‘previous’ and ‘next’ button

This is very tricky. The injected elements has nothing to do with angular – I believe it is somehow possible to add a ng-click to an element and then (re)$compile. However, the pagination buttons is recreated each and every time the table is redrawn, so the angularification’ would need to happen over and over. But you can easily facilitate events for the prev/next buttons without std angular directives :

.withOption('drawCallback', function() {
   angular.element('.paginate_button.previous').on('click', function() { alert('prev')} )
   angular.element('.paginate_button.next').on('click', function() { alert('next')} )             
})

There is also a page.dt event, fired when the active page changes :

angular.element('body').on('page.dt', function(e, api) {
   console.log('Page #'+(api._iDisplayStart / api._iDisplayLength + 1) +' shown') ;
})

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply