Skip to content

Commit

Permalink
Merge pull request #21158 from abpframework/auto-merge/rel-9-0/3117
Browse files Browse the repository at this point in the history
Merge branch dev with rel-9.0
  • Loading branch information
maliming authored Oct 23, 2024
2 parents 12ddbfe + 8cef757 commit 207b000
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 29 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 69 additions & 29 deletions docs/en/framework/ui/angular/list-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,37 +139,86 @@ You may use observables in combination with [AsyncPipe](https://angular.io/guide
<!-- DO NOT WORRY, ONLY ONE REQUEST WILL BE MADE -->
```

...or...
## Handle request status
To handle the request status `ListService` provides a `requestStatus$` observable. This observable emits the current status of a request, which can be one of the following values: `idle`, `loading`, `success` or `error`. These statuses allow you to easily manage the UI flow based on the request's state.

![RequestStatus](./images/list-service-request-status.gif)

```js
@Select(BookState.getBooks)
books$: Observable<BookDto[]>;
import { ListService } from '@abp/ng.core';
import { AsyncPipe } from '@angular/common';
import { Component, inject } from '@angular/core';
import { BookDto, BooksService } from './books.service';

@Select(BookState.getBookCount)
bookCount$: Observable<number>;
@Component({
standalone: true,
selector: 'app-books',
templateUrl: './books.component.html',
providers: [ListService, BooksService],
imports: [AsyncPipe],
})
export class BooksComponent {
list = inject(ListService);
booksService = inject(BooksService);

ngOnInit() {
this.list.hookToQuery((query) => this.store.dispatch(new GetBooks(query))).subscribe();
items = new Array<BookDto>();
count = 0;

//It's an observable variable
requestStatus$ = this.list.requestStatus$;

ngOnInit(): void {
this.list
.hookToQuery(() => this.booksService.getList())
.subscribe(response => {
this.items = response.items;
this.count = response.totalCount;
});
}
}
```

```html
<!-- simplified representation of the template -->

<ngx-datatable
[rows]="(books$ | async) || []"
[count]="(bookCount$ | async) || 0"
[list]="list"
default
>
<!-- column templates here -->
</ngx-datatable>
<div class="card">
<div class="card-header">
@if (requestStatus$ | async; as status) {
@switch (status) {
@case ('loading') {
<div style="height: 62px">
<div class="spinner-border" role="status" id="loading">
<span class="visually-hidden">Loading...</span>
</div>
</div>
}
@case ('error') {
<h4>Error occured</h4>
}
@default {
<h4>Books</h4>
}
}
}
</div>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
</thead>

<tbody>
@for (book of items; track book.id) {
<tr>
<td>{{ book.id }}</td>
<td>{{ book.name }}</td>
</tr>
}
</tbody>
</table>
</div>
```

> We do not recommend using the NGXS store for CRUD pages unless your application needs to share list information between components or use it later on in another page.

## How to Refresh Table on Create/Update/Delete

`ListService` exposes a `get` method to trigger a request with the current query. So, basically, whenever a create, update, or delete action resolves, you can call `this.list.get();` and it will call hooked stream creator again.
Expand All @@ -183,15 +232,6 @@ You may use observables in combination with [AsyncPipe](https://angular.io/guide
});
```

...or...

```js
this.store.dispatch(new DeleteBook(id)).subscribe(this.list.get);
```

> We do not recommend using the NGXS store for CRUD pages unless your application needs to share list information between components or use it later on in another page.

## How to Implement Server-Side Search in a Table

`ListService` exposes a `filter` property that will trigger a request with the current query and the given search string. All you need to do is to bind it to an input element with two-way binding.
Expand Down

0 comments on commit 207b000

Please sign in to comment.