0.0.1-n • Published 9 months ago

angular-tz-datatable-core v0.0.1-n

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

AngularTzDatatableCore

AngularTzDatatableCore is a powerful and flexible Angular library designed to streamline the creation and management of data tables in your Angular applications. It offers features such as searching, sorting, pagination, and customizable column visibility, making it an essential tool for developers looking to build interactive data-driven applications.

It is a generic, strictly-typed Angular library designed for flexible and type-safe datatable management.

Use AngularTzDatatableCore to build beautiful, customizable Angular datatables effortlessly.

Note that this library will support API data fetching in the future.

Live Demo

Test and interact with the AngularTzDatatableCore library directly in your browser! Try it out on Stackblitz (Demo) for a hands-on demonstration of the library's capabilities.

[Open in StackBlitz

Installation

To install AngularTzDatatableCore, run the following command in your Angular project:

npm install angular-tz-datatable-core

Basic Example

Here’s a simple example of how to use the AngularTzDatatableCore in a component.

1. Sample Columns Configuration

// test-data/datatype.ts

// Type of data to be displayed in the table
export type User = {
    id: number;
    name: string;
    age: number | null;
};

2. Sample Data

// test-data/data.ts
import { Columns } from "angular-tz-datatable-core";
import { User } from './data';

const columns: Columns = {
    id: {
        title: 'ID',
        isSearchableByDefault: false,
        isSortedByDefault: true,
    },
    name: {
        title: 'Name',
    },
    age: {
        title: 'Age',
        isSortableByDefault: false,
        defaultContent: '-',
    },
};

export const data: Array<User> = [
    { id: 2, name: 'Jane Smith', age: 34 },
    { id: 5, name: 'Chris White', age: 30 },
    // ... other user data
]

3. Create datatable instance

import { TzDatatable } from 'angular-tz-datatable-core';
import { data, columns } from '../test-data/data'; // Sample data and columns
import { User } from '../test-data/datatype'; // types of table data

@Component({
    // Your component configuration
})
export class DatatableTestComponent
{
    protected readonly dt: TzDatatable<User> = new TzDatatable();
    
    
    constructor()
    {
        this.dt.initCols(columns); // Must be called before setData
        this.dt.initData(data);
    }
}

4. Use in HTML template

<table>
   <tr>
      <th
         *ngFor="let col of dt.getColsAsArray(); trackBy: col.key"
         *ngIf="col.isVisible"
      >
         {{ col.title }}
      </th>
   </tr>

   <tbody>
      <tr
         *ngFor="let row of dt.getPageDataViewAsAny(); trackBy: row._dataUuid_"
      >
         <td
            *ngFor="let col of dt.getColsAsArray(); trackBy: col.key"
            *ngIf="col.isVisible"
         >
            {{ row[col.key] }}
         </td>
      </tr>
   </tbody>

</table>

API Reference

Suppose T is the type of the data you want to display in the table (here T will be User). In that case, you can use the following methods to interact with the AngularTzDatatableCore service:

import { TzDatatable } from 'angular-tz-datatable-core';

export class YourComponent
{
    protected readonly dt: TzDatatable<User> = new TzDatatable();
}

For the following methods, dt is the instance of the TzDatatable service.

APIReturn TypeUsage
dt.initCols(ColumnsConfig)voidInitializes table columns
dt.initData(Array<T>)voidInitializes table data
dt.getColsAsArray()Array<ColumnsConfigExtended>Get columns configurations
dt.getData()Array<TExtended>Get data
dt.getViewMode()"NORMAL" \| "SEARCH"Get the current view mode
---
dt.getTotalRows()number-
dt.getTotalPages()number-
dt.gotoPage(number)voidGo to a specific page
---
dt.setPageNb(number)voidSet the current page number
dt.getPageNb()number-
dt.getPageSize()numberCurrent Page Size
dt.getPageDataView()Array<TExtended>Get data for the current page view
dt.getPageDataViewAsAny()Array<any>Get data for the current page view (in specific cases)
---
dt.toggleRowSelection(number)voidToggle row selection
dt.toggleAllRowsSelectionInPage()voidToggle all rows selection in the current page
dt.selectAllRowsInPage()voidSelect all rows in the current page
dt.unselectAllRowsInPage()voidUnselect all rows in the current page
dt.getSelectedRowCountOnPage()numberGet the number of selected rows in current page
dt.getUnselectedRowCountOnPage()numberGet the number of unselected rows in current page
dt.getSelectedRowCountBeyondPage()numberGet the number of selected rows beyond current page
dt.getSelectedRowCountInTotal()numberGet the number of selected rows beyond current page
dt.getUnselectedRowCountInTotal()numberGet the number of unselected rows in total
dt.getSelectedRowCountInTotal()numberGet the number of selected rows in total
---
dt.getSortingKey()stringGet the current sorting column
dt.setSortingKey(string)voidSet the current sorting column
---
dt.toggleColVisibility(string)voidToggle column visibility
dt.resetColsVisibility()voidReset column visibility
---
dt.toggleSearchCol(string)voidToggle column as isSearchable
dt.search(string)void-
dt.resetSearchCols()void-
dt.clearSearchCols()void-
dt.getSearchQuery()string-
dt.getSearchChunks()Array<string>- (search query words split by whitespaces)
dt.getSearchCols()Array<string>- (searchable columns)

Events

You can listen to the following events using the addEventListener method:

dt.addEventListener("RowClick", (row) =>
{
    console.log(row);
});
EventCallback Arguments
dt.addEventListener("BeforeSearch", callback)1. processedQuery: { query: string, chunks: Array<string> } 2. e: { preventDefault: () => true }
dt.addEventListener("AfterSearch", callback)-
dt.addEventListener("RowHover", callback)1. row: TExtended<T>
dt.addEventListener("RowClick", callback)1. row: TExtended<T>
dt.addEventListener("CellClick", callback)1. row: TExtended<T> 2. column: string
dt.addEventListener("RowsSelectionFlagChange", callback)1. rows: Array<TExtended<T>> - only effected rows
dt.addEventListener("PageNbChange", callback)1. pageNb: number 2. prevPageNb: number 3. pageSize: number 4. prevPageSize: number
dt.addEventListener("ColumnsSortedFlagChange", callback)1. column: string 2. direction: SortDirection 3. prevColumn: string 4. prevDirection: SortDirection
dt.addEventListener("ColumnsVisibleFlagChange", callback)1. columns: Array<StrictColumn<T>> - only effected columns
dt.addEventListener("ColumnsSearchFlagChange", callback)1. columns: Array<StrictColumn<T>> - only effected columns
dt.addEventListener("RowsPerPageChange", callback)1. rowsPerPage: number 2. prevRowsPerPage: number

Same events can be removed using the removeEventListener method:

dt.removeEventListener("RowClick", callback);

Extended Properties and Methods

Property / MethodReturn TypeUsage
row._dataUuid_numberGet the assigned unique id of the row in the data array
row._selected_booleanGet the selection status of the row
col.isSearchedbooleanGet the search status of the column
col.isSortedbooleanGet the sort status of the column
col.isVisiblebooleanGet the visibility status of the column
col.keystringGet the key of the column

Column Configuration

export type Columns = {
    [key: string]: {
        title: string,
        colClasses?: string,
        
        isVisibleByDefault?: boolean,
        isSearchableByDefault?: boolean,
        isSortableByDefault?: boolean,
        isSortedByDefault?: boolean,
        
        render?: (row: TExtended<T>, col: string, element: HTMLElement) => HTMLElement,
        defaultContent?: string,
    };
};

Column Configuration Extended

export type StrictColumn = {
    key: string,
    title: string,
    colClasses: string,
    
    isVisibleByDefault: boolean,
    isSearchableByDefault: boolean,
    isSortableByDefault: boolean,
    isSortedByDefault: boolean,
    
    isVisible: boolean,
    isSearched: boolean,
    isSorted: boolean,
    
    render: (row: TExtended<T>, col: string, meta: element) => HTMLElement,
    defaultContent: string,
};

Data Type Extended

export type TExtended = T & {
    _dataUuid_: number;
    _selected_: boolean;
};

Issues and Feature - Done

  • 0.0.1-g - Fixed the issue - on call to setRowsPerPage(), page nb is not automatically updated to be within the new page range.
  • 0.0.1-k - Exported the all types and interfaces to be used in the project.
  • 0.0.1-l - Add processed search query and chunks to the AfterSearch event callback.
  • 0.0.1-n - Fixed the issue - both search and normal view don't have own total rows count.

Upcoming Features

  • API data fetching support
  • Customizable row actions
  • Customizable row selection

Contributors

0.0.1-k

9 months ago

0.0.1-n

9 months ago

0.0.1-m

9 months ago

0.0.1-l

9 months ago

0.0.1-j

9 months ago

0.0.1-i

9 months ago

0.0.1-c

9 months ago

0.0.1-b

9 months ago

0.0.1-a

9 months ago

0.0.1-g

9 months ago

0.0.1-f

9 months ago

0.0.1-e

9 months ago

0.0.1-d

9 months ago

0.0.1-h

9 months ago

0.0.1

9 months ago