Skip to main content

DataTable

Intro

DataTable is to lessen the load and complexity of the developer in the simplest form.

Prerequisits

  • CakePHP v4.0 with Asgard setup.
  • You can clone the repo from https://gitlab.com/ascendtis-boilerplates/asgard-v5

Basic

Let's say you want to create a DataTable for Admins Table. So let's take an example how you do it, check it out below.

create a file at templates/Asgard/Admins/index.php

Note: Our DataTable must resides in index.php so that it can easily accessible with /asgard/admins

templates/Asgard/Admins/index.php
  <data-table 
model="Admins"
:headers="[
{
text: 'Name',
field: 'name'
},
{
text: 'Username',
field: 'username'
},
{
text: 'RegisteredAt',
field: 'created_at',
dateFormat: 'Do MMM YYYY'
}
]"
>
</data-table>

The above code will render DataTable like this. Basic DataTable

Props

NameTypeRequiredDescription
modelstringYesModel name of a Table.
:headersarrayYesFields which you want to display in DataTable.
datakeystringNoIf you have multiple DataTable in same page, then you have to define key for it's uniqueness. default: datatable
:queryobjectNoIt is used to filter the data based on conditions.
:limitnumberNoNo. of records per page. default: 20
:has-checkboxbooleanNoEnable multiple records deletion.
base-urlstringNoIf you want to use DataTable other than /asgard then you can specify it here. default: /asgard/
:is-expandbooleanNoEach row can be expandable. It will helpful if you want to show some fields only when user expands row. default: false
:single-expandbooleanNoIf it's true when you expand a row and if you have other row open it will be closed automatically. default: true
:header-actionsarrayNoAction buttons can be added on top of DataTable (in Header) eg. Add Admin

:headers

Headers are the fields which you wanted to display on DataTable. Look at the example below.

headers
  [
{
text: 'Name',
displayAs: ({ first_name, last_name }) => first_name + ' ' + last_name
},
{
text: 'Username',
field: 'username'
},
{
text: 'RegisteredAt',
field: 'created_at',
dateFormat: 'Do MMM YYYY'
}
]
NameTypeRequiredDescription
textstring