2 * Copyright 2016-2017, CMCC Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 var serverPageTable = {};
17 /* Bootstrap style full number pagination control */
18 $.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
21 "iStart": oSettings._iDisplayStart,
22 "iEnd": oSettings.fnDisplayEnd(),
23 "iLength": oSettings._iDisplayLength,
24 "iTotal": oSettings.fnRecordsTotal(),
25 "iFilteredTotal": oSettings.fnRecordsDisplay(),
26 "iPage": Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
27 "iTotalPages": Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
31 $.extend( $.fn.dataTableExt.oPagination, {
32 "bootstrap_extended": {
33 "fnInit": function( oSettings, nPaging, fnDraw ) {
34 var oLang = oSettings.oLanguage.oPaginate;
35 var oPaging = oSettings.oInstance.fnPagingInfo();
37 var fnClickHandler = function ( e ) {
39 if ( oSettings.oApi._fnPageChange(oSettings, e.data.action) ) {
45 '<div class="pagination-panel"> ' + oLang.sPage + ' ' +
46 '<a href="#" class="btn btn-sm default prev disabled" title="' + oLang.sPrevious + '"><i class="fa fa-angle-left"></i></a>' +
47 '<input type="text" class="pagination-panel-input input-mini input-inline input-sm" maxlenght="5" style="text-align:center; margin: 0 4px; border: 1px solid rgb(169, 169, 169);height: 28px;">' +
48 '<a href="#" class="btn btn-sm default next disabled" title="' + oLang.sNext + '"><i class="fa fa-angle-right"></i></a> ' +
49 oLang.sPageOf + ' <span class="pagination-panel-total"></span>' +
53 var els = $('a', nPaging);
55 $(els[0]).bind('click.DT', { action: "previous" }, fnClickHandler );
56 $(els[1]).bind('click.DT', { action: "next" }, fnClickHandler);
58 $('.pagination-panel-input', nPaging).bind('change.DT', function(e) {
59 var oPaging = oSettings.oInstance.fnPagingInfo();
61 var page = parseInt($(this).val());
62 if (page > 0 && page < oPaging.iTotalPages) {
63 if ( oSettings.oApi._fnPageChange(oSettings, page-1) ) {
67 $(this).val(oPaging.iPage + 1);
71 $('.pagination-panel-input', nPaging).bind('keypress.DT', function(e) {
72 var oPaging = oSettings.oInstance.fnPagingInfo();
74 var page = parseInt($(this).val());
75 if (page > 0 && page < oSettings.oInstance.fnPagingInfo().iTotalPages) {
76 if ( oSettings.oApi._fnPageChange(oSettings, page-1) ) {
80 $(this).val(oPaging.iPage + 1);
87 "fnUpdate": function ( oSettings, fnDraw ) {
89 var oPaging = oSettings.oInstance.fnPagingInfo();
90 var an = oSettings.aanFeatures.p;
91 var i, j, sClass, iStart, iEnd, iHalf=Math.floor(iListLength/2);
93 if ( oPaging.iTotalPages < iListLength) {
95 iEnd = oPaging.iTotalPages;
97 else if ( oPaging.iPage <= iHalf ) {
100 } else if ( oPaging.iPage >= (oPaging.iTotalPages-iHalf) ) {
101 iStart = oPaging.iTotalPages - iListLength + 1;
102 iEnd = oPaging.iTotalPages;
104 iStart = oPaging.iPage - iHalf + 1;
105 iEnd = iStart + iListLength - 1;
109 for ( i=0, iLen=an.length ; i<iLen ; i++ ) {
110 var wrapper = $(an[i]).parents(".dataTables_wrapper");
112 if (oPaging.iTotalPages <= 0) {
113 $('.pagination-panel, .dataTables_length', wrapper).hide();
115 $('.pagination-panel, .dataTables_length', wrapper).show();
118 $('.pagination-panel-total', an[i]).html(oPaging.iTotalPages);
119 $('.pagination-panel-input', an[i]).val(oPaging.iPage + 1);
121 // Remove the middle elements
122 $('li:gt(1)', an[i]).filter(':not(.next)').remove();
124 // Add the new list items and their event handlers
125 for ( j=iStart ; j<=iEnd ; j++ ) {
126 sClass = (j==oPaging.iPage+1) ? 'class="active"' : '';
127 $('<li '+sClass+'><a href="#">'+j+'</a></li>')
128 .insertBefore( $('li.next:first', an[i])[0] )
129 .bind('click', function (e) {
131 oSettings._iDisplayStart = (parseInt($('a', this).text(),10)-1) * oPaging.iLength;
136 // Add / remove disabled classes from the static elements
137 if ( oPaging.iPage === 0 ) {
138 $('a.prev', an[i]).addClass('disabled');
140 $('a.prev', an[i]).removeClass('disabled');
143 if ( oPaging.iPage === oPaging.iTotalPages-1 || oPaging.iTotalPages === 0 ) {
144 $('a.next', an[i]).addClass('disabled');
146 $('a.next', an[i]).removeClass('disabled');
152 serverPageTable.initDataTable = function( setting , divId ,pageInfos) {
153 serverPageTable.pageInfo = pageInfos;
156 var column = setting.columns;
158 $('#'+ divId).children().remove();
159 var tableId = setting.tableId;
160 var tableEleStr = '<table class="table table-striped table-bordered table-hover" id= '+ tableId + '>'
162 +'<tr role="row" class="heading" >'
168 $('#'+ divId).append(tableEleStr);
169 //$('#'+ tableId).append(' <thead><tr role="row" class="heading" ></tr></thead><tbody></tbody>');
170 var trEle = $('#'+ tableId + ' > thead >tr');
171 //var dataTableColumn = [];
172 for ( var one in column){
173 var th = '<th>' + column[one].name + '</th>';
176 serverPageTable.pageInfo = pageInfos;
177 if(!setting.pageHtml){
178 pageHtml="<'row'<'col-md-12 col-sm-12'lip>r><'table-scrollable't>>";
180 pageHtml=setting.pageHtml;
182 var table = $("#" + tableId).dataTable({
183 //"sDom" : "tr<'row'<'col-md-6 col-sm-12'><'col-md-6 col-sm-12'pli>>", // datatable layout
185 "oLanguage": setting.language,//汉化
187 "bPaginate": true,// 分页按钮
188 "bFilter": false,// 搜索栏
189 "bAutoWidth":true,//自动设置列宽
190 "bLengthChange": true,// 每行显示记录数
191 "iDisplayLength": 10,// 每页显示行数
193 "bInfo": true,// Showing 1 to 10 of 23 entries 总记录数没也显示多少等信息
195 "bScrollCollapse": true,
196 "sPaginationType": "bootstrap_extended", // 分页,一共两种样式 另一种为two_button // 是datatables默认
200 "bSortCellsTop": true,
201 "sAjaxSource": setting.restUrl,
202 "aoColumns": setting.columns,
203 "fnServerData": function (sSource, aoData, fnCallback, oSettings) {
204 fnServerData(sSource, aoData, fnCallback, oSettings);
207 $('#'+ tableId + '>tbody').on('click', 'td.details-control', function () {
208 var tr = $(this).closest('tr');
209 //var nTr = $(this).parents('tr')[0];
210 //var row = table.row( tr );
211 if ( table.fnIsOpen(tr[0]) ){
212 table.fnClose( tr[0] );
213 //if ( row.child.isShown() ) {
214 // This row is already open - close it
216 tr.removeClass('shown');
220 table.fnOpen( tr[0], format_Detail(table, tr[0],column) );
221 //row.child( format_Detail(row.data()) ).show();
222 tr.addClass('shown');