angularjs – 如何在JHipster中添加自定义查询?
我正在学习使用JHipster,无法弄清楚如何使用创建自定义查询.
在我的项目中,我有带有DeliveryDay和Week字段的Orders表,并且只想显示当周当天的订单. DeliveryDay和Week是int的值(1-7和0-2) 所以在OrdersRepository.java中我添加了这样的自定义查询: public interface OrdersRepository extends JpaRepository<Orders,Long> { Page<Orders> findByDeliveryDayAndWeek(int weekday,int week,pageable); 在OrdersResource.java中我添加了这个: @RequestMapping(value = "/today",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE) @Timed public ResponseEntity<List<Orders>> getOrdersForToday(Pageable pageable) throws URISyntaxException { log.debug("REST request to get a page of Orderss"); Page<Orders> page = ordersRepository.findByDeliveryDayAndWeek(1,pageable); HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page,"/api/today"); return new ResponseEntity<>(page.getContent(),headers,HttpStatus.OK); } 我还添加了today.html(复制的orders.html)和today.js 'use strict'; angular.module('fruitcrmApp') .config(function ($stateProvider) { $stateProvider .state('today',{ parent: 'entity',url: '/today',data: { authorities: ['ROLE_USER'],pageTitle: 'fruitcrmApp.orders.home.title' },views: { 'content@': { templateUrl: 'scripts/app/custom/today.html',controller: 'OrdersController' } },resolve: { translatePartialLoader: ['$translate','$translatePartialLoader',function ($translate,$translatePartialLoader) { $translatePartialLoader.addPart('orders'); $translatePartialLoader.addPart('global'); return $translate.refresh(); }] } }) }); 并在index.html中添加today.js 我的orders.controller.js看起来像这样(由JHipster生成) 'use strict'; angular.module('fruitcrmApp') .controller('OrdersController',function ($scope,$state,Orders,OrdersSearch,ParseLinks) { $scope.orderss = []; $scope.predicate = 'id'; $scope.reverse = true; $scope.page = 1; $scope.loadAll = function() { Orders.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.orderss = result; }); }; $scope.loadPage = function(page) { $scope.page = page; $scope.loadAll(); }; $scope.loadAll(); $scope.search = function () { OrdersSearch.query({query: $scope.searchQuery},function(result) { $scope.orderss = result; },function(response) { if(response.status === 404) { $scope.loadAll(); } }); }; $scope.refresh = function () { $scope.loadAll(); $scope.clear(); }; $scope.clear = function () { $scope.orders = { details: null,orderDate: null,firstDelivery: null,isActive: false,id: null }; }; }); 现在我可以访问http:// localhost:3000 /#/ today但它显示了Orders的所有数据我做错了什么?如何使用OrdersRepository.java中我自己的方法? 我试图搜索示例,但没有找到任何相关的.我错过了哪些必要的步骤?如果答案很长,那么链接到一些教程将会很好. 解决方法
您需要为今天的API端点创建一个新的角度服务.像这样的东西叫做orders-today.service.js:
'use strict'; angular.module('fruitcrmApp') .factory('OrdersToday',function ($resource) { return $resource('api/orders/today',{},{ 'query': { method: 'GET',isArray: true} }); }); 然后在orders.controller.js文件中,您需要注入新的OrdersToday服务: .controller('OrdersController',OrdersToday,ParseLinks) { 当您想获得今天的订单列表时,您需要使用OrdersToday.query,就像您在粘贴的示例中使用Orders.query一样. 您可能希望创建一个带有OrdersToday引用的OrdersTodayController,并在today.js而不是OrdersController中使用它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |