Skip to content

Instantly share code, notes, and snippets.

@joelibaceta
Forked from EtienneDepaulis/active_admin.css.scss
Last active August 29, 2015 14:27
Show Gist options
  • Save joelibaceta/3fac293143c7be3ac3a3 to your computer and use it in GitHub Desktop.
Save joelibaceta/3fac293143c7be3ac3a3 to your computer and use it in GitHub Desktop.
jQuery Datatables + Active Admin
#= require active_admin/base
#= require chosen-jquery
#= require dataTables/jquery.dataTables
$.fn.dataTableExt.oApi.fnPagingInfo = (oSettings) ->
{
'iStart': oSettings._iDisplayStart
'iEnd': oSettings.fnDisplayEnd()
'iLength': oSettings._iDisplayLength
'iTotal': oSettings.fnRecordsTotal()
'iFilteredTotal': oSettings.fnRecordsDisplay()
'iPage': Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength)
'iTotalPages': Math.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)
}
$.extend $.fn.dataTableExt.oPagination, 'active_admin':
'fnInit': (oSettings, nPaging, fnDraw) ->
oLang = oSettings.oLanguage.oPaginate
fnClickHandler = (e) ->
e.preventDefault()
if oSettings.oApi._fnPageChange(oSettings, e.data.action)
fnDraw oSettings
return
$(nPaging).addClass('pagination').append '<span class="prev disabled"><a href="#">' + oLang.sPrevious + '</a></span>' + '<span class="next disabled"><a href="#">' + oLang.sNext + '</a></span>'
els = $('a', nPaging)
$(els[0]).bind 'click.DT', { action: 'previous' }, fnClickHandler
$(els[1]).bind 'click.DT', { action: 'next' }, fnClickHandler
return
'fnUpdate': (oSettings, fnDraw) ->
iListLength = 5
oPaging = oSettings.oInstance.fnPagingInfo()
an = oSettings.aanFeatures.p
i = undefined
j = undefined
sClass = undefined
iStart = undefined
iEnd = undefined
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
i = 0
iLen = an.length
while i < iLen
# Remove the middle elements
$('span:gt(0)', an[i]).filter(':not(:last)').remove()
# Add the new list items and their event handlers
j = iStart
while j <= iEnd
#sClass = (j==oPaging.iPage+1) ? 'class="current page"' : 'class="page"';
sElement = if j == oPaging.iPage + 1 then $('<span class="current page">' + j + '</span>') else $('<span class="page"><a href="#">' + j + '</a></span>')
sElement.insertBefore($('span:last', an[i])[0]).bind 'click', (e) ->
e.preventDefault()
oSettings._iDisplayStart = (parseInt($('a', this).text(), 10) - 1) * oPaging.iLength
fnDraw oSettings
return
j++
# Add / remove disabled classes from the static elements
if oPaging.iPage == 0
$('span:first', an[i]).addClass 'disabled'
else
$('span:first', an[i]).removeClass 'disabled'
if oPaging.iPage == oPaging.iTotalPages - 1 or oPaging.iTotalPages == 0
$('span:last', an[i]).addClass 'disabled'
else
$('span:last', an[i]).removeClass 'disabled'
i++
return
$(document).ready ->
$('.filter_select select').chosen()
$('.data_table').dataTable
'sDom': 't<p>'
'sPaginationType': 'active_admin'
return
# ---
# generated by js2coffee 2.1.0
@import "dataTables/jquery.dataTables";
/* A modifier pour avoir les bons espacements */
body.active_admin .data_table {
padding-bottom: 10px;
}
body.active_admin .dataTables_paginate {
margin-top: -10px;
}
gem 'jquery-datatables-rails'
# ...
table_for(project.all_media, {:class => "data_table"}) do
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment