Skip to content

Instantly share code, notes, and snippets.

@geraldhumphries
Last active January 15, 2016 05:26
Show Gist options
  • Save geraldhumphries/569b189c1e1b62fb77d8 to your computer and use it in GitHub Desktop.
Save geraldhumphries/569b189c1e1b62fb77d8 to your computer and use it in GitHub Desktop.
JHipster search pagination
<form name="searchForm" class="form-inline">
<div class="form-group">
<input type="text" class="form-control" ng-model="searchQuery" id="searchQuery" placeholder="query">
</div>
<button class="btn btn-info" ng-click="search(searchQuery)"><span class="glyphicon glyphicon-search"></span> <span>Search an Entity</span>
</button>
<button class="btn btn-info" ng-click="clear()" ng-show="currentSearch != null"><span class="glyphicon glyphicon-trash"></span> <span>Clear search</span>
</button>
</form>
...
<thead>
<tr jh-sort="predicate" ascending="reverse" callback="loadAll()">
<th jh-sort-by="id"><span translate="global.field.id">ID</span> <span class="glyphicon glyphicon-sort"></span></th>
<th jh-sort-by="name"><span translate="app.entity.name">Name</span> <span class="glyphicon glyphicon-sort"></span></th>
</tr>
</thead>
'use strict';
angular.module('jedApp')
.controller('EntityController', function ($scope, $state, Entity, EntitySearch, ParseLinks) {
$scope.entities = [];
$scope.predicate = 'id';
$scope.reverse = true;
$scope.page = 1;
$scope.loadAll = function () {
if ($scope.currentSearch) {
EntitySearch.query({
query: $scope.currentSearch,
page: $scope.page - 1,
size: 20,
sort: [$scope.predicate + ',' + ($scope.reverse ? 'asc' : 'desc'), 'id']
}, function (result, headers) {
$scope.links = ParseLinks.parse(headers('link'));
$scope.totalItems = headers('X-Total-Count');
$scope.entities = result;
});
} else {
Entity.query({
page: $scope.page - 1,
size: 20,
sort: [$scope.predicate + ',' + ($scope.reverse ? 'asc' : 'desc'), 'id']
}, function (result, headers) {
$scope.links = ParseLinks.parse(headers('link'));
$scope.totalItems = headers('X-Total-Count');
$scope.entities = result;
});
}
};
$scope.search = function (searchQuery) {
$scope.links = null;
$scope.page = 1;
$scope.predicate = 'id';
$scope.reverse = true;
$scope.currentSearch = searchQuery;
$scope.loadAll();
};
$scope.clear = function () {
$scope.links = null;
$scope.page = 0;
$scope.predicate = 'id';
$scope.reverse = true;
$scope.currentSearch = null;
$scope.loadAll();
};
$scope.loadAll();
});
/**
* SEARCH /_search/entities/:query -> search for the entity corresponding
* to the query.
*/
@RequestMapping(value = "/_search/entities/{query}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<List<Entity>> searchEntities(@PathVariable String query, Pageable pageable) throws URISyntaxException {
Page<Entity> page = truckSearchRepository.search(queryStringQuery(query), pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/_search/entities/" + query);
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment