| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Wijmo

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

Wijmo

| Angular |

Angular クイックスタート

インストール

  1. $ npm install @grapecity/wijmo.all

Sample

Angular

オートコンプリートでthisが見えない(コンポーネントでない)

  1. <wj-auto-complete #theAutoComplete id="autoFirstName"
  2. [placeholder]="'FIRST_NAME'"
  3. [displayMemberPath]="'masterValue'"
  4. [itemsSourceFunction]="getFirstNameBySearch">
  5. </wj-auto-complete>
  1. import { Component, Inject, OnInit, ViewChild } from '@angular/core';
  2. import * as wjcCore from '@grapecity/wijmo';
  3. import * as wjcGrid from '@grapecity/wijmo.grid';
  4. import * as input from '@grapecity/wijmo.input';
  5.  
  6. import { Employee } from '../../models/employee';
  7. import { EmployeeService } from '../../services/employee.service';
  8. import { MasterKeyValue } from 'src/app/models/masterKeyValue';
  9. import { MasterService } from 'src/app/services/master.service';
  10.  
  11. @Component({
  12. selector: 'app-employees',
  13. templateUrl: './employees.component.html',
  14. styleUrls: ['./employees.component.css'],
  15. })
  16. export class EmployeesComponent implements OnInit {
  17.  
  18. public employees: Employee[] = [];
  19. selectedItem: string = "";
  20. jobIdList: string[] = [];
  21. jobId: string = "";
  22.  
  23. @ViewChild('flex', { static: true })
  24. flex!: wjcGrid.FlexGrid;
  25.  
  26. constructor(private masterService: MasterService, private employeeService: EmployeeService) {
  27. console.log('constructor');
  28. console.log(`${this.employeeService.getBaseUrl()}`);
  29. }
  30.  
  31. flexInitialized(flexgrid: wjcGrid.FlexGrid) {
  32. // sort the data by country
  33. let sd = new wjcCore.SortDescription('employeeId', true);
  34. flexgrid.collectionView.sortDescriptions.push(sd);
  35. flexgrid.collectionView.currentChanged.addHandler(this._updateCurrentInfo.bind(this));
  36. this._updateCurrentInfo();
  37. }
  38.  
  39. private _updateCurrentInfo() {
  40. console.log(`this.employeeService = ${this.employeeService}`);
  41. // this.selectedItem = wjcCore.format(
  42. // '国: <b>{country}</b>、売上: <b>{sales:c0}</b>、費用: <b>{expenses:c0}</b>',
  43. // this.flex.collectionView.currentItem);
  44. }
  45.  
  46. // JOB_ID データソースロード
  47. async loadJobIdList() {
  48. try {
  49. let jobIdList: MasterKeyValue[]
  50. = await this.masterService.getJobIdList().toPromise();
  51. let buf: string[] = [];
  52. jobIdList.forEach((mst, idx) => buf.push(`${mst.masterValue}`));
  53. this.jobIdList = buf;
  54. } catch (error) {
  55. console.log(error);
  56. }
  57. }
  58.  
  59. getFirstNameBySearch(query: string, max: number, callback: Function) {
  60. console.log(this); // this -> AutoCompleteコントロール
  61. if (!query) {
  62. callback(null);
  63. return;
  64. }
  65.  
  66. // TODO Service で処理をしたい
  67. // 本関数は、AutoComplete のプロパティにセットされるため、
  68. // ComponentにDIされてたサービスの参照方法が現在不明
  69. // 最悪この書き方だとしても、同様にDIされるbaseURL を解決する必要がある。。。
  70. //
  71. // Function.prototype.bind() を利用して、変数を束縛する
  72. // https://typescript-jp.gitbook.io/deep-dive/main-1/bind
  73. // アロー関数のチェーン
  74. // https://typescript-jp.gitbook.io/deep-dive/main-1/currying
  75. wjcCore.httpRequest(`https://localhost:44472/Autocomplete/EmployeeFirstname?id=${encodeURIComponent(query)}`, {
  76. success: (xhr: XMLHttpRequest) => {
  77. let response = JSON.parse(xhr.response);
  78. callback(response);
  79. }
  80. });
  81. }
  82. // Service 使用に書き換え From
  83. async getFirstNameBySearchBindComponent(thisComponent: EmployeesComponent, query: string, max: number, callback: Function) {
  84. console.log(thisComponent.employeeService);
  85. if (!query) {
  86. callback(null);
  87. return;
  88. }
  89. try {
  90. let result = await this.masterService.firstNameAutoComplete(query).toPromise();
  91. console.log(result);
  92. callback(result);
  93. } catch(error) {
  94. console.error(error);
  95. }
  96. }
  97. getFirstNameBySearch2: Function = (query: string, max: number, callback: Function) =>
  98. this.getFirstNameBySearchBindComponent(this, query, max, callback);
  99. // Service 使用に書き換え To
  100.  
  101. async searchSync() {
  102. this.employees.splice(0);
  103. this.employees = [];
  104. // Observable は宣言型で、購読するまで処理が開始されない。Promise は作成時に直ちに実行
  105. // Observable は多くの値を提供します。Promise は1つです
  106. try {
  107. this.employees = await this.employeeService.getEmployees().toPromise();
  108. } catch(error) {
  109. console.error(error);
  110. }
  111. }
  112.  
  113. searchASync() {
  114. this.employees.splice(0);
  115. this.employees = [];
  116.  
  117. this.employeeService.getEmployees().subscribe(
  118. result => {
  119. this.employees = result;
  120. },
  121. error => {
  122. console.error(error);
  123. });
  124. }
  125.  
  126. ngOnInit(): void {
  127. this.loadJobIdList();
  128. }
  129.  
  130. }