Statistics
| Branch: | Tag: | Revision:

root / static / js / dataTableslatest / dataTables.bootstrap.js @ 353f6cde

History | View | Annotate | Download (9.4 kB)

1
/* Set the defaults for DataTables initialisation */
2
$.extend(true, $.fn.dataTable.defaults, {
3
    "sDom": "<'row'<'col-sm-6'l><'col-sm-6'f>r>" + "t" + "<'row'<'col-sm-6'i><'col-sm-6'p>>",
4
    "oLanguage": {
5
        "sLengthMenu": "_MENU_ records per page"
6
    }
7
});
8

    
9

    
10
/* Default class modification */
11
$.extend($.fn.dataTableExt.oStdClasses, {
12
    "sWrapper": "dataTables_wrapper form-inline",
13
    "sFilterInput": "form-control input-sm",
14
    "sLengthSelect": "form-control input-sm"
15
});
16

    
17
// In 1.10 we use the pagination renderers to draw the Bootstrap paging,
18
// rather than  custom plug-in
19
if ($.fn.dataTable.Api) {
20
    $.fn.dataTable.defaults.renderer = 'bootstrap';
21
    $.fn.dataTable.ext.renderer.pageButton.bootstrap = function(settings, host, idx, buttons, page, pages) {
22
        var api = new $.fn.dataTable.Api(settings);
23
        var classes = settings.oClasses;
24
        var lang = settings.oLanguage.oPaginate;
25
        var btnDisplay, btnClass;
26

    
27
        var attach = function(container, buttons) {
28
            var i, ien, node, button;
29
            var clickHandler = function(e) {
30
                e.preventDefault();
31
                if (e.data.action !== 'ellipsis') {
32
                    api.page(e.data.action).draw(false);
33
                }
34
            };
35

    
36
            for (i = 0, ien = buttons.length; i < ien; i++) {
37
                button = buttons[i];
38

    
39
                if ($.isArray(button)) {
40
                    attach(container, button);
41
                } else {
42
                    btnDisplay = '';
43
                    btnClass = '';
44

    
45
                    switch (button) {
46
                        case 'ellipsis':
47
                            btnDisplay = '&hellip;';
48
                            btnClass = 'disabled';
49
                            break;
50

    
51
                        case 'first':
52
                            btnDisplay = lang.sFirst;
53
                            btnClass = button + (page > 0 ?
54
                                '' : ' disabled');
55
                            break;
56

    
57
                        case 'previous':
58
                            btnDisplay = lang.sPrevious;
59
                            btnClass = button + (page > 0 ?
60
                                '' : ' disabled');
61
                            break;
62

    
63
                        case 'next':
64
                            btnDisplay = lang.sNext;
65
                            btnClass = button + (page < pages - 1 ?
66
                                '' : ' disabled');
67
                            break;
68

    
69
                        case 'last':
70
                            btnDisplay = lang.sLast;
71
                            btnClass = button + (page < pages - 1 ?
72
                                '' : ' disabled');
73
                            break;
74

    
75
                        default:
76
                            btnDisplay = button + 1;
77
                            btnClass = page === button ?
78
                                'active' : '';
79
                            break;
80
                    }
81

    
82
                    if (btnDisplay) {
83
                        node = $('<li>', {
84
                            'class': classes.sPageButton + ' ' + btnClass,
85
                            'aria-controls': settings.sTableId,
86
                            'tabindex': settings.iTabIndex,
87
                            'id': idx === 0 && typeof button === 'string' ? settings.sTableId + '_' + button : null
88
                        })
89
                            .append($('<a>', {
90
                                    'href': '#'
91
                                })
92
                                .html(btnDisplay)
93
                        )
94
                            .appendTo(container);
95

    
96
                        settings.oApi._fnBindAction(
97
                            node, {
98
                                action: button
99
                            }, clickHandler
100
                        );
101
                    }
102
                }
103
            }
104
        };
105

    
106
        attach(
107
            $(host).empty().html('<ul class="pagination"/>').children('ul'),
108
            buttons
109
        );
110
    }
111
} else {
112
    // Integration for 1.9-
113
    $.fn.dataTable.defaults.sPaginationType = 'bootstrap';
114

    
115
    /* API method to get paging information */
116
    $.fn.dataTableExt.oApi.fnPagingInfo = function(oSettings) {
117
        return {
118
            "iStart": oSettings._iDisplayStart,
119
            "iEnd": oSettings.fnDisplayEnd(),
120
            "iLength": oSettings._iDisplayLength,
121
            "iTotal": oSettings.fnRecordsTotal(),
122
            "iFilteredTotal": oSettings.fnRecordsDisplay(),
123
            "iPage": oSettings._iDisplayLength === -1 ? 0 : Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength),
124
            "iTotalPages": oSettings._iDisplayLength === -1 ? 0 : Math.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)
125
        };
