JHipster增加查詢功能

以公司company模塊爲例

1、在company.component.html增加查詢按鈕

<div class="row">
        <div class="col-sm-12">
            <form name="searchForm" class="form-inline">
                <div class="input-group w-100 mt-3">
                    <input type="text" class="form-control" [(ngModel)]="currentSearch" id="currentSearch" name="currentSearch" placeholder="{{ 'faceMachineApp.company.home.search' | translate }}">
                    <button class="input-group-append btn btn-info" (click)="transition()">
                        <fa-icon [icon]="'search'"></fa-icon>
                    </button>
                    <button class="input-group-append btn btn-danger" (click)="clear()" *ngIf="currentSearch">
                        <fa-icon [icon]="'trash-alt'"></fa-icon>
                    </button>
                </div>
            </form>
        </div>
    </div>

2、在company.component.ts增加查詢字段、參數和方法

currentSearch: string;
if(this.currentSearch) {
      this.companyService
        .query({
          page: this.page - 1,
          size: this.itemsPerPage,
          sort: this.sort(),
          currentSearch: this.currentSearch
        })
        .subscribe(
          (res: HttpResponse<ICompany[]>) => this.paginateCompanies(res.body, res.headers),
          (res: HttpErrorResponse) => this.onError(res.message)
        );
    } else {
      this.companyService
        .query({
          page: this.page - 1,
          size: this.itemsPerPage,
          sort: this.sort()
        })
        .subscribe(
          (res: HttpResponse<ICompany[]>) => this.paginateCompanies(res.body, res.headers),
          (res: HttpErrorResponse) => this.onError(res.message)
        );
    }

3、在company.component.ts文件clean()方法中增加以下代碼:

this.currentSearch = '';

4、在zh-cn/company.json增加faceMachineApp.company.home.search國際化資源

"search": "查找 Company",

5、在en/company.json增加faceMachineApp.company.home.search國際化資源

"search": "Search for Company",

6、在manager\src\main\java\com\xadhsd\manager\web\rest\CompanyResource.java增加以下方法:

/**
     * {@code GET  /companies} : 根據查詢條件獲取公司列表
     *
     * @param currentSearch 查詢條件
     * @param pageable      分頁信息
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of companies in body.
     */
    @GetMapping(value = "/companies", params = {"currentSearch"})
    public ResponseEntity<List<Company>> getByCompanyName(
        @RequestParam(value = "currentSearch") String currentSearch,
        @RequestParam MultiValueMap<String, String> queryParams, UriComponentsBuilder uriBuilder,
        Pageable pageable) {
        Page<Company> page = companyQueryService.findByCompanyName(currentSearch, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(uriBuilder.queryParams(queryParams), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

7、在manager\src\main\java\com\xadhsd\manager\service\CompanyQueryService.java增加以下方法:

/**
     * 根據查詢條件獲取公司列表
     *
     * @param companyName 查詢條件
     * @param pageable    分頁信息
     * @return the matching entities.
     */
    public Page<Company> findByCompanyName(String companyName, Pageable pageable) {
        return companyRepository.findAllByCompanyName(companyName, pageable);
    }

8、在manager\src\main\java\com\xadhsd\manager\repository\CompanyRepository.java增加以下方法:

 /**
     * 根據查詢條件獲取公司列表
     *
     * @param companyName
     * @param pageable
     * @return
     */
    Page<Company> findAllByCompanyName(String companyName, Pageable pageable);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章