//= require jquery //= require jquery_ujs $(function() { $("table[data-activegrid=true]").each(function(index, table) { var self = $(table); var name = self.attr("data-activegrid-name"); var page = 1; var pages = self.attr("data-activegrid-pages"); var paginate = self.attr("data-activegrid-paginate"); var order = null; var filterField = null, filterValue = null; var baseURL = function() { var url = "?" url += encodeURI("activegrid[name]") + "=" + encodeURI(name); url += "&" + encodeURI("activegrid[limit]") + "=" + encodeURI(paginate); url += "&" + encodeURI("activegrid[offset]") + "=" + encodeURI((page - 1) * paginate); return url; }; var updateTable = function() { var url = "?" url += encodeURI("activegrid[name]") + "=" + encodeURI(name); url += "&" + encodeURI("activegrid[limit]") + "=" + encodeURI(paginate); url += "&" + encodeURI("activegrid[offset]") + "=" + encodeURI((page - 1) * paginate); if (order) { url += "&" + encodeURI("activegrid[order]") + "=" + encodeURI(order); } if (filterField) { url += "&" + encodeURI("activegrid[filter][field]") + "=" + encodeURI(filterField); url += "&" + encodeURI("activegrid[filter][value]") + "=" + encodeURI(filterValue); } $.ajax({ url: url, dataType: "script" }); }; // Sorting var resetSortDirection = function() { var set = $("th[data-activegrid-sort=true]", table); set.data("activegrid-sort-direction", null).removeClass("activegrid-sort-asc").removeClass("activegrid-sort-desc"); }; $("th[data-activegrid-sort=true]", table).click(function() { var self = $(this); var field = self.attr("data-activegrid-sort-field"); var direction = self.data("activegrid-sort-direction"); var newDirection = direction == "asc" ? "desc" : "asc"; resetSortDirection(); self.data("activegrid-sort-direction", newDirection).addClass("activegrid-sort-" + newDirection); order = field + (newDirection == "asc" ? "" : " DESC"); updateTable(); }); // Filtering $(":input[data-activegrid-filter=true]", table).change(function() { var self = $(this); var field = self.attr("data-activegrid-filter-field"); filterField = field; filterValue = self.val(); updateTable(); }); // TODO: pagination (back and forward buttons, list of pages) }); }); function activegrid_update(element, body) { $("#" + element + " tbody").html(body); };