PHP code example of cakedc / cakephp-datatables

1. Go to this page and download the library: Download cakedc/cakephp-datatables library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cakedc / cakephp-datatables example snippets


<?= $this->Datatable->setFields($article->getVisible()) 

<?= $this->Datatable->setFields(['id', 'title', 'user_id', 'user.name']); 

<?= $this->Datatable->setFields([
    [
        'name' => 'user_id',
        'render' => '
            function(data, type) {
                return Math.floor(Math.random() * 1000);
            }
        '
    ]
]); 

<?= $this->Datatable->setFields([
    [
        'name' => 'title',
        'links' => [
            // Will produce a dynamic link with object data, i.e.
            // <a href="/articles/view/' + obj.id + '">hard coded</a>
            ['url' => ['action' => 'view', 'extra' => ("/' + obj.id + '")], 'label' => 'hard coded'],

            // Will produce a fixed link with a hard coded label, i.e.
            // <a href="/articles/view/d">hard coded</a>
            ['url' => ['action' => 'view', 'd'], 'label' => 'hard coded'],

            // Will produce a fixed link with a dynamic label, i.e.
            // <a href="/articles/edit">' + obj.user_id + '</a>
            ['url' => ['action' => 'edit'], 'value' => 'obj.user_id'],

            // Will produce a fixed link without an external URL in the href attribute, i.e.
            // <a href="#">' + obj.user_id + '</a>
            ['url' => '#', 'value' => 'obj.user_id'],
        ]
    ],
]); 

<?= $this->Datatable->setFields([
    [
        'name' => 'title',
        'links' => [
            [
                'url' => ['action' => 'view', 'extra' => ("/' + obj.id + '")],
                'label' => 'hard coded',
                'disable' => 'function (value) {
                    return value === "N/A"
                }',
            ],
            [
                'url' => ['action' => 'view', 'd'],
                'label' => 'hard coded'
                'disable' => 'function (value, obj) {
                    return obj.status === "inactive"
                }',
            ],
        ]
    ],
]); 

<?= $this->Datatable->setFields([
    [
        'name' => 'action',
        'links' => [
            [
                'url' => ['action' => 'delete', 'extra' => ("/' + obj.id + '")],
                'label' => 'delete record',
                'type' => \CakeDC\Datatables\Datatables::LINK_TYPE_POST,
                'confirm' => __('Are you sure you want to delete this item?'),
            ],
        ]
    ],
]); 

<?= $this->Datatable->setFields([
    [
        'name' => 'action',
        'links' => [
            [
                'url' => ['action' => 'delete', 'extra' => ("/' + obj.id + '")],
                'label' => 'delete record',
                'type' => \CakeDC\Datatables\Datatables::LINK_TYPE_POST,
                'confirm' => __('Are you sure you want to delete this item?'),
                'confirmCondition' => 'function (message){ return window.confirm(message); }',
            ],
        ]
    ],
]); 

$this->Datatable->setFields(
    [
        'id',
        [
            'name' => 'title',
            'links' => [
                ['url' => ['action' => 'view', 'extra' => ("/' + obj.id + '")], 'label' => 'hard coded'],
                ['url' => ['action' => 'view', 'd'], 'label' => 'hard coded'],
                ['url' => ['action' => 'edit'], 'value' => 'obj.user_id'],
                ['url' => '#', 'value' => 'obj.user_id'],
            ]
        ],
        [
            'name' => 'user_id',
            'render' => '
                function(data, type) {
                    return Math.floor(Math.random() * 1000);
                }
            '
        ],
        'user.name'
    ]
);

$this->Datatable->getDatatableScript("table-articles");

'searchInput' => [
            'type' => '{{any-type}}',
            'options' => [
                    ['id' => 1, 'name' => 'one'],
            ],
        ],

'searchInput' => [
    type => 'select',
    'options' => [
        ['id' => 1, 'name' => 'one'],
        ['id' => 2, 'name' => 'two'],
        ....
    ]
],

'searchInput' => [
    type => 'multiple',
    'options' => [
        ['id' => 1, 'name' => 'one'],
        ['id' => 2, 'name' => 'two'],
        ....
    ]
],

'searchInput' => [
    type => 'date',
    'options' => [],
]


<?= $this->Datatable->setFields([
    [
        'name' => 'user_id',
        'searchInput' => [
            'type' => 'select',
            'options' => [
                ['id' => 1, 'name' => 'one'],
                ['id' => 2, 'name' => 'two'],
                ....
            ]
        ],
        'render' => '
            function(data, type) {
                return Math.floor(Math.random() * 1000);
            }
        '
    ]
]); 
 'searchable' => false, 

 $this->Datatable->setConfigKey('delay', 2000, true); 

<?= $this->Datatable->getDatatableScript("table-articles") 

<?= $this->Datatable->getTableHeaders($article->getVisible(), true) 

<?= $this->Datatable->reset() 

<?= $this->Datatable->getInstance()->setConfig('ajaxUrl', ['controller' => 'Pages', 'action' => 'list']); 


$this->Datatable->getInstance()->setConfig('ajaxType', 'POST');
$this->Datatable->getInstance()->setConfig('csrfToken', $this->getRequest()->getAttribute("csrfToken"));

public function initialize(): void
{
    parent::initialize();

    ...

    if ($this->components()->has('Security')) {
        $this->Security->setConfig('unlockedActions', ['list']);
    }

    ...

}