angular-tz-datatable-core v0.0.1-n
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.
[
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.
API | Return Type | Usage |
---|---|---|
dt.initCols(ColumnsConfig) | void | Initializes table columns |
dt.initData(Array<T>) | void | Initializes 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) | void | Go to a specific page |
- | - | - |
dt.setPageNb(number) | void | Set the current page number |
dt.getPageNb() | number | - |
dt.getPageSize() | number | Current 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) | void | Toggle row selection |
dt.toggleAllRowsSelectionInPage() | void | Toggle all rows selection in the current page |
dt.selectAllRowsInPage() | void | Select all rows in the current page |
dt.unselectAllRowsInPage() | void | Unselect all rows in the current page |
dt.getSelectedRowCountOnPage() | number | Get the number of selected rows in current page |
dt.getUnselectedRowCountOnPage() | number | Get the number of unselected rows in current page |
dt.getSelectedRowCountBeyondPage() | number | Get the number of selected rows beyond current page |
dt.getSelectedRowCountInTotal() | number | Get the number of selected rows beyond current page |
dt.getUnselectedRowCountInTotal() | number | Get the number of unselected rows in total |
dt.getSelectedRowCountInTotal() | number | Get the number of selected rows in total |
- | - | - |
dt.getSortingKey() | string | Get the current sorting column |
dt.setSortingKey(string) | void | Set the current sorting column |
- | - | - |
dt.toggleColVisibility(string) | void | Toggle column visibility |
dt.resetColsVisibility() | void | Reset column visibility |
- | - | - |
dt.toggleSearchCol(string) | void | Toggle 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);
});
Event | Callback 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 / Method | Return Type | Usage |
---|---|---|
row._dataUuid_ | number | Get the assigned unique id of the row in the data array |
row._selected_ | boolean | Get the selection status of the row |
col.isSearched | boolean | Get the search status of the column |
col.isSorted | boolean | Get the sort status of the column |
col.isVisible | boolean | Get the visibility status of the column |
col.key | string | Get 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