1.0.4 • Published 4 years ago

ng11-smart-generic-table v1.0.4

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
4 years ago

Smart Generic Table

Linkedin GitLab Typescript Angular

Generic Table to use in Angular 11 Apps. Provides a generic structure to define table settings: data, width, translation, pagination, using the following libraries

smart-generic-tableAngularngx-bootstrapboostrap@angular/localizengx-translate/core@ngx-translate/http-loader
1.x.x11.x.x7.1.05.0.211.x.x13.0.06.0.0

Angular 12 Version

Installation

Install Dependencies

$ ng add ngx-bootstrap

If you do not wish to use Bootstrap's global CSS, we now package the project with only the relevant bootstrap styling needed for the dropdown. As such, you can remove the bootstrap styling from angular.json.

Further, Angular CLI should tree-shake the rest of Ngx-Boostrap away if you don't utilize other dependencies from the bootstrap package. This should keep this dependency a lean feature-add

Install This Library

$ npm install smart-generic-table --save

Usage

Import

Add SmartGenericTableModule and TranslateModule in your application module:

imports: [SmartGenericTableModule, TranslateModule];

Model:

export class TableColumnSettings {
  key: string;
  title: string;
  templateName: string;
  width: string;
  show: boolean;
  objectKey?: string;
  extra?: any[];
}

export class TablePagination<T> {
  list: Array<T> = [];
  from = 0;
  to = 0;
  total = 0;
  pages = 0;
}

Example Usage

export class Product {
  id: number;
  name: string;
  code: string;
  price: number;
  status: boolean;
}

Create a enum with entity Keys

export enum ProductsTableColumnKey {
  Id = "id",
  Name = "name",
  Code = "code",
  Price = "price",
  Status = "status"
}

Choose a template available according to your class/interface attribute type

export enum TemplateNames {
  NumberTemplate = 'numberTemplate',
  TextTemplate = 'textTemplate',
  DateTemplate = 'dateTemplate',
  FullDateTemplate = 'fullDateTemplate',
  TimeTemplate = 'timeTemplate',
  StatusTemplate = 'statusTemplate',
  CurrencyTemplate = 'currencyTemplate',
  ActionsTemplate = 'actionsTemplate'
}

Choose a button type if your template chosen is 'ActionsTemplate'

export enum ButtonType {
  Modal = 'modal',
  Swal = 'swal',
  Href = 'href',
}

Product Table Settings Example:

export const PRODUCTTABLESETTINGS: TableColumnSettings[] = [
  {
    key: ProductsTableColumnKey.Code,
    title: 'MODEL.PRODUCT.CODE',
    templateName: TemplateNames.TextTemplate,
    width: 'w-15',
    show: true
  },
  {
    key: ProductsTableColumnKey.Name,
    title: 'MODEL.PRODUCT.NAME',
    templateName: TemplateNames.TextTemplate,
    width: 'w-25',
    show: true
  },
  {
    key: ProductsTableColumnKey.Price,
    title: 'MODEL.PRODUCT.PRICE',
    templateName: TemplateNames.CurrencyTemplate,
    width: 'w-15',
    show: true
  },
  {
    key: ProductsTableColumnKey.Status,
    title: 'SHARED.TABLE.STATUS',
    templateName: TemplateNames.StatusTemplate,
    width: 'w-15',
    show: true
  },
  {
    key: ProductsTableColumnKey.Id,
    title: 'SHARED.TABLE.ACTIONS',
    templateName: TemplateNames.ActionsTemplate,
    width: 'w-15',
    show: true,
    extra: [
      {
        type: ButtonType.Modal,
        tooltip: 'MODEL.PRODUCT.DETAIL',
        class: 'btn btn-sm btn-info',
        icon: 'far fa-window-restore'
      },
      {
        type: ButtonType.Swal,
        key: ProductsTableColumnKey.Status,
        enable: {
          tooltip: 'MODEL.PRODUCT.DISABLE',
          class: 'btn btn-sm btn-success',
          icon: 'fas fa-lock-open'
        },
        disable: {
          tooltip: 'MODEL.PRODUCT.ENABLE',
          class: 'btn btn-sm btn-danger',
          icon: 'fas fa-lock'
        }
      }
    ]
  }
]

The library includes a languageService looking for the language set on localstorage, you can set your language.json inside assets folder and fill your dictionary, in this case shared and table are basics, you can add all the tables you need here.

    ├── src
    │   ├── assets
    │   │    ├── i18n
    │   │    │     ├── es.json
    │   │    │     ├── en.json
{
  "SHARED":{
    "ENABLED": "Enabled",
    "DISABLED": "Disabled"
  },
  "TABLE": {
    "SHOW": "Show",
    "ENTRIES": "entries",
    "SEARCH": "Search",
    "SHOWING": "Showing",
    "TO": "a",
    "OF": "of",
    "GO": "Go page",
    "HREF": "Go to",
    "ACTIONS": "Actions",
    "STATUS": "Status",
    "FILTERS": "Filters",
    "NORECORDS": "No records registered to show"
  },
  "MODEL": {
    "PRODUCT": {
      "ID": "Id",
      "NAME": "Name",
      "CODE": "Supplier Code",
      "PRICE": "Price",
      "DETAIL": "Product Detail",
      "ENABLE": "Enable Product",
      "DISABLE": "Disable Product"
    }
  }
}

Important Options

OptionsTypeDefaultDescription
tableSettingsTableColumnSettings[]undefinedIncludes attribute's key, title, templateName, width and show options
tablePaginationTablePagination<any>undefinedStore the data and pagination info: from, to, pages, total
pageSizenumber10Size of elements shown in the table
hasPaginationbooleantrueConditional to show pagination or not
hasPageSizebooleantrueConditional to show page size select
hasPageSearchbooleantrueConditional to show page search input
currencystringCLPCurrency preferred if you chose CurrencyTemplate
symbolstringsymbol-narrowCurrency symbol
digitsstring.0-0Currency digits
setPageEmitterEventEmitter<number>1Returns the page selected from pagination to parent component
setPageSizeEmitterEventEmitter<number>10Returns the page size selected to parent component
onButtonClickedEmmiterEventEmitter<any>Returns a json including the selected row and Button Type. Example: {data: {id: 1, name: 'Product'}, type: 'swal'}

Example

Refer to main app in this repository for working example.

<sg-table [tableSettings]="settings"
          [titles]="titles"
          [tablePagination]="tablePagination"
          (setPageEmitter)="setPage($event)"
          (setPageSizeEmitter)="setPageSize($event)"
          [hasPagination]="true"
          (onButtonClickedEmmiter)="onButtonClickedEmitter($event)"></sg-table>

Library Contributions

  • Fork repo.
  • Update ./projects/smart-generic-table
  • Build / test library.
  • Update ./src/app with new functionality.
  • Update README.md
  • Pull request.

Helpful commands

  • Build lib: $ npm run build_lib
  • Copy license and readme files: $ npm run copy-files
  • Create package: $ npm run npm_pack
  • Build lib and create package: $ npm run package

Use locally

After building and creating package, you can use it locally too.

In your project run:

$ npm install --save {{path to your local '*.tgz' package file}}

Contributors

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago