0.2.1 • Published 5 years ago

simpleng v0.2.1

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

SimpleNG Library

npm version

SimpleNG is Angular library that contains simple and easy-to-use components. Bootstrap css classes are used in the UI; however, the customization is possible by using component-specific css classes.

Available Components

  • Table
  • Alert

Dependencies

Angular 5+

Install

npm install simpleng --save

Setup

import { SimpleNGModule } from 'simpleng';

@NgModule({
  ...
  imports: [
    ...
    SimpleNGModule,
  ],
})
export class AppModule {}

Global configurations can be provided by importing SimpleNGModule.configure({...})

import { SimpleNGModule } from 'simpleng';

@NgModule({
  ...
  imports: [
    ...
    SimpleNGModule.configure({
      table: {...},
      pagination: {...},
      alert: {...}
    }),
  ],
})
export class AppModule {}

Table

SNGTable is a component that renders html table, with sorting and pagination user interface. Data should be passed manually instead of slicing and sorting them internally.

Usage

<sng-table (pageChange)="loadData($event)">
  <sng-table-column>
    <th *sngTableHeader>ID</th>
    <td *sngTableRow="let row">{{ row.id }}</td>
  </sng-table-column>
  <sng-table-column sort="title">
    <th *sngTableHeader>Title</th>
    <td *sngTableRow="let row">{{ row.title }}</td>
  </sng-table-column>
  <sng-table-column sort="body">
    <th *sngTableHeader>Body</th>
    <td *sngTableRow="let row">{{ row.body }}</td>
  </sng-table-column>
</sng-table>
@ViewChild(SNGTableComponent) table: SNGTableComponent<any>;
...
loadData(page: SNGTablePage) {
  this.dummyService.getPagedDummyData(
    page.pageNumber,
    page.pageSize,
    page.sortProp,
    page.sortDirection
  ).subscribe(pageResponse => {
    this.table.updateByPageResponse(pageResponse);
  });
}

SNGTableComponent instance should be updated after fetching new data. If the response comes as PageResponse object. Table properties can be updated by calling SNGTableComponent.update(data: T[], pageNumber?: number, pageSize?: number, totalRecords?: number). If the response comes as PageResponse object, a shorthand method can be used instead SNGTableComponent.updateByPageResponse(page).

SNGTableComponent.page contains current page properties. Note that Initial table page object is available in the lifecycle hook ngAfterViewInit().

import { AfterViewInit } from '@angular/core';
...
export class AppComponent implements AfterViewInit {
    @ViewChild(SNGTableComponent) table: SNGTableComponent<any>;
		...
    ngAfterViewInit() {
        this.loadData(this.table.page); // Initial load
    }
}

Pagination

Table components will render the pagination by default. It can be disabled by setting pagination="none" attribute to the component.

<sng-table pagination="none">
  ...
</sng-table>

Configurations

SNGTable component instance can be configured individually by passing [config] for table configuration, and [paginationConfig] for pagination configuration.

<sng-table [config]="{...}" [paginationConfig]="{...}">
  ...
</sng-table>

Table Options

OptionTypeDefaultDescription
responsivebooleantrueMake the table responsive by adding responsive bootstrap class to the component
styleobjectObject containing several options to style the table
style.tableStylestringdefaultTable style which accepts one of the following values: default, bordered, borderless
style.tableThemestringdefaultTable background color which accepts the following values: default, dark, light
style.headerThemestringdefaultTable header background color which accepts the following values: default, dark, light
style.smallbooleanfalsetrue makes the table compact
style.stripedbooleanfalseStriped table rows
style.hoverbooleanfalseHighlight a row when hovering

Pagination Options

OptionTypeDefaultDescription
zeroBasedbooleantruePage data will start from zero
pageSizesstring[]10, 50, 100, 200Page size options to allow users picking one of them
visiblePagesnumber5Maximum number of pagination buttons that appears to the user
defaultPageSizenumber50Default page size

Alert

SNGAlert makes it easy to show bootstrap alerts with different level using SNGAlertService.

Usage

<sng-alert context="general"></sng-alert>
// Injecting SNGAlertService
constructor(private alertService: SNGAlertService) { }
...
showMessage() {
    // Display success message in 'general' alert
    this.alertService.success('Success message', 'general');
    // Adding visibility timeout to the alert in millis
    this.alertService.warning('Warning message', 'general', 1000);
}
...
clearMessage() {
    this.alertService.clear('general');
}

context is optional. If not provided, the alert service will notify the components with no context. Available levels are success, warning, error, info.

Alerts can be sticky, remains at the top of the viewport, if sticky flag is set to true. Also, more than one alert message can appear at the same time in the same SNGAlertComponent instance by setting multi to true.

<sng-alert [sticky]="true" [multi]="true"></sng-alert>

Alert messages can be mapped to something else by creating a service that implements SNGAlertMessagesMapper and then injecting it with SNG_ALERT_MESSAGES_MAPPER token.

  ...
  providers: [
    { provide: SNG_ALERT_MESSAGES_MAPPER, useClass: AlertLocalizedMessages }
  ]
})
export class AppModule {}

Example of mapper service

@Injectable()
export class AlertLocalizedMessages implements SNGAlertMessagesMapper {

  private translations: any;

  constructor(@Inject(LOCALE_ID) localeId: string) {
    // Load translations
  }

  // Return translated message, for example
  getMessage(message: SNGAlertMessage): string {
    return this.translations[message];
  }

}

Configurations

SNGAlert component can be configured individually by passing [config] for alert configuration.

<sng-alert [config]="{...}"></sng-alert>

Options

OptionTypeDefaultDescription
dismissablebooleantrueAdds dismiss button on each alert
animationbooleantrueEnables show/hide animation
styleobjectObject containing several options to style the table
style.stickyShadowbooleantrueActivates the shadow when alerts sticks at the top
0.2.1

5 years ago

0.2.0

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago