secamedia/datatables-form-bundle

Provides Form Types and Entities representing the DataTables AJAX Request.

v1.3 2021-01-11 11:46 UTC

This package is auto-updated.

Last update: 2024-04-11 18:43:02 UTC


README

This is a Symfony Bundle to represent the DataTables Ajax Request Data into an entity via form types.

Installation via Composer

Install it via composer as a dependency:

$ php composer.phar require secamedia/datatables-form-bundle

Activation

Register the bundle in the kernel to activate the bundle.

    public function registerBundles()
    {
        $bundles = [
            ...
            new Sm\DatatablesFormBundle\SmDatatablesFormBundle(),
            ...
        ];
    }

How to use with DataTables ajax request

To recognize the request data you have to move the datatables data into the key datatables:

$('table').DataTable({
    'ajax': {
        'url': '/ajax-request.php',
        'data': function (data) {
            // Move DataTables data into own key
            for (var d in data) {
                if (!data.hasOwnProperty(d)) {
                    continue;
                }
                data['datatables[' + d + ']'] = data[d];
                delete data[d];
            }
        },
        'type': 'POST'
    }
});

In your controller you can then create a form and set the entity to retrieve the data as an object:

use Sm\DatatablesFormBundle\Entity\DataTablesForm;
use Sm\DatatablesFormBundle\Form\Type\DataTablesFormType;
...
    public function someAction(Request $request)
    {
        $data = new DataTablesForm();
        $form = $this->createForm(DataTablesFormType::class, $data);
        $form->handleRequest($request);
        // use $data
    }
...