i new in datatable , insert datatable serve-side processing , other features.
i followed this tutorial in order insert datatable in app , worked.
here app/assets/datatables/addresses_datatable.rb file:
class addressesdatatable delegate :params, :h, :link_to, :number_to_currency, to: :@view def initialize(view) @view = view end def as_json(options = {}) { secho: params[:secho].to_i, itotalrecords: addresses.count, itotaldisplayrecords: addresses.total_entries, aadata: data } end private def data addresses.map |address| [ h(address.user_id), h(address.first_name), ] end end def addresses @addresses ||= fetch_addresses end def fetch_addresses addresses = address.order("#{sort_column}").page(page).per_page(per_page) if params[:ssearch].present? addresses = addresses.where("user_id first_name :search", search: "%#{params[:ssearch]}%") end addresses 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_column columns = %w[user_id first_name] columns[params[:isortcol_0].to_i] end end
here app/views/addresses/index.html.erb file:
<h1>user's addresses</h1> <table id="addresses" class ="display" data-source= "<% user_addresses_path(format:"json")%>"> <thead> <tr> <th>user id</th> <th>first anme</th> </tr> </thead> <tbody> </tbody> </table>
here app/asset/javascript/addresses.js file :
$(document).ready(function() { var otable = $('#addresses').datatable({ spaginationtype: "full_numbers", bsortclasses: false, bprocessing: true, bserverside: true, sajaxsource: $('#addresses').data('source') }); } );
and here addresses_controller.rb:
class addressescontroller < applicationcontroller def index if signed_in? respond_to |format| format.html format.json { render json: addressesdatatable.new(view_context) } end end end end
when try add additional features datatable in this example :, code doesn't work :
$(document).ready(function() { $('#addresses tbody tr').each( function() { var stitle; var ntds = $('td', this); var sbrowser = $(ntds[0]).text(); <a href="/ref#stitle">stitle</a> = sbrowser+' user s id'; this.setattribute( 'title', <a href="/ref#stitle">stitle</a> ); } ); var otable = $('#addresses').datatable({ spaginationtype: "full_numbers", bsortclasses: false, bprocessing: true, bserverside: true, sajaxsource: $('#addresses').data('source') }); otable.$('tr').tooltip( { "delay": 0, "track": true, "fade": 250 } ); } );
i've tried many different examples , each time thing managed display simple datatable. because of server side behaviour of datatable?
thank help.
clément
Comments
Post a Comment