title: How to populate select filters on ng-table from async call
body: <h2>tl:dr</h2>
  
  <p>How can I populate an ng-table including 'select' filters using ajax/json?</p>
  
  <p><a href="http://plnkr.co/Zn09LV">Plunk showing the problem: http://plnkr.co/Zn09LV</a></p>
  
  <hr>
  
  <h2>Detail</h2>
  
  <p>I am trying to get to grips with AngualrJS and the ng-table extension and although I can get some nice tables with working filters and such when I'm using static data defined in the javascript - once I get to trying to load real data into the table I hit a snag.</p>
  
  <p>The main body of the ng-table is populated correctly and as long as I only use the text filter everthing seems to be working:</p>
  
  <pre><code>        &lt;td data-title="'Name'" filter="{ 'Name': 'text' }" sortable="'Name'"&gt;
              {{user.Name}}
          &lt;/td&gt;
  </code></pre>
  
  <p>Works just fine.</p>
  
  <p>However, if I update this to use the select filter:</p>
  
  <pre><code>        &lt;td data-title="'Name'" filter="{ 'Name': 'select' }" sortable="'Name'"  filter-data="Names($column)"&gt;
              {{user.Name}}
          &lt;/td&gt;
  </code></pre>
  
  <p>I run into a syncronisation issue in that the Names variable is always evaluated before the data has returned from the server. (Possibly the Names varibale is evaluated before the request to the server is even sent.) This means I get an empty list for the filter.</p>
  
  <p>Once the data returns from the server - I can't seem to find a way of updating the select filter. Re-running the code that creates the filter list initially seems to have no effect - I'm not sure how to trigger the ng-table to re-check its filters so the updated variable isn't read.
  I also can't figure out a way to postpone the evaluation of the variable until after the async call is complete.</p>
  
  <p>For my javascript I have pretty much used the example ajax code from the ng-table GitHub page and added onto it the example code for the select filter.</p>
  
  <pre><code>    $scope.tableParams = new ngTableParams({
          page: 1,            // show first page
          count: 10,          // count per page
          sorting: {
              name: 'asc'     // initial sorting
          }
      }, {
          total: 0,           // length of data
          getData: function($defer, params) {
              // ajax request to api
              Api.get(params.url(), function(data) {
                  $timeout(function() {
                      // update table params
                      var orderedData = params.sorting ?
                      $filter('orderBy')(data.result, params.orderBy()) :
                      data.result;
                      orderedData = params.filter ?
                      $filter('filter')(orderedData, params.filter()) :
                      orderedData;
  
                      $scope.users = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());
  
                      params.total(orderedData.length); // set total for recalc pagination
                      $defer.resolve($scope.users);
                  }, 500);
              });
          }
      });
  
      var inArray = Array.prototype.indexOf ?
      function (val, arr) {
          return arr.indexOf(val)
      } :
      function (val, arr) {
          var i = arr.length;
          while (i--) {
              if (arr[i] === val) return i;
          }
          return -1
      };
  $scope.names = function(column) {
      var def = $q.defer(),
          arr = [],
          names = [];
      angular.forEach(data, function(item){
          if (inArray(item.name, arr) === -1) {
              arr.push(item.name);
              names.push({
                  'id': item.name,
                  'title': item.name
              });
          }
      });
      def.resolve(names);
      return def;
  };
  </code></pre>
  
  <p>I've tried a few attempts at adding on an additional $q.defer() and wrapping the initial data get followed by the $scope.names function - but my understanding of promise and defer isn't strong enough to get anything working.</p>
  
  <p>There are a few notes on GitHub suggesting this is a bug in ng-table, but I'm not sure if that's the case or I'm just doing something daft.</p>
  
  <p><a href="https://github.com/esvit/ng-table/issues/186">https://github.com/esvit/ng-table/issues/186</a></p>
  
  <p>Pointers on how to procede greatly appreciated </p>
  
  <p>-Kaine-</p>