diff --git a/horizon/static/horizon/js/horizon.tables.js b/horizon/static/horizon/js/horizon.tables.js index 82c45ff3b..61991fbba 100644 --- a/horizon/static/horizon/js/horizon.tables.js +++ b/horizon/static/horizon/js/horizon.tables.js @@ -218,6 +218,20 @@ horizon.datatables.update_footer_count = function (el, modifier) { $footer.text(footer_text); }; +horizon.datatables.add_no_results_row = function (table) { + // Add a "no results" row if there are no results. + template = horizon.templates.compiled_templates["#empty_row_template"]; + if (!table.find("tbody tr:visible").length && typeof(template) !== "undefined") { + colspan = table.find("th[colspan]").attr('colspan'); + params = {"colspan": colspan}; + table.find("tbody").append(template.render(params)); + } +}; + +horizon.datatables.remove_no_results_row = function (table) { + table.find("tr.empty").remove(); +}; + horizon.datatables.set_table_sorting = function (parent) { // Function to initialize the tablesorter plugin strictly on sortable columns. $(parent).find("table.datatable").each(function () { @@ -252,7 +266,7 @@ horizon.datatables.add_table_checkboxes = function(parent) { }); }; -horizon.datatables.set_table_filter = function (parent) { +horizon.datatables.set_table_query_filter = function (parent) { $(parent).find('table').each(function (index, elm) { var input = $($(elm).find('div.table_search input')), table_selector; @@ -280,23 +294,14 @@ horizon.datatables.set_table_filter = function (parent) { 'show': this.show, 'hide': this.hide, onBefore: function () { - // Clear the "no results" row. var table = $(table_selector); - table.find("tr.empty").remove(); + horizon.datatables.remove_no_results_row(table); }, onAfter: function () { var template, table, colspan, params; table = $(table_selector); horizon.datatables.update_footer_count(table); - // Add a "no results" row if there are no results. - template = horizon.templates.compiled_templates["#empty_row_template"]; - if (!$(table_selector + " tbody tr:visible").length && typeof(template) !== "undefined") { - colspan = table.find("th[colspan]").attr('colspan'); - params = {"colspan": colspan}; - table.find("tbody").append(template.render(params)); - } - // Update footer count - + horizon.datatables.add_no_results_row(table); }, prepareQuery: function (val) { return new RegExp(val, "i"); @@ -309,6 +314,29 @@ horizon.datatables.set_table_filter = function (parent) { }); }; +horizon.datatables.set_table_fixed_filter = function (parent) { + $(parent).find('table.datatable').each(function (index, elm) { + $(elm).on('click', 'div.table_filter button', function(evt) { + var table = $(elm); + var category = $(this).val(); + evt.preventDefault(); + horizon.datatables.remove_no_results_row(table); + table.find('tbody tr').hide(); + table.find('tbody tr.category-' + category).show(); + horizon.datatables.update_footer_count(table); + horizon.datatables.add_no_results_row(table); + }); + $(elm).find('div.table_filter button').each(function (i, button) { + // Select the first non-empty category + if ($(button).text().indexOf(' (0)') == -1) { + $(button).addClass('active'); + $(button).trigger('click'); + return false; + } + }); + }); +}; + horizon.addInitFunction(function() { horizon.datatables.validate_button(); horizon.datatables.update_footer_count($.find('table.datatable'),0); @@ -341,12 +369,14 @@ horizon.addInitFunction(function() { // Trigger run-once setup scripts for tables. horizon.datatables.add_table_checkboxes($('body')); horizon.datatables.set_table_sorting($('body')); - horizon.datatables.set_table_filter($('body')); + horizon.datatables.set_table_query_filter($('body')); + horizon.datatables.set_table_fixed_filter($('body')); // Also apply on tables in modal views. horizon.modals.addModalInitFunction(horizon.datatables.add_table_checkboxes); horizon.modals.addModalInitFunction(horizon.datatables.set_table_sorting); - horizon.modals.addModalInitFunction(horizon.datatables.set_table_filter); + horizon.modals.addModalInitFunction(horizon.datatables.set_table_query_filter); + horizon.modals.addModalInitFunction(horizon.datatables.set_table_fixed_filter); horizon.datatables.update(); }); diff --git a/horizon/static/horizon/tests/tables.js b/horizon/static/horizon/tests/tables.js index 410c0aaeb..cbccdd905 100644 --- a/horizon/static/horizon/tests/tables.js +++ b/horizon/static/horizon/tests/tables.js @@ -1,29 +1,49 @@ module("Tables (horizon.tables.js)"); +test("Row filtering (fixed)", function () { + var fixture = $("#qunit-fixture"); + var table = fixture.find("#table2"); + + ok(!table.find(".cat").is(":hidden"), "Filtering cats: cats visible by default"); + ok(table.find(":not(.cat)").is(":hidden"), "Filtering cats: non-cats hidden by default"); + + $("#button_cats").trigger("click"); + ok(!table.find(".cat").is(":hidden"), "Filtering cats: cats visible"); + ok(table.find(":not(.cat)").is(":hidden"), "Filtering cats: non-cats hidden"); + + $("#button_dogs").trigger("click"); + ok(!table.find(".dog").is(":hidden"), "Filtering dogs: dogs visible"); + ok(table.find(":not(.dog)").is(":hidden"), "Filtering dogs: non-dogs hidden"); + + $("#button_big").trigger("click"); + ok(!table.find(".big").is(":hidden"), "Filtering big animals: big visible"); + ok(table.find(":not(.big)").is(":hidden"), "Filtering big animals: non-big hidden"); +}); + test("Footer count update", function () { var fixture = $("#qunit-fixture"); - var table = fixture.find("table.datatable"); + var table = fixture.find("#table1"); var tbody = table.find('tbody'); var table_count = table.find("span.table_count"); + var rows = tbody.find('tr'); horizon.datatables.update_footer_count(table); notEqual(table_count.text().indexOf('4 items'), -1, "Initial count is correct"); // hide rows - $("table.datatable tbody tr#dog1").hide(); - $("table.datatable tbody tr#cat2").hide(); + rows.first().hide(); + rows.first().next().hide(); horizon.datatables.update_footer_count(table); notEqual(table_count.text().indexOf('2 items'), -1, "Count correct after hiding two rows"); // show a row - $("table.datatable tbody tr#cat2").show(); + rows.first().next().show(); horizon.datatables.update_footer_count(table); notEqual(table_count.text().indexOf('3 items'), -1, "Count correct after showing one row"); // add rows - $("table.datatable tbody tr#cat2").show(); - $('
cat1 |
dog1 |
cat2 |
dog2 |
cat1 |
dog1 |
cat2 |
dog2 |
+
+
+
+ |
---|
cat1 |
dog1 |
cat2 |
dog2 |
+ Displaying 4 items + | +