Giter VIP home page Giter VIP logo

angular-data-table's Introduction

Angular generic data table component

Angular generic data table with list, sort, pagination, search on backend API (with generic API DAO)

DaoGeneric contient principalement la fonction list() qui construit la requête qui sera envoyée au backend API

Format de l'URL construite pour la liste : (il est possible de modifier ces différents paramètres en surchargeant _getPagination(), _getSorting() et _getParams()

  • pour la pagination : &limit=n&offset=m
  • pour le sort : &ordering=[-]champ le - fait un tri desc
  • pour le search : &search=txt

Exemple : https://mondomaine/api/projets/?limit=20&offset=5 : renvoie les 20 projets, en commençant au 5ème

Format de retour de la liste :

L'objet ramené est de type Pagination :

Pagination { 
  total: number; // total des éléments en base (par exemple, 1 000 projets)
  totalView: number; // total de la pagination (par exemple, 20 projets)
  list: any[]; // liste des éléments ramenés
}

Lors de l'implémentation de DaoGeneric (generic.dao.ts), il est possible de surcharger 3 méthodes supplémentaires pour modifier les paramètres querystring envoyés

_getSorting(): HttpParams : la construction pour le tri
_getPagination(): Pagination : la construction pour la pagination
_getParams(): HttpParams : la construction pour la recherche et la pagination

Exemple d'utilisation :

user.model.ts

export interface User {
   nom: string;
   prenom: string;
};

user.service.ts

import {User} from "./user.model";

@Injectable()
export class UserService extends DaoGeneric<User> {
  private url = `${environment.baseUrl}/user/`;

  constructor(private httpClient: HttpClient) {
    super(httpClient);
  }

  getRootUrl(): string {
    return this.url;
  }
}

myapp.component.html

<app-data-table
      [dataSource]="ds" 
      [extraParams]="extraParams"
      [filterDisplay]="true"
      [placeHolderFilter]="'Rechercher par ...'"
      [toolTipFilter]="'Rechercher par ...'"
      [flexAction]="5" [flexColumn]="90"
      [pageSize]="10" [pageSizesList]="[5,10,25]"
      [columns]="columns" [actions]="actions">
</app-data-table>

myapp.component.ts

@Component({
  selector: 'app-my-component',
  templateUrl: './myapp.component.html'
})
export class MyComponent implements OnInit {
  ds: MatDataSourceGeneric<User> = new MatDataSourceGeneric<User>();

  extraParams: Map<string, string> = new Map<string, string>();
  
  @ViewChild(DataTableComponent) dataTable: DataTableComponent;

  columns: ColumnDataTable[] = [
    { columnDef: 'nom', header: 'Nom', display: (element: User) => {        
        return user.nom;
      } , sort: true, hidden: false, flex: 15 },
    { columnDef: 'prenom', header: 'Prénom', display: (element: User) => {        
        return user.prenom;
      } , sort: true, hidden: false, flex: 15 },
   ];
   
   actions: ActionDataTable[] = [
    { label: 'Supprimer', tooltip: 'Supprimer l\'utilisateur',  icon: 'delete',
      click: (row) => this.delete(row), flex: 3, }
    ];
   
   constructor(private userSvc: UserService) {
       this.ds.daoService = userSvc;
   }
   
   delete(row: User) {
      // 
   }
      
}

angular-data-table's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.