packages/data/src/lib/resource-crud/resource-crud.component.ts
The ResourceCrudComponent takes a relation name and api to render a resource list with create/edit/delete functionality out of the box.
```html
selector | ec-resource-crud |
templateUrl | ./resource-crud.component.html |
Properties |
Methods |
|
Inputs |
Outputs |
constructor(sdk: SdkService, auth: AuthService, loaderService: LoaderService, notificationService: NotificationsService, router: Router, route: ActivatedRoute)
|
|||||||||||||||||||||
Parameters :
|
api | |
Type : Core
|
|
The API Connector that possesses the resource list, see https://entrecode.github.io/ec.sdk/#api-connectors |
config | |
Type : CrudConfig<T>
|
|
Default value : {}
|
|
CrudConfig for customization of the crud's UI. |
relation | |
Type : string
|
|
The name of the resource. If given, the generic ListResource loading will be used (api.resourceList) |
selection | |
Type : Selection<T>
|
|
The selection that should be used |
columnClicked | |
Type : EventEmitter<any>
|
|
Emits when a list element is clicked |
createClicked | |
Type : EventEmitter<any>
|
|
Output that is nexted when pressing the create button |
selected | |
Type : EventEmitter<any>
|
|
Emits when the selection has changed |
create |
create()
|
Method that is invoked when pressing the create button. Default behaviour is opening the resource-pop.
Returns :
void
|
Public hasMethod | ||||||
hasMethod(method: string)
|
||||||
Returns true if the given method is part of the methods array (or if there is no methods array)
Parameters :
Returns :
boolean
|
initMethods |
initMethods()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
select | ||||
select(item)
|
||||
Called on list columnClicked
Parameters :
Returns :
void
|
list |
Type : ResourceListComponent
|
Decorators :
@ViewChild(ResourceListComponent)
|
The ResourceListComponent inside the template. |
loader |
Type : LoaderComponent
|
Decorators :
@ViewChild(LoaderComponent, {static: true})
|
The lists loader |
pop |
Type : ResourcePopComponent
|
Decorators :
@ViewChild(ResourcePopComponent, {static: true})
|
The Pop inside the template. |
Public route |
Type : ActivatedRoute
|
Decorators :
@Optional()
|
Public router |
Type : Router
|
Decorators :
@Optional()
|
import { Component, EventEmitter, Input, OnInit, Optional, Output, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { SdkService } from '../sdk/sdk.service';
import { Selection } from '@ec.components/core';
import { LoaderComponent } from '@ec.components/ui';
import { LoaderService } from '@ec.components/ui';
import { NotificationsService } from '@ec.components/ui';
import { AuthService } from '../auth/auth.service';
import { WithLoader } from '@ec.components/ui';
import { CrudConfig } from '../crud/crud-config.interface';
import { ResourceListComponent } from '../resource-list/resource-list.component';
import { ResourcePopComponent } from '../resource-pop/resource-pop.component';
import Core from 'ec.sdk/lib/Core';
/** The ResourceCrudComponent takes a relation name and api to render a resource list with create/edit/delete functionality out of the box.
* ```html
* <ec-resource-crud [api]="sdk.datamanager" relation="dataManager"></ec-resource-crud>
* ```
* */
@Component({
selector: 'ec-resource-crud',
templateUrl: './resource-crud.component.html',
})
export class ResourceCrudComponent<T> implements OnInit, WithLoader {
/** The API Connector that possesses the resource list, see https://entrecode.github.io/ec.sdk/#api-connectors */
@Input() api: Core; // sdk api connector
/** The name of the resource. If given, the generic ListResource loading will be used (api.resourceList) */
@Input() relation: string;
/** CrudConfig for customization of the crud's UI.*/
@Input() config: CrudConfig<T> = {};
/** The selection that should be used */
@Input() selection: Selection<T>;
/** The ResourceListComponent inside the template. */
@ViewChild(ResourceListComponent) list: ResourceListComponent;
/** The Pop inside the template. */
@ViewChild(ResourcePopComponent, { static: true }) pop: ResourcePopComponent;
/** The lists loader */
@ViewChild(LoaderComponent, { static: true }) loader: LoaderComponent;
/** Emits when a list element is clicked */
@Output() columnClicked: EventEmitter<any> = new EventEmitter();
/** Emits when the selection has changed */
@Output() selected: EventEmitter<any> = new EventEmitter();
/** Output that is nexted when pressing the create button */
@Output() createClicked: EventEmitter<any> = new EventEmitter();
constructor(
private sdk: SdkService,
private auth: AuthService,
private loaderService: LoaderService,
private notificationService: NotificationsService,
@Optional() public router: Router,
@Optional() public route: ActivatedRoute,
) {}
ngOnInit() {
this.initMethods();
}
initMethods() {
if (!this.relation) {
return;
}
this.auth.getAllowedResourceMethods(this.relation, {}, this.config.methods).then((methods) => {
this.config.methods = methods;
});
}
/** Returns true if the given method is part of the methods array (or if there is no methods array) */
public hasMethod(method: string) {
return this.config.methods && this.config.methods.indexOf(method) !== -1;
}
/** Called on list columnClicked */
select(item) {
if (!item) {
return;
}
if (this.columnClicked.observers.length) {
this.columnClicked.emit(item);
return;
}
this.pop.edit(item.getBody());
// TODO: check CrudComponent#loadEntry for further inspiration
}
/** Method that is invoked when pressing the create button. Default behaviour is opening the resource-pop. */
create() {
if (this.createClicked.observers.length) {
this.createClicked.next();
} else if (this.pop) {
this.pop.create();
}
}
}
<div class="ec-crud">
<header class="ec-crud-header">
<ul class="nav">
<li class="nav__item" *ngIf="hasMethod('post')">
<a (click)="create()" class="btn" [class.btn_square]="!config?.createLabel">
<span *ngIf="config?.createLabel">{{ config?.createLabel }}</span>
<span *ngIf="!config?.createLabel">
<ec-icon name="add"></ec-icon>
</span>
</a>
</li>
<li class="nav__item" *ngIf="!config?.disableSelectSwitch">
<a (click)="list.list?.toggleSelectMode()" class="btn btn_square">
<ec-icon name="checkbox"></ec-icon>
</a>
</li>
</ul>
</header>
<div class="ec-crud-body">
<ec-resource-list
[loader]="config?.loader || loader"
[api]="api"
[selection]="selection"
[relation]="relation"
(columnClicked)="select($event)"
*ngIf="hasMethod('get')"
[config]="config"
></ec-resource-list>
</div>
<ec-loader class="ec-loader loader is-global" #loader></ec-loader>
<!-- [resource]="resource" -->
<ec-resource-pop [api]="api" [config]="config" [relation]="relation" #resourcePop></ec-resource-pop>
<ec-notifications></ec-notifications>
</div>