How do I save the state of an object table and reapply

How do I save the state of an object table and reapply it when the user gets back to a view.

I have a view with an object-table and when the user links to another view and gets back to my view with the object-table I want the state to be the same as when they left.

We've used this technique to maintain any search, filters, paging, etc. that was in place before navigating to another view.

In the view xml file:

    <object-table id="1" query="table_query" init-state="$:initState()" on-state-change="$:captureState($state)" controls="$:getControls()">

In the view js file:

var saved_state;

function resume(from) {
    refreshList();  
    if (saved_state) {
        component.objectTable({ id: "1" }).setState(saved_state);
    }
}

function refreshList() {
  view.table_query = .... however you build object_table;
}

function initState() {
    if (saved_state) {
        return saved_state;
    }
    return {};
}

function captureState(state) {
    saved_state = state;
}

function sortByActionDate() {
    view.order_by = '-action_date';
    refreshList();

    // Reset sorting portion of state on the object table
    component.objectTable({ id: "1" }).setState({ sorting: [] });
}

function resetObjectTable() {
    var newState = {
        filters: {},
        sorting: [],
        page: '1',
        search: null
    };
    refreshList();
    component.objectTable({ id: "1" }).setState(newState);
}
2 Likes