Vorrei utilizzare il plugin jQuery jTable in Ruby on Rails 4. Qual è il modo migliore per farlo? La compatibilità con Turbolink è importante per me.Come posso usare jTable in Ruby on Rails?
risposta
ho installato js JTable e CSS nella cartella vendor (jQuery UI è necessaria, ho usato jquery-ui-rails gemma)
ho creato un controllore (chiamato tables_controller), e ho usato un modo ingannevole po 'per rendere più facile usare e rotaie amichevoli.
Vedere l'esempio seguente. Ho usato una convenzione di denominazione speciale per semplificare il processo: il nome dei campi contiene il nome della tabella e il nome della colonna separati da "__", ad esempio visitors__id
. Vedrai come aiuta a gestire l'ordinamento automatico.
app/controllers/tables_controller.erb:
class TablesController < ApplicationController
def demo
respond_to do |format|
format.json {
page_size = params[:jtPageSize].to_i
start_index = params[:jtStartIndex].to_i
start_page = start_index/page_size + 1
sort_by = params[:jtSorting].gsub('__', '.')
filter = params[:filter]
if filter
@visitors_count = Visitor.joins(:city).
where('visitors.name like ? OR cities.name like ?', "%#{filter}%", "%#{filter}%").size()
@visitors = Visitor.joins(:city).
where('visitors.name like ? OR cities.name like ?', "%#{filter}%", "%#{filter}%").order(sort_by).
paginate(:page => start_page, :per_page => page_size)
else
@visitors_count = Visitor.all.size()
@visitors = Visitor.joins(:city).order(sort_by).
paginate(:page => start_page, :per_page => page_size)
end
}
format.html {}
format.js {}
end
end
def demo_delete
@visitor = Visitor.find(params[:visitors__id])
if @visitor
if @visitor.destroy
render js: '{"Result":"OK"}'
end
end
end
end
app/views/tavoli/demo.json.jbuilder:
json.Result "OK"
json.TotalRecordCount @visitors_count
json.Records do
json.array!(@visitors) do |visitor|
json.visitors__id visitor.id
json.visitors__name visitor.name
json.visitors__age visitor.age
json.cities__name visitor.city.name
end
end
app/views/tavoli/demo.html.erb:
<div class="jtable-filter">
<form class="jtable-filter">
<input type="text" name="filter" id="filter"/>
<button type="submit" data-jtable-filter="demo">Filter</button>
</form>
</div>
<div id="demo" data-jtable-config="/demo">
</div>
app/views/tavoli/demo.js.erb:
var jtable_config = {
title: '',
paging: true, //Enable paging
pageSize: 10, //Set page size (default: 10)
sorting: true, //Enable sorting
defaultSorting: 'visitors__name ASC', //Set default sorting
defaultDateFormat: 'yy-mm-dd',
gotoPageArea: 'combobox',
actions: {
listAction: '/demo',
deleteAction: '/demo/delete'
},
fields: {
visitors__id: {
key: true,
list: false
},
visitors__name: {
title: 'Name',
width: '30%'
},
visitors__age: {
title: 'Age',
width: '20%'
},
cities__name: {
title: 'City',
width: '20%'
}
}
};
application.js:
(function ($) {
jQuery.fn.jtable_ou = function() {
return this.each(function() {
var $this = $(this);
var config_url=$this.attr("data-jtable-config");
$.getScript(config_url, function(data, textStatus, jqxhr) {
$this.jtable(jtable_config);
$this.jtable('load');
});
});
};
})(jQuery);
$(document).on("page:change", function() {
$('[data-jtable-config]').jtable_ou();
$('button[data-jtable-filter]').click(function (e) {
e.preventDefault();
var target=this.getAttribute("data-jtable-filter");
$('#'+target).jtable('load', {
filter: $('#filter').val()
});
});
});
Gemfile:
match '/demo', to: 'tables#demo', via: [:get, :post]
match '/demo/delete', to: 'tables#demo_delete', via: [:post]
È TurboLink amichevole e utilizza JavaScript discreto.
È possibile trovare la descrizione dettagliata in this post.
- 1. Perché dovrei usare Ruby on Rails?
- 2. Curl on Ruby on Rails
- 3. Docsplit Ruby on Rails
- 4. Quando usare un lambda in Ruby on Rails?
- 5. Come posso dichiarare una variabile globale in Ruby on Rails?
- 6. Come posso registrare un operatore in Ruby on Rails?
- 7. Come posso localizzare will_paginate in ruby on rails?
- 8. Come posso avere un'architettura di plugin in Ruby on Rails?
- 9. Come posso connettermi a MySQL in Ruby on Rails?
- 10. Come posso profilare una richiesta in Ruby on Rails?
- 11. Devo usare has_one o belongs_to in ruby on rails?
- 12. Ruby on Rails colonna decimale
- 13. Formati personalizzati in Ruby on Rails
- 14. Problemi compresi httparty in Ruby on Rails
- 15. Associazione polimorfica Ruby on Rails
- 16. Ruby on Rails javascript_include_tag: default
- 17. Ruby on Rails, rilevamento Javascript
- 18. Busting Ruby on Rails Myths
- 19. Come posso rimuovere Ruby on Rails 4 beta?
- 20. Ruby on rails tasks scheduling
- 21. Ruby on Rails: come posso modificare database.yml per postgresql?
- 22. Ruby on Rails Rake Error
- 23. Ruby on Rails Interpolazione I18n
- 24. Come posso disinstallare Ruby on Rails su Mac OS X?
- 25. Come disinstallare un'applicazione Ruby on Rails?
- 26. HTTPClient :: ReceiveTimeoutError in Ruby on Rails
- 27. sottraendo due date in Ruby on Rails
- 28. Come manipolare DOM con Ruby on Rails
- 29. Campo Ruby on Rails medio?
- 30. Ruby on Rails 301 reindirizzamento