Search filter without displaying table when loading page

0

Hello. I have a question regarding filters. I would like a filter similar to that of the code below, but without displaying the table with all the values when loading the page for the first time.

Only after the search did the already filtered values appear. What would it be like?

   <div id="notebooks" ng-app="notebooks" ng- 
   controller="NotebookListCtrl">
   <input type="text" id="query" ng-model="query"/>
   <select ng-model="orderList">
   <option value="name">By name</option>
   <option value="-age">Newest</option>
   <option value="age">Oldest</option>
   </select>
   <ul id="notebook_ul">
   <li ng-repeat="notebook in notebooks | filter:query | orderBy: orderList">
  name: {{notebook.name}}<br/>
  procesor: {{notebook.procesor}}<br/>
  <div class="right top">{{notebook.age}}</div>
  </li>
  </ul>
    <span>Number of notebooks: {{notebooks.length}}</span>
    </div>

Complete code here: link

    
asked by anonymous 24.10.2018 / 16:58

1 answer

0

I can think of two ways. The first and easiest is to add a search button. The user fills in the search field by clicking on the button that performs a search function, filters and fills $scope.notebooks

Then just remove | filter:query

It looks like this:

<div id="notebooks" ng-app="notebooks" ng- controller="NotebookListCtrl">
  <input type="text" id="query" ng-model="query" />
  <button ng-click="search()">Buscar</button>
  <select ng-model="orderList">
    <option value="name">By name</option>
    <option value="-age">Newest</option>
    <option value="age">Oldest</option>
  </select>
  <ul id="notebook_ul">
    <li ng-repeat="notebook in notebooks | orderBy: orderList">
      name: {{notebook.name}}<br />
      procesor: {{notebook.procesor}}<br />
      <div class="right top">{{notebook.age}}</div>
    </li>
  </ul>
  <span>Number of notebooks: {{notebooks.length}}</span>
</div>

<script>
  $scope.notebooks = []

  $scope.search = function () {
    /* 
    Voce pode buscar tudo no servidor e usar o filter com $scope.query para filtrar o resultado ou fazer a busca com o query
     */
    $scope.notebooks = result // coloca o resultado no notebooks
  }
</script>

The other way is to use ngKeyup and browse as the user types. You create a function that does the search using $scope.query and adds ng-keyup="onSearch()" to the search input

It looks like this:

<div id="notebooks" ng-app="notebooks" ng- controller="NotebookListCtrl">
  <input type="text" id="query" ng-model="query" ng-keyup="onSearch()" />
  <select ng-model="orderList">
    <option value="name">By name</option>
    <option value="-age">Newest</option>
    <option value="age">Oldest</option>
  </select>
  <ul id="notebook_ul">
    <li ng-repeat="notebook in notebooks | orderBy: orderList">
      name: {{notebook.name}}<br />
      procesor: {{notebook.procesor}}<br />
      <div class="right top">{{notebook.age}}</div>
    </li>
  </ul>
  <span>Number of notebooks: {{notebooks.length}}</span>
</div>

<script>
  $scope.notebooks = []
  $scope.onSearch = function () {
    // Procura as informaçoes baseadas em $scope.query
    /* Voce pode buscar tudo no servidor e usar o filter com $scope.query para filtrar o resultado ou fazer a busca com o query
     */
    $scope.notebooks = result // coloca o resultado no notebooks
  }
</script>
    
26.10.2018 / 20:41