-->

Smart table - angularJs - ajax refresh table

2019-04-12 16:34发布

问题:

I am using the angularjs mudule smart table.

My table has server-side processing and all the data load is located on the server. I want to have a refresh button outside of the table.

This is the function that makes a call to the server and I want to be able to call it manually, but I can't figure out how to retrieve the table state in the my controller.

this.callServer = function callServer(tableState) {

        ctrl.isLoading = true;

        var pagination = tableState.pagination;

        var start = pagination.start || 0;     // This is NOT the page number, but the index of item in the list that you want to use to display the table.
        var number = pagination.number || 10;  // Number of entries showed per page.

        service.getPage(start, number, tableState, ctrl.startDateFilter,
                ctrl.endDateFilter).then(function (result) {
            ctrl.displayed = result.data;
            tableState.pagination.numberOfPages = result.numberOfPages;
            ctrl.isLoading = false;
        });
    };

My goal is to have a function like this, How can I get the table state?

    this.refreshTable = function(){
        tableState = getTableState();
        ctrl.callServer(tableState);
    }

回答1:

The solution is to put the table state in a controller variable.

Everytime the callServer function is called, it will update this variable. This way, I am able to refresh the table.

    this.tableState = null;

    this.callServer = function callServer(tableState) {
        ctrl.tableState = tableState;
        ...
    }

    this.refreshGrid = function(){
        ctrl.callServer(ctrl.tableState);
    }


回答2:

You can also create a directive to do it.

View

<table st-pipe="productsTableCtrl.getTableData" 
st-table="productsTableCtrl.tableData" refresh-table>

Inside your Controller

$scope.$broadcast('refreshMyTable');

Directive

app.directive("refreshTable", function(){
    return {
        require:'stTable',
        restrict: "A",
        link:function(scope,elem,attr,table){
            scope.$on("refreshMyTable", function() {
                table.pipe(table.tableState());
            });
    }
}});

Credits to gtaylor44: https://github.com/lorenzofox3/Smart-Table/issues/363#issuecomment-246636293