Server side search

Michael Pope

Server Setup

Client Setup

Angular Rails Best Practices

AngularJS Filters

Only good for small data sets

AngularJS ng-table

  • Has AJAX Data Loading & Filtering
  • Requires jQuery
  • Looks promising
  • Ransack

  • Rails gem used for searching on the server
  • Search accross multiple tables
  • cont, lt, gt, eq, matches, in, start, end, true, present, null
  • Search form


    <form>
      <tr>
        <td><input ng-model="subject_cont" /></td>
        <td><input ng-model="body_cont" /></td>
        <td>
        <input type="reset" value="reset" ng-click="reset()" />
        <input type="submit" name="submit" value="submit" ng-click="search()" />
        </td>
      </tr>
    </form>
    

    AngualrJS search method


    $scope.search = ->
      searchService.setSubject($scope.subject_cont)
      searchService.setBody($scope.body_cont)
      
      # Submit the search
      $http.get('/api/letters?' +
        'q[subject_cont]=' + $scope.subject_cont +
        '&q[body_cont]=' + $scope.body_cont)
      .then ((response) ->
        $scope.letters=response.data
      )
      

    Rails controller


    class Api::LettersController < Api::BaseController
      def index
        @q=Letter.search(params[:q])
        @letters=@q.result()
        respond_with :api, @letters
      end
    end
    
    

    Remember values


    # Retain search variables whilst moving through the application
    app.service('searchService', ->
      subject = ''
      body = ''
      getSubject : -> return subject
      setSubject : (text) -> subject = text
      
      getBody : -> return body
      setBody : (text) -> body = text
    )
    

    Load values


    if $state.current.name == 'letters'
      $scope.subject_cont = searchService.getSubject()
      $scope.body_cont = searchService.getBody()
      $scope.search()
      

    References

    Questions