126
    };
127

    
128
    /* Bootstrap style pagination control */
129
    $.extend($.fn.dataTableExt.oPagination, {
130
        "bootstrap": {
131
            "fnInit": function(oSettings, nPaging, fnDraw) {
132
                var oLang = oSettings.oLanguage.oPaginate;
133
                var fnClickHandler = function(e) {
134
                    e.preventDefault();
135
                    if (oSettings.oApi._fnPageChange(oSettings, e.data.action)) {
136
                        fnDraw(oSettings);
137
                    }
138
                };
139

    
140
                $(nPaging).append(
141
                    '<ul class="pagination">' +
142
                    '<li class="prev disabled"><a href="#">&larr; ' + oLang.sPrevious + '</a></li>' +
143
                    '<li class="next disabled"><a href="#">' + oLang.sNext + ' &rarr; </a></li>' +
144
                    '</ul>'
145
                );
146
                var els = $('a', nPaging);
147
                $(els[0]).bind('click.DT', {
148
                    action: "previous"
149
                }, fnClickHandler);
150
                $(els[1]).bind('click.DT', {
151
                    action: "next"
152
                }, fnClickHandler);
153
            },
154

    
155
            "fnUpdate": function(oSettings, fnDraw) {
156
                var iListLength = 5;
157
                var oPaging = oSettings.oInstance.fnPagingInfo();
158
                var an = oSettings.aanFeatures.p;
159
                var i, ien, j, sClass, iStart, iEnd, iHalf = Math.floor(iListLength / 2);
160

    
161
                if (oPaging.iTotalPages < iListLength) {
162
                    iStart = 1;
163
                    iEnd = oPaging.iTotalPages;
164
                } else if (oPaging.iPage <= iHalf) {
165
                    iStart = 1;
166
                    iEnd = iListLength;
167
                } else if (oPaging.iPage >= (oPaging.iTotalPages - iHalf)) {
168
                    iStart = oPaging.iTotalPages - iListLength + 1;
169
                    iEnd = oPaging.iTotalPages;
170
                } else {
171
                    iStart = oPaging.iPage - iHalf + 1;
172
                    iEnd = iStart + iListLength - 1;
173
                }
174

    
175
                for (i = 0, ien = an.length; i < ien; i++) {
176
                    // Remove the middle elements
177
                    $('li:gt(0)', an[i]).filter(':not(:last)').remove();
178

    
179
                    // Add the new list items and their event handlers
180
                    for (j = iStart; j <= iEnd; j++) {
181
                        sClass = (j == oPaging.iPage + 1) ? 'class="active"' : '';
182
                        $('<li ' + sClass + '><a href="#">' + j + '</a></li>')
183
                            .insertBefore($('li:last', an[i])[0])
184
                            .bind('click', function(e) {
185
                                e.preventDefault();
186
                                oSettings._iDisplayStart = (parseInt($('a', this).text(), 10) - 1) * oPaging.iLength;
187
                                fnDraw(oSettings);
188
                            });
189
                    }
190

    
191
                    // Add / remove disabled classes from the static elements
192
                    if (oPaging.iPage === 0) {
193
                        $('li:first', an[i]).addClass('disabled');
194
                    } else {
195
                        $('li:first', an[i]).removeClass('disabled');
196
                    }
197

    
198
                    if (oPaging.iPage === oPaging.iTotalPages - 1 || oPaging.iTotalPages === 0) {
199
                        $('li:last', an[i]).addClass('disabled');
200
                    } else {
201
                        $('li:last', an[i]).removeClass('disabled');
202
                    }
203
                }
204
            }
205
        }
206
    });
207
}
208

    
209

    
210
/*
211
 * TableTools Bootstrap compatibility
212
 * Required TableTools 2.1+
213
 */
214
if ($.fn.DataTable.TableTools) {
215
    // Set the classes that TableTools uses to something suitable for Bootstrap
216
    $.extend(true, $.fn.DataTable.TableTools.classes, {
217
        "container": "DTTT btn-group",
218
        "buttons": {
219
            "normal": "btn btn-default",
220
            "disabled": "disabled"
221
        },
222
        "collection": {
223
            "container": "DTTT_dropdown dropdown-menu",
224
            "buttons": {
225
                "normal": "",
226
                "disabled": "disabled"
227
            }
228
        },
229
        "print": {
230
            "info": "DTTT_print_info modal"
231
        },
232
        "select": {
233
            "row": "active"
234
        }
235
    });
236

    
237
    // Have the collection use a bootstrap compatible dropdown
238
    $.extend(true, $.fn.DataTable.TableTools.DEFAULTS.oTags, {
239
        "collection": {
240
            "container": "ul",
241
            "button": "li",
242
            "liner": "a"
243
        }
244
    });
245
}