packages/ui/src/lib/list/list.component.ts
The ListComponent will render a list containing the given items or collection.
changeDetection | ChangeDetectionStrategy.OnPush |
encapsulation | ViewEncapsulation.None |
selector | ec-list |
template |
|
Properties |
|
Methods |
Inputs |
Outputs |
constructor(listConfig: ListConfigService, cdr: ChangeDetectorRef)
|
|||||||||
Parameters :
|
autoFocusFirst | |
Default value : false
|
|
If true, the first item in the list will always be focused after changed |
collection | |
Type : Collection<T>
|
|
The used collection |
config | |
Type : ListConfig<T>
|
|
items | |
Type : Array<T>
|
|
The visible items |
list | |
Type : List<T>
|
|
The Instance of the List |
pagination | |
Type : Pagination<T>
|
|
Pagination that should be used |
paginationConfig | |
Type : PaginationConfig
|
|
Custom PaginationConfig |
selection | |
Type : Selection<T>
|
|
The used selection |
solo | |
Type : boolean
|
|
If true, only one item is selectable next |
changed | |
Type : EventEmitter<List<T>>
|
|
emits after the list changed |
columnClicked | |
Type : EventEmitter<Item<T>>
|
|
Event emitter on item selection |
selected | |
Type : EventEmitter<Selection<T>>
|
|
Event emitter on selection change |
columnClick | ||||
columnClick(item)
|
||||
Column click handler. Triggers select.emit(item) with fallback to selection.toggle
Parameters :
Returns :
any
|
filter | ||||||
filter(property, value)
|
||||||
Filters the list
Parameters :
Returns :
void
|
focusFirst |
focusFirst()
|
Returns :
void
|
focusNext |
focusNext()
|
Selects the next item
Returns :
void
|
focusPrev |
focusPrev()
|
Selects the previous item
Returns :
void
|
init | ||||||
init(list: List
|
||||||
Parameters :
Returns :
void
|
ngOnChanges | ||||
ngOnChanges(changes?)
|
||||
Changing items or collection will trigger reconstructing the list with the new items. Changing the selection will reconstruct the selection
Parameters :
Returns :
void
|
selectIndex | ||||||
selectIndex(index: number)
|
||||||
Selects the item with the given index
Parameters :
Returns :
void
|
showHeader |
showHeader()
|
Decides if the header should be visible or not
Returns :
any
|
Public cdr |
Type : ChangeDetectorRef
|
config |
Type : ListConfig<T>
|
Default value : {}
|
The current list config |
focusItem |
Type : Item<T>
|
Current focus |
isLoading |
Default value : false
|
Config input for List |
Public listConfig |
Type : ListConfigService
|
The List is one of the main components to work with. The basic idea is to display collections of data in a structured but flexible way.
You can create a new List like this:
this.trees = new List(
//first argument: Array of Objects
[{
name: 'Appletree',
height: 10,
fruits: true
}, {
name: 'Lemontree',
height: 8,
fruits: true
}, {
name: 'Birch',
height: 20,
fruits: false
}],
//second (optional) argument: ListConfig
{
fields: {
name: {
label: 'Name'
},
height: {
label: 'Height',
group: (h) => h > 10 ? 'Higher than 10m' : 'Lower than 10m'
},
fruits: {
label: 'Has Fruits?',
display: (value) => value ? 'yes' : 'no'
},
}
})
This is how you display a List instance into your template:
<ec-list [list]="trees"></ec-list>
import {
Component,
EventEmitter,
Input,
OnChanges,
Output,
ViewEncapsulation,
ChangeDetectionStrategy,
ChangeDetectorRef,
} from '@angular/core';
import { Collection, List, ListConfig, Selection, Pagination } from '@ec.components/core';
import { Item } from '@ec.components/core';
import { PaginationConfig } from './pagination/pagination-config.interface';
import { ListConfigService } from './list-config.service';
import { listTemplate } from './list.component.html';
/**
* The ListComponent will render a list containing the given items or collection.
*
* <example-url>https://components.entrecode.de/ui/list/basic?e=1</example-url>
* <example-url>https://components.entrecode.de/ui/list/transforms?e=1</example-url>
* */
@Component({
selector: 'ec-list',
/* templateUrl: './list.component.html', */
template: listTemplate,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListComponent<T> implements OnChanges {
/** The current list config */
config: ListConfig<T> = {};
/** Config input for List */
/** Flag that flips true when loading. */
isLoading = false;
// tslint:disable-next-line:no-input-rename
@Input('config') configInput: ListConfig<T>;
/** The visible items */
@Input() items: Array<T>;
/** The used collection */
@Input() collection: Collection<T>;
/** The used selection */
@Input() selection: Selection<T>;
/** If true, only one item is selectable next */
@Input() solo: boolean;
/** Event emitter on item selection */
@Output() columnClicked: EventEmitter<Item<T>> = new EventEmitter();
/** Event emitter on selection change */
@Output() selected: EventEmitter<Selection<T>> = new EventEmitter();
/** The Instance of the List */
@Input() list: List<T>;
/** Pagination that should be used */
@Input() pagination: Pagination<T>;
/** Custom PaginationConfig */
@Input() paginationConfig: PaginationConfig;
/** If true, the first item in the list will always be focused after changed */
@Input() autoFocusFirst = false;
/** Current focus */
focusItem: Item<T>;
/** emits after the list changed */
@Output() changed: EventEmitter<List<T>> = new EventEmitter();
constructor(public listConfig: ListConfigService, public cdr: ChangeDetectorRef) {}
/** Changing items or collection will trigger reconstructing the list with the new items.
* Changing the selection will reconstruct the selection */
ngOnChanges(changes?) {
this.config = Object.assign(this.config || {}, this.configInput || {});
if (this.items) {
this.init(new List(this.items, this.config, this.pagination));
} else if (this.collection) {
this.init(new List(this.collection.items, this.config, this.pagination));
}
}
init(list: List<T>) {
if (!list) {
console.warn('tried to init list.component with undefined list');
return;
}
this.list = list;
this.listConfig.applyConfig(this.list);
this.list.change$.subscribe(() => {
if (this.autoFocusFirst || this.list.isFiltered()) {
this.focusFirst();
} else {
delete this.focusItem;
}
this.cdr.markForCheck();
this.changed.emit(this.list);
});
if (!this.selection) {
this.selection = new Selection([], this.list.config);
}
if (this.selection) {
this.selection.update$.subscribe((selection: Selection<T>) => {
this.selected.emit(selection);
});
}
}
/** Column click handler. Triggers select.emit(item) with fallback to selection.toggle*/
columnClick(item) {
if (this.list.config.selectMode && this.selection) {
this.selection.toggle(item, this.solo);
} else if (this.columnClicked.observers.length) {
return this.columnClicked.emit(item);
}
}
/** Decides if the header should be visible or not */
showHeader() {
return (
this.list &&
this.list.config &&
!this.list.config.disableHeader &&
(this.list.fields.length || this.list.config.title)
);
/* && (this.list.config.alwaysShowHeader || !this.list.isEmpty()); */
}
/** Selects the item with the given index */
selectIndex(index: number) {
if (!this.selection || this.list.isEmpty() || !this.list.items[index]) {
return;
}
this.selection.select(this.list.items[index]);
}
focusFirst() {
delete this.focusItem;
this.focusNext();
}
/** Selects the next item */
focusNext() {
if (!this.list) {
return;
}
let index = 0;
if (this.focusItem) {
index = this.list.page.indexOf(this.focusItem) + 1;
}
this.focusItem = this.list.page[index % this.list.page.length];
this.cdr.markForCheck();
}
/** Selects the previous item */
focusPrev() {
if (!this.list) {
return;
}
let index = this.list.page.length - 1;
if (this.focusItem) {
index = this.list.page.indexOf(this.focusItem) + this.list.page.length - 1;
}
this.focusItem = this.list.page[index % this.list.page.length];
this.cdr.markForCheck();
}
/** Filters the list */
filter(property, value) {
this.list.filter(property, value);
}
}