简体   繁体   中英

Restangular get list by query parameters from form

My backend Controller looks like this

@RequestMapping(value ="/search",method=RequestMethod.POST)
@ResponseBody
public List<Book> searchSubmit(@ModelAttribute Book book, Model model) {

How could I get the typical post --> Response cycle to work with Restangular or Angularjs ? The following works to get all of the books but not target the "/search" mapping with the Book Model parameters and I put it in ng-Grid.

app.controller('searchController', function($scope, $http, $location, Restangular){
$scope.bookData = {};


$scope.processUpdateForm = function(){


  //data from the form works !!
  var bAuthor = $scope.bookData.author;
  console.log(">>>>>bData"+bAuthor);

  Restangular.all('books').getList().then(function(result) {
      $scope.books = result;
      console.log(">>>>>books: "+angular.toJson($scope.books, 2));
      bookData = angular.toJson($scope.books, 2);
      $scope.$emit('bookData', $scope.books);
  });              

  }
});

but how can you actually do something useful such as query by paramaters ?? Is there some example out there of doing this?

The reference Docs only show simple get all or get one...

I see you can do..

Restangular.get('books').get({filters: {
  author: bAuthor,
  genre: bGenre
}});

would send something like..

 ?filters={"author":"thedata","genre":"thedata"}

but that wouldn't work...

Thanks!

Ok so here's the solution...

Using angularjs and restangular lib (also reqs lodash lib)..

var app = angular.module('myApp', [ 'ngGrid', 'restangular' ]);

var bookData;
app.controller('gridCtrl', [ '$scope', function($scope) {
$scope.myData = [];

$scope.gridOptions = {
    data : 'myData',
    enableColumnResize : true
};

$scope.$on('bookData', function(event, bookData) {
    $scope.myData = bookData.plain();
    console.log("$scope.myData = " + $scope.myData);
    $scope.gridOptions = {
        data : 'myData',
        enableColumnResize : true
    };

    });

} ]);

app.controller('searchController', function($scope, $http, $location,
    Restangular) {

$scope.bookData = {};

$scope.processUpdateForm = function() {
    // data from the form 
    var aData = $scope.bookData.author;
    var gData = $scope.bookData.genre;
    var pData = $scope.bookData.pages;
    var yData = $scope.bookData.year;
    var rData = $scope.bookData.rating;
    Restangular.all(
            'books/rest/' + aData + '/' + gData + '/' + pData + '/' + yData
                    + '/' + rData).getList().then(
            function(result) {
                $scope.books = result;
                $scope.$emit('bookData', result);
                // reset the form..
                $scope.bookData = {};
            });

}
});

In the Spring Backend retrieve the Params...

@RequestMapping(value = "/{author}/{genre}/{pages}/{year}/{rating}",method = RequestMethod.GET)
@ResponseBody
public List<Book> searchBooks(@PathVariable String author,
                              @PathVariable String genre,
                              @PathVariable String pages,
                              @PathVariable String year,
                              @PathVariable String rating) {
 Etc....

Hope this helps somebody!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM