diff --git a/bitbake/lib/toaster/toastergui/static/js/table.js b/bitbake/lib/toaster/toastergui/static/js/table.js index 7b55102546..176ce579fb 100644 --- a/bitbake/lib/toaster/toastergui/static/js/table.js +++ b/bitbake/lib/toaster/toastergui/static/js/table.js @@ -15,6 +15,7 @@ function tableInit(ctx){ orderby : null, filter : null, search : null, + default_orderby: null, }; var defaultHiddenCols = []; @@ -192,6 +193,8 @@ function tableInit(ctx){ tableHeadRow.html(""); editColMenu.html(""); + tableParams.default_orderby = tableData.default_orderby; + if (!tableParams.orderby && tableData.default_orderby){ tableParams.orderby = tableData.default_orderby; } @@ -217,6 +220,7 @@ function tableInit(ctx){ var title = $(''); title.data('field-name', col.field_name); + title.attr('data-sort-field', col.field_name); title.text(col.title); title.click(sortColumnClicked); @@ -344,29 +348,65 @@ function tableInit(ctx){ } } + /* Apply an ordering to the current table. + * + * 1. Find the column heading matching the sortSpecifier + * 2. Set its up/down arrow and add .sorted + * + * orderby: e.g. "-started_on", "completed_on" + * colHeading: column heading element to activate (by showing the caret + * up/down, depending on sort order); if not set, the correct column + * heading is selected from the DOM using orderby as a key + */ + function applyOrderby(orderby, colHeading) { + if (!orderby) { + return; + } + + // We only have one sort at a time so remove existing sort indicators + $("#" + ctx.tableName + " th .icon-caret-down").hide(); + $("#" + ctx.tableName + " th .icon-caret-up").hide(); + $("#" + ctx.tableName + " th a").removeClass("sorted"); + + // normalise the orderby so we can use it to find the link we want + // to style + var fieldName = orderby; + if (fieldName.indexOf('-') === 0) { + fieldName = fieldName.slice(1); + } + + // find the table header element which corresponds to the sort field + // (if we don't already have it) + if (!colHeading) { + colHeading = $('[data-sort-field="' + fieldName + '"]'); + } + + colHeading.addClass("sorted"); + + var parent = colHeading.parent(); + + if (orderby.indexOf('-') === 0) { + parent.children('.icon-caret-up').show(); + } + else { + parent.children('.icon-caret-down').show(); + } + + tableParams.orderby = orderby; + loadData(tableParams); + } + function sortColumnClicked(e){ e.preventDefault(); - /* We only have one sort at a time so remove any existing sort indicators */ - $("#"+ctx.tableName+" th .icon-caret-down").hide(); - $("#"+ctx.tableName+" th .icon-caret-up").hide(); - $("#"+ctx.tableName+" th a").removeClass("sorted"); - - var fieldName = $(this).data('field-name'); - /* if we're already sorted sort the other way */ - if (tableParams.orderby === fieldName && + var orderby = $(this).data('field-name'); + if (tableParams.orderby === orderby && tableParams.orderby.indexOf('-') === -1) { - tableParams.orderby = '-' + $(this).data('field-name'); - $(this).parent().children('.icon-caret-up').show(); - } else { - tableParams.orderby = $(this).data('field-name'); - $(this).parent().children('.icon-caret-down').show(); + orderby = '-' + orderby; } - $(this).addClass("sorted"); - - loadData(tableParams); + applyOrderby(orderby, $(this)); } function pageButtonClicked(e) { @@ -385,11 +425,13 @@ function tableInit(ctx){ table.find("."+col).show(); } else { table.find("."+col).hide(); - /* If we're ordered by the column we're hiding remove the order by */ + // If we're ordered by the column we're hiding remove the order by + // and apply the default one instead if (col === tableParams.orderby || '-' + col === tableParams.orderby){ tableParams.orderby = null; - $("#"+ctx.tableName +" .default-orderby").click(); + + applyOrderby(tableParams.default_orderby); } }