Commit 158be81a325e1bd3f0eeca32c663ac39089f9fed
1 parent
46083d4e
Delete action
Showing
3 changed files
with
36 additions
and
1 deletions
Show diff stats
src/app/pages/statements/components/busStop2/busStop2.component.ts
... | ... | @@ -181,7 +181,35 @@ export class BusStop2 { |
181 | 181 | } else { |
182 | 182 | result = this.service.create(data); |
183 | 183 | } |
184 | - console.log(result); | |
184 | + } | |
185 | + } | |
186 | + | |
187 | + private deleteRows() { | |
188 | + let objects = this.gridOptions.api.getSelectedRows(); | |
189 | + if (!objects.length) { | |
190 | + return ; | |
191 | + } | |
192 | + let ids = []; | |
193 | + objects.forEach(element => { | |
194 | + let id = element.busStopId; | |
195 | + if (id) { | |
196 | + ids.push(id); | |
197 | + } | |
198 | + }); | |
199 | + if (ids.length) { | |
200 | + let rowData = this.rowData; | |
201 | + rowData = rowData.filter(element => { | |
202 | + let index = ids.indexOf(element.busStopId); | |
203 | + if (index === -1) { | |
204 | + return true; | |
205 | + } else { | |
206 | + this.service.delete(ids[index]); | |
207 | + return false; | |
208 | + } | |
209 | + }); | |
210 | + this.rowData = rowData; | |
211 | + this.gridOptions.api.setDatasource(this.setRowData(this.rowData)); | |
212 | + this.gridOptions.api.refreshVirtualPageCache(); | |
185 | 213 | } |
186 | 214 | } |
187 | 215 | ... | ... |
src/app/pages/statements/components/busStop2/busStop2.html
... | ... | @@ -3,6 +3,7 @@ |
3 | 3 | <div class="row"> |
4 | 4 | <ba-card title="Автобусні зупинки" baCardClass="with-scroll"> |
5 | 5 | <button class="btn btn-warning" (click)="addNewRow()" type="button">Добавить новое поле</button> |
6 | + <button class="btn btn-danger" (click)="deleteRows()" type="button">Удалить</button> | |
6 | 7 | <ag-grid-ng2 #agGrid style="width: 100%; height: 350px;" class="ag-dark" |
7 | 8 | [gridOptions]="gridOptions" |
8 | 9 | [columnDefs]="columnDefs" | ... | ... |
src/app/pages/statements/components/busStop2/busStop2.service.ts
... | ... | @@ -44,6 +44,12 @@ export class BusStop2Service { |
44 | 44 | .then(response => response.json()) |
45 | 45 | .catch(this.handleError); |
46 | 46 | } |
47 | + delete(id: number): Promise<any> { | |
48 | + return this.http.delete(this.url + '/delete?id=' + id, { headers: this.headers }) | |
49 | + .toPromise() | |
50 | + .then(response => response.json()) | |
51 | + .catch(this.handleError); | |
52 | + } | |
47 | 53 | private handleError(error: any): Promise<any> { |
48 | 54 | console.error('An error occured', error); |
49 | 55 | return Promise.reject(error.message || error); | ... | ... |