3

I am trying to understand how sorting works in Table widgets works when loading a page. For most of my pages using the Table widget, the page loads sorted by the first column.

I do see the below code in the onAttach event for the Table panel in a page. I am wondering if this is the code that sets the sorting when a page loads.

// GENERATED CODE: modify at your own risk
window._am = window._am || {};
if (!window._am.TableState) {
  window._am.TableState = {};
  window._am.inFlight = false;
}
if (!window._am.sortTableBy) {
  window._am.sortTableBy = function(datasource, field, fieldHeader, tableState) {
    if (!field) {
      throw "Can't sort the table because specified field was not found.";
    }
    tableState.inFlight = true;
    if (tableState.sortByField === field.name) {
      tableState.ascending = !tableState.ascending;
    } else {
      if (tableState.fieldHeader) {
        tableState.fieldHeader.text = tableState.fieldHeaderText;
        tableState.fieldHeader.ariaLabel = "";
      }
      tableState.sortByField = field.name;
      tableState.ascending = true;
      tableState.fieldHeader = fieldHeader;
      tableState.fieldHeaderText = fieldHeader.text;
    }
    datasource.query.clearSorting();
    var sortDirection = tableState.ascending ? "ascending" : "descending";
    datasource.query.sorting[field.name]["_" + sortDirection]();
    datasource.query.pageIndex = 1;
    datasource.load(function() {
      tableState.inFlight = false;
      fieldHeader.text = fieldHeader.text.replace(/ (\u25B2|\u25BC)/g, "");
      if (tableState.sortByField === field.name) {
        fieldHeader.ariaLabel = fieldHeader.text + " sort " + sortDirection;
        fieldHeader.text = fieldHeader.text + (tableState.ascending ? " \u25B2" : " \u25BC");
        app.accessibility.announce(fieldHeader.ariaLabel);
      }
    });
  };
}

2 Answers2

3

Sorting is set on your datasource.

Click the model (top left) that your table is using.

Click Datasources and expand your Datasource (there will only be one, unless you have created additional ones).

Once you choose the field you want the table to use for sorting, you can choose "Ascending" or "Descending".

enter image description here

Adam Bergeron
  • 535
  • 3
  • 11
0

The problem is with adding new records from a separate create form. The new records always appear at the end of the list , until a subsequent 'load' is performed.