Merge pull request #953 from sonalkr132/datatable-cleanup
Datatable cleanup
|
Before Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 1 KiB |
|
Before Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 1 KiB |
|
Before Width: | Height: | Size: 127 B |
|
Before Width: | Height: | Size: 2.5 KiB |
|
Before Width: | Height: | Size: 110 B |
|
Before Width: | Height: | Size: 96 B |
|
Before Width: | Height: | Size: 153 B |
|
Before Width: | Height: | Size: 105 B |
|
Before Width: | Height: | Size: 124 B |
|
Before Width: | Height: | Size: 130 B |
|
Before Width: | Height: | Size: 119 B |
|
Before Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 4.3 KiB |
|
|
@ -179,167 +179,6 @@ $( document ).ready(function() {
|
||||||
} );
|
} );
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Set the defaults for DataTables initialisation */
|
|
||||||
$.extend( true, $.fn.dataTable.defaults, {
|
|
||||||
"sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
|
|
||||||
"sPaginationType": "bootstrap",
|
|
||||||
"oLanguage": {
|
|
||||||
"sLengthMenu": "_MENU_ items per page"
|
|
||||||
}
|
|
||||||
} );
|
|
||||||
|
|
||||||
/* Default class modification */
|
|
||||||
$.extend( $.fn.dataTableExt.oStdClasses, {
|
|
||||||
"sWrapper": "dataTables_wrapper form-inline"
|
|
||||||
} );
|
|
||||||
|
|
||||||
/* API method to get paging information */
|
|
||||||
$.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
|
|
||||||
{
|
|
||||||
return {
|
|
||||||
"iStart": oSettings._iDisplayStart,
|
|
||||||
"iEnd": oSettings.fnDisplayEnd(),
|
|
||||||
"iLength": oSettings._iDisplayLength,
|
|
||||||
"iTotal": oSettings.fnRecordsTotal(),
|
|
||||||
"iFilteredTotal": oSettings.fnRecordsDisplay(),
|
|
||||||
"iPage": oSettings._iDisplayLength === -1 ?
|
|
||||||
0 : Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
|
|
||||||
"iTotalPages": oSettings._iDisplayLength === -1 ?
|
|
||||||
0 : Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )
|
|
||||||
{
|
|
||||||
if ( typeof sNewSource != 'undefined' && sNewSource != null ) {
|
|
||||||
oSettings.sAjaxSource = sNewSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server-side processing should just call fnDraw
|
|
||||||
if ( oSettings.oFeatures.bServerSide ) {
|
|
||||||
this.fnDraw();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.oApi._fnProcessingDisplay( oSettings, true );
|
|
||||||
var that = this;
|
|
||||||
var iStart = oSettings._iDisplayStart;
|
|
||||||
var aData = [];
|
|
||||||
|
|
||||||
this.oApi._fnServerParams( oSettings, aData );
|
|
||||||
|
|
||||||
oSettings.fnServerData.call( oSettings.oInstance, oSettings.sAjaxSource, aData, function(json) {
|
|
||||||
/* Clear the old information from the table */
|
|
||||||
that.oApi._fnClearTable( oSettings );
|
|
||||||
|
|
||||||
/* Got the data - add it to the table */
|
|
||||||
var aData = (oSettings.sAjaxDataProp !== "") ?
|
|
||||||
that.oApi._fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ) : json;
|
|
||||||
|
|
||||||
for ( var i=0 ; i<aData.length ; i++ )
|
|
||||||
{
|
|
||||||
that.oApi._fnAddData( oSettings, aData[i] );
|
|
||||||
}
|
|
||||||
|
|
||||||
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
|
|
||||||
|
|
||||||
if ( typeof bStandingRedraw != 'undefined' && bStandingRedraw === true )
|
|
||||||
{
|
|
||||||
oSettings._iDisplayStart = iStart;
|
|
||||||
that.fnDraw( false );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
that.fnDraw();
|
|
||||||
}
|
|
||||||
|
|
||||||
that.oApi._fnProcessingDisplay( oSettings, false );
|
|
||||||
|
|
||||||
/* Callback user function - for event handlers etc */
|
|
||||||
if ( typeof fnCallback == 'function' && fnCallback != null )
|
|
||||||
{
|
|
||||||
fnCallback( oSettings );
|
|
||||||
}
|
|
||||||
}, oSettings );
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Bootstrap style pagination control */
|
|
||||||
$.extend( $.fn.dataTableExt.oPagination, {
|
|
||||||
"bootstrap": {
|
|
||||||
"fnInit": function( oSettings, nPaging, fnDraw ) {
|
|
||||||
var oLang = oSettings.oLanguage.oPaginate;
|
|
||||||
var fnClickHandler = function ( e ) {
|
|
||||||
e.preventDefault();
|
|
||||||
if ( oSettings.oApi._fnPageChange(oSettings, e.data.action) ) {
|
|
||||||
fnDraw( oSettings );
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
$(nPaging).addClass('pagination').append(
|
|
||||||
'<ul>'+
|
|
||||||
'<li class="prev disabled"><a href="#">← '+oLang.sPrevious+'</a></li>'+
|
|
||||||
'<li class="next disabled"><a href="#">'+oLang.sNext+' → </a></li>'+
|
|
||||||
'</ul>'
|
|
||||||
);
|
|
||||||
var els = $('a', nPaging);
|
|
||||||
$(els[0]).bind( 'click.DT', { action: "previous" }, fnClickHandler );
|
|
||||||
$(els[1]).bind( 'click.DT', { action: "next" }, fnClickHandler );
|
|
||||||
},
|
|
||||||
|
|
||||||
"fnUpdate": function ( oSettings, fnDraw ) {
|
|
||||||
var iListLength = 5;
|
|
||||||
var oPaging = oSettings.oInstance.fnPagingInfo();
|
|
||||||
var an = oSettings.aanFeatures.p;
|
|
||||||
var i, ien, j, sClass, iStart, iEnd, iHalf=Math.floor(iListLength/2);
|
|
||||||
|
|
||||||
if ( oPaging.iTotalPages < iListLength) {
|
|
||||||
iStart = 1;
|
|
||||||
iEnd = oPaging.iTotalPages;
|
|
||||||
}
|
|
||||||
else if ( oPaging.iPage <= iHalf ) {
|
|
||||||
iStart = 1;
|
|
||||||
iEnd = iListLength;
|
|
||||||
} else if ( oPaging.iPage >= (oPaging.iTotalPages-iHalf) ) {
|
|
||||||
iStart = oPaging.iTotalPages - iListLength + 1;
|
|
||||||
iEnd = oPaging.iTotalPages;
|
|
||||||
} else {
|
|
||||||
iStart = oPaging.iPage - iHalf + 1;
|
|
||||||
iEnd = iStart + iListLength - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for ( i=0, ien=an.length ; i<ien ; i++ ) {
|
|
||||||
// Remove the middle elements
|
|
||||||
$('li:gt(0)', an[i]).filter(':not(:last)').remove();
|
|
||||||
|
|
||||||
// Add the new list items and their event handlers
|
|
||||||
for ( j=iStart ; j<=iEnd ; j++ ) {
|
|
||||||
sClass = (j==oPaging.iPage+1) ? 'class="active"' : '';
|
|
||||||
$('<li '+sClass+'><a href="#">'+j+'</a></li>')
|
|
||||||
.insertBefore( $('li:last', an[i])[0] )
|
|
||||||
.bind('click', function (e) {
|
|
||||||
e.preventDefault();
|
|
||||||
oSettings._iDisplayStart = (parseInt($('a', this).text(),10)-1) * oPaging.iLength;
|
|
||||||
fnDraw( oSettings );
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add / remove disabled classes from the static elements
|
|
||||||
if ( oPaging.iPage === 0 ) {
|
|
||||||
$('li:first', an[i]).addClass('disabled');
|
|
||||||
} else {
|
|
||||||
$('li:first', an[i]).removeClass('disabled');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( oPaging.iPage === oPaging.iTotalPages-1 || oPaging.iTotalPages === 0 ) {
|
|
||||||
$('li:last', an[i]).addClass('disabled');
|
|
||||||
} else {
|
|
||||||
$('li:last', an[i]).removeClass('disabled');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} );
|
|
||||||
|
|
||||||
/* Commodity function for modal windows */
|
/* Commodity function for modal windows */
|
||||||
|
|
||||||
window.build_dialog = function(selector, content) {
|
window.build_dialog = function(selector, content) {
|
||||||
|
|
|
||||||
|
|
@ -1,98 +0,0 @@
|
||||||
# derived from http://railscasts.com/episodes/340-datatables
|
|
||||||
# handles server side multi-column searching and sorting
|
|
||||||
|
|
||||||
class Datatable
|
|
||||||
delegate :params, :h, :raw, :link_to, :number_to_currency, to: :@view
|
|
||||||
|
|
||||||
def initialize(klass, view)
|
|
||||||
@klass = klass
|
|
||||||
@view = view
|
|
||||||
end
|
|
||||||
|
|
||||||
def as_json
|
|
||||||
{
|
|
||||||
sEcho: params[:sEcho].to_i,
|
|
||||||
iTotalRecords: @klass.count,
|
|
||||||
iTotalDisplayRecords: items.total_entries,
|
|
||||||
aaData: data
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def data
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
|
|
||||||
def columns
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def items
|
|
||||||
@items ||= fetch_items
|
|
||||||
end
|
|
||||||
|
|
||||||
def fetch_items
|
|
||||||
items = filtered_list
|
|
||||||
items = selected_columns(items)
|
|
||||||
items = items.order(sort_order)
|
|
||||||
items = items.page(page).per_page(per_page)
|
|
||||||
if params[:sSearch].present?
|
|
||||||
items = items.where(quick_search)
|
|
||||||
end
|
|
||||||
items
|
|
||||||
end
|
|
||||||
|
|
||||||
def filtered_list
|
|
||||||
@klass.scoped
|
|
||||||
end
|
|
||||||
|
|
||||||
def selected_columns items
|
|
||||||
items
|
|
||||||
end
|
|
||||||
|
|
||||||
def quick_search
|
|
||||||
search_for = params[:sSearch].split(' ')
|
|
||||||
terms = {}
|
|
||||||
which_one = -1
|
|
||||||
criteria = search_for.inject([]) do |mem, atom|
|
|
||||||
which_one += 1
|
|
||||||
terms["search#{which_one}".to_sym] = "%#{atom}%"
|
|
||||||
mem << "(#{search_cols.map{|col| "#{col} like :search#{which_one}"}.join(' or ')})"
|
|
||||||
end.join(' and ')
|
|
||||||
[criteria, terms]
|
|
||||||
end
|
|
||||||
|
|
||||||
def page
|
|
||||||
params[:iDisplayStart].to_i/per_page + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
def per_page
|
|
||||||
params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
|
|
||||||
end
|
|
||||||
|
|
||||||
def sort_order
|
|
||||||
colnum = 0
|
|
||||||
sort_by = []
|
|
||||||
while true
|
|
||||||
break if !sorted?(colnum)
|
|
||||||
sort_by << "#{sort_column(colnum)} #{sort_direction(colnum)}"
|
|
||||||
colnum += 1
|
|
||||||
end
|
|
||||||
sort_by.join(', ')
|
|
||||||
end
|
|
||||||
|
|
||||||
def sorted? index=0
|
|
||||||
!params["iSortCol_#{index}"].nil?
|
|
||||||
end
|
|
||||||
|
|
||||||
def sort_column index=0
|
|
||||||
index = "iSortCol_#{index}"
|
|
||||||
columns[params[index].to_i]
|
|
||||||
end
|
|
||||||
|
|
||||||
def sort_direction index=0
|
|
||||||
index = "sSortDir_#{index}"
|
|
||||||
params[index] == 'desc' ? 'desc' : 'asc'
|
|
||||||
end
|
|
||||||
end
|
|
||||||