JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 59
  • Score
    100M100P100Q76956F
  • License MIT

Package Exports

  • ng-crud-kit
  • ng-crud-kit/package.json

Readme

NgCrudKit

CRUD Utility to be used with Angular Material.

Installation

First, ensure you have Angular Material set up in your project:

ng add @angular/material

Then, install this package via NPM:

npm install ng-crud-kit

Since this is a standalone component, you can import it directly into your component's imports array. You'll also need to ensure HttpClientModule and ReactiveFormsModule are available in your application's app.config.ts or app.module.ts:

import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';
import { ReactiveFormsModule } from '@angular/forms';

export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(),
    importProvidersFrom(ReactiveFormsModule)
  ]
};

And that's it! Just use the selectors for the components in your HTML and you're all set!

ng-curd-aio

A versatile, all-in-one Angular component for handling common CRUD (Create, Read, Update, Delete) operations. This component is designed to be highly configurable, working out of the box with your REST API or in a manual mode where you handle the API calls yourself.

It comes with two main modes:

  • auto (default): The component handles all API requests (GET, POST, PUT, DELETE) internally, requiring only the API endpoint and form configuration.

  • manual: The component emits events for data loading, saving, and deletion, allowing you to manage the API calls and data flow from the parent component.

Usage

  1. auto mode

This is the default and simplest way to use the component. It's ideal for a standard CRUD table interface where the component manages all the data. Set your parent component as:

import { Component } from '@angular/core';
import { NgCrudAioComponent } from 'ng-crud-aio';
import { CrudFormItem, CrudTableColumns } from 'ng-crud-aio/models';

@Component({
  standalone: true,
  selector: 'app-root',
  template: `
    <ng-crud-aio
      [title]="'User Management'"
      [urlEndpoint]="'users'"
      [fields]="formFields"
      [displayedColumns]="displayedCols"
      [columns]="tableColumns">
    </ng-crud-aio>
  `,
  imports: [NgCrudAioComponent]
})
export class AppComponent {
  // Your API will be called at: 'http://localhost:4200/api/users'
  // using this component.
  
  formFields: CrudFormItem[] = [
    { name: 'name', type: 'text', required: true, label: 'Full Name', defaultValue: '' },
    { name: 'email', type: 'email', required: true, label: 'Email', defaultValue: '' }
  ];

  displayedCols: string[] = ['name', 'email'];

  tableColumns: CrudTableColumns[] = [
    { name: 'name', title: 'User Name', isSortable: true, isSearchable: true },
    { name: 'email', title: 'Email Address', isSortable: false, isSearchable: true }
  ];
}
  1. manual mode

Use this mode if you'd like to have full control of the data flow. Instead of realying on the component's making the HTTP requests you'll receive emissions from each action and you can make the requests yourself.

import { Component } from '@angular/core';
import { NgCrudAioComponent } from 'ng-crud-aio';
import { CrudFormItem } from 'ng-crud-aio/models';

@Component({
  standalone: true,
  selector: 'app-root',
  template: `
    <ng-crud-aio
      [mode]="'manual'"
      [hasTable]="false"
      [title]="'Edit Profile'"
      [subtitle]="'Update your personal information'"
      [fields]="formFields"
      (dataLoaded)="loadProfile($event)"
      (recordSaved)="saveProfile($event)">
    </ng-crud-aio>
  `,
  imports: [NgCrudAioComponent]
})
export class AppComponent {
  formFields: CrudFormItem[] = [
    { name: 'firstName', type: 'text', required: true, label: 'First Name' },
    { name: 'lastName', type: 'text', required: true, label: 'Last Name' }
  ];

  loadProfile(id: number) {
    // Implement your logic to fetch the user profile from your API
    // and patch the form data in the ng-crud-aio component.
    console.log('User requested to load profile for ID:', id);
  }

  saveProfile(formData: any) {
    // Implement your logic to save the updated profile to your API.
    console.log('User submitted form data:', formData);
  }
}

Sending Bearer Token

In most cases you'd like to submit a Bearer token or some other authentication via your headers.
To accomplish this just create an interceptor within you app, if havent yet, and let this interceptor inject the required header for all HTTP calls. This is an example on how to inject a Bearer token stored in the Local Storage:

// ./interceptors/auth.interceptor.ts
import { HttpInterceptorFn } from '@angular/common/http';

export const authInterceptor: HttpInterceptorFn = (req, next) => {
  const token = localStorage.getItem('jwt_token');

  if (token) {
    const clonedRequest = req.clone({
      setHeaders: {
        Authorization: `Bearer ${token}`
      }
    });
    return next(clonedRequest);
  }

  return next(req);
};

And in your app.config.ts use it in your HttpClient provider:

//all imports go here
import { authInterceptor } from './interceptors/auth.interceptor';

export const appConfig: ApplicationConfig = {
  //other configs go here
  providers: [
    //other providers go here
    provideHttpClient(
      withInterceptors([authInterceptor])
    )
  ]
};

API

Inputs

The form is built inside a Material Card and the table is a Material Table. Some of the inputs are used to design those elements.

Property Type Default Description
mode 'auto'|'manual' 'auto' Let's you decide wether you want the component to automatically handle the API calls or if you just want to hear the emitters and handle them yourself.
tableData any[] [] Manual mode only: the data to display in the table.
title string 'CRUD' The main title of the page or form. The form is presented in a Material Card, and this is the title of the card
subtitle string '' A subtitle to display below the main title. If no subtitle needed, leave blank.
returnBtnUrl string '/' The URL for the top-left return button.
returnBtnText string 'Return to Home' The text for the return button.
returnBtnIcon string '' An Angular Material icon name for the return button. If no icon needed, leave blank.
apiUrl string 'http://localhost:4200/api/' Auto mode only: the base URL for your backend API.
urlEndpoint string 'items' Auto mode only: the specific endpoint for your resource (e.g., 'users', 'products').
fields CrudFormItem[] [] An array defining the form fields and their properties. See CrudFormItem model below.
displayedColumns string[] [] An array of column names that will be displayed in the table.
columns CrudTableColumns[] [] An array defining the table column headers and their behavior. See CrudTableColumns models below.
idField string 'id' Auto mode only: the name of the ID field present in the table. Used, for example, when removing a record which field specifies which record to be removed
hasTable boolean true Indicates if the component should display a table or just a single record form. Set it to false if you are editing single record items, such as user profile or settings

Outputs

These events are only emitted when mode is set to 'manual'.

Event Type Description
dataLoaded EventEmitter Emits when an existing record is requested for editing. The emitted value is the record's ID.
recordSaved EventEmitter Emits when a new or existing record is saved. The emitted value is the form data.
recordRemoved EventEmitter Emits when a record is requested for removal. The emitted value is the record's ID.

Models

The component relies on two models to define its structure and behavior.

CrudFormItem This model defines the properties of each input field in your form. They will be used to build elements in a FormGroup.

Property Type Required Description
name string Yes The name of the form control.
type string Yes The input type (e.g., 'text', 'email').
label string Yes The display label for the input field.
defaultValue any No A default value for the field.
required boolean No Sets a Validators.required on the field.
options any[] No Options for select/dropdown fields.

CrudTableColumns This model defines the properties for each column in your table.

Property Type Required Description
name string Yes The name of the data property to display in the column.
title string No The display name for the column header. Defaults to name if not provided.
isSortable boolean No Indicates if the column can be sorted.
isSearchable boolean No Indicates if the column's data can be searched.

Upcoming

Two separate components to have table and form in different routes, useful for more complex records.

License

This component is open-sourced under the MIT license.