Zizzamia / ng-tasty

A tasty collection of reusable UI components for Angular, like grandma used to make. Like Table directive, WebSocket / Debounce / Throttle service, Range / Camelize / Slugify filters
MIT License
434 stars 64 forks source link

running a function before sorting, paging... #175

Open sali1982 opened 8 years ago

sali1982 commented 8 years ago

I have a question for you.

I am having a serverside table. I want to give a warning to user, before the action of sorting happens, that he has some unsaved changes. If he says proceed, I will do the sort, if not, I won't do it.

Is there any function I can bind to the table to triggers before each action, like sorting, paging so if it is confirmed by user continues the action?

Zizzamia commented 8 years ago

On the Table Server Side, you usually assign a callback to call anytime something change like sorting, pagination or filters.

This function as we now from the documentation should look like this one.

$scope.getResource = function (params, paramsObj) {
  var urlApi = 'table.json?' + params;
  return $http.get(urlApi).then(function (response) {
    return {
      'rows': response.data.rows,
      'header': response.data.header,
      'pagination': response.data.pagination,
      'sortBy': response.data['sort-by'],
      'sortOrder': response.data['sort-order']
    }
  });
}

You want control the exact moment $http.get it's trigged.

$scope.getResource = function (params, paramsObj) {

  return $scope.showModalToCheckIfProceed().then(function() {
    var urlApi = 'table.json?' + params;
    return $http.get(urlApi).then(function (response) {
      return {
        'rows': response.data.rows,
        'header': response.data.header,
        'pagination': response.data.pagination,
        'sortBy': response.data['sort-by'],
        'sortOrder': response.data['sort-order']
      }
    });
  }, function () {
    console.log('Nope this time!')
 });
}

Not sure will work, but the idea is to return a Promise with your custom implementation. I'm going to make documentation about it, so see how do it.

Thanks for the question.