0

I create a filter on a table. Here the code:

<table id="tableText" class="table table-hover table-striped" ng-init="allNews()">
  <tr>
    <td colspan="5">
      <input type="text" placeholder="Ricerca testo" class="form-control" ng-model="inputText">
    </td>
  </tr>

  <tr>
    <th>Titolo</th>
    <th>Text</th>
    <th>Disattivato</th>
    <th>Modifica</th>
    <th ng-if="!cancelDelete">Elimina</th>
    <th ng-if="cancelDelete">Annulla</th>
  </tr>

  <tr ng-repeat="news in allNews | filter : inputText">
    <td>
      <div ng-hide="editingData[news.id]"><span ng-bind-html="news | deleteTitle"></span></div>
      <div ng-show="editingData[news.id]"><input type="text" class="form-control" ng-model="news.title" /></div>
    </td>

    <td>
      <div ng-hide="editingData[news.id]"><span ng-bind-html="news | deleteText"></span></div>
      <div ng-show="editingData[news.id]"><input type="text" class="form-control" ng-model="news.arg" /></div>
    </td>

    <td>
      <div ng-hide="editingData[news.id]"><input type="checkbox" disabled ng-model="news.isDeleted"></div>
      <div ng-show="editingData[news.id]"><input type="checkbox" ng-model="news.isDeleted"></div>
    </td>

    <td>
      <div ng-hide="editingData[news.id]"><button id="modify" class="btn btn-primary" ng-click="modify(news, $event)">Modifica</button></div>
      <div ng-show="editingData[news.id]"><button id="accept" class="btn btn-success" ng-click="update(news)">Accetta</button></div>
    </td>

    <td>
      <div ng-hide="editingData[news.id]"><button id="delete" class="btn btn-danger" ng-click="delete(news.id)">Cancella</button></div>
      <div ng-show="editingData[news.id]"><button id="cancel" class="btn btn-danger" ng-click="cancelModify()">Annulla</button></div>
    </td>
  </tr>
</table>

The entry on the table is read from db:

$scope.allNews = function () {
  var url = '/data_db.asmx/GetAllNews';
  var obj = {};
  $http.post(url, obj)
  .success(
    function (response) {
      if (response.Error) {
        console.log('Server error');
      }
      else {
        $scope.allNews = response.d;
      }
    })
  .error(
    function (response) {
      console.log('Unkwnow error.');
    });
}

I'd like to highlight the text that is search in the 1st row of the table. For now, i receive this error:

angular.js:13920 Error: [filter:notarray] Expected array but received: function ()

but the filter works.

4
  • can you add your filter codes? Commented Oct 5, 2016 at 7:50
  • Have no filter code! This is the "magic" D: I use ng-model="inputText" in the input search, and then filter on the ng-repeat with filter : inputText Commented Oct 5, 2016 at 7:55
  • You don't need a filter but directive or component. Filter uses for filtering not for DOM manipulation. Commented Oct 5, 2016 at 7:55
  • Also, you are using allNews for the array and the method. Change the name of the method. (news in allNews and $scope.allNews = function () { Commented Oct 5, 2016 at 7:59

1 Answer 1

1

Your problem is that $scope.allNews is a function. When you use it in ng-repeat directive and the directive is evaluated for the first time, your angular will try to examine the allNews property of your $scope as an array.

When the function gets called the first time (which might never happen when angular first encouters the error), it woul overwrite the allNews property with the resulting array of your $http POST request.

Rename either the function or the property and bind your ng-repeat to the array it recieves (and maybe initialize it with an empty array until it is populated by the $http result).

Something like:

$scope.allNews = [];

$scope.getAllNews = function() {
var url = '/data_db.asmx/GetAllNews';
var obj = {};
  $http.post(url, obj)
  .success(
    function (response) {
      if (response.Error) {
        console.log('Server error');
      }
      else {
        $scope.allNews = response.d;
      }
    })
  .error(
    function (response) {
      console.log('Unkwnow error.');
    });
}

Alternatively try using ngResource, create a service and inject that into your controller. Then populate the array by accessing the service.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.