{"id":19081500,"url":"https://github.com/intracto/datatables-backend","last_synced_at":"2025-04-30T07:44:23.924Z","repository":{"id":56992777,"uuid":"65451197","full_name":"Intracto/datatables-backend","owner":"Intracto","description":null,"archived":false,"fork":false,"pushed_at":"2023-03-16T13:24:19.000Z","size":26,"stargazers_count":4,"open_issues_count":0,"forks_count":4,"subscribers_count":29,"default_branch":"master","last_synced_at":"2025-04-13T02:37:48.183Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Intracto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-11T08:10:49.000Z","updated_at":"2023-02-03T08:27:30.000Z","dependencies_parsed_at":"2024-11-09T02:36:48.426Z","dependency_job_id":"46da1919-d440-4a24-9015-f355549104e4","html_url":"https://github.com/Intracto/datatables-backend","commit_stats":{"total_commits":16,"total_committers":7,"mean_commits":"2.2857142857142856","dds":0.75,"last_synced_commit":"0778793c8f1661ddb6085dfb04cd75626cf3e61a"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Intracto%2Fdatatables-backend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Intracto%2Fdatatables-backend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Intracto%2Fdatatables-backend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Intracto%2Fdatatables-backend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Intracto","download_url":"https://codeload.github.com/Intracto/datatables-backend/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251664528,"owners_count":21624144,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T02:36:35.383Z","updated_at":"2025-04-30T07:44:23.877Z","avatar_url":"https://github.com/Intracto.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Intracto DataTables library\n\nHandle AJAX requests for Datatables.net.\n\n## Install\n\n```\ncomposer require intracto/datatables-backend\n\nor\n\n\"intracto/datatables-backend\" : \"dev-master\"\n```\n\n\n### Columns\n \nContainer class to hold `Column` objects. This is used to get the field for sorting.\n\n### Column\n\nHold data about a column, can be used for the frontend to render `\u003cth\u003e` and let DataTables known which fields\nare sortable and searchable (via javascript).\n\n### DataProvider\n\nGet the data to show in the datatable. Requires `Parameters`, `DataTablesRepository`, `ColumnTransformer`.\n\n### Parameters\n\nContainer class to hold data from the datatables AJAX request.\n\n### DataTablesRepositoryInterface\n\nDefines query functions needed to fetch the data.\n\n### DataTablesRepositoryTrait\n\nAn implementation of `DataTablesRepositoryInterface` with general queries. Only available for Doctrine ODM for now.\n\n### ColumnTransformerInterface\n\nTransform data fetched from the `Repository` to format needed for the datatables. The order of the fields is important here.\n\n---\n\n## Example\n\n### Columns\n\n```\nclass AddressListColumns extends Columns\n{\n    public function __construct()\n    {\n        parent::__construct(\n            array(\n                // In order of the tables headers\n                new Column('city', 'city', true, true),\n                new Column('zip', 'zip', false, false),\n                new Column('street', 'street', false, false),\n                new Column('actions', 'actions', false, false),\n            )\n        );\n    }\n}\n```\n\n### Transformer\n\n```\nclass AddressListColumnTransformer implements ColumnTransformerInterface\n{\n    /**\n     * @var EngineInterface\n     */\n    private $renderEngine;\n\n    /**\n     * AddressListColumnTransformer constructor\n     *\n     * @param EngineInterface $renderEngine\n     */\n    public function __construct(EngineInterface $renderEngine)\n    {\n        $this-\u003erenderEngine = $renderEngine;\n    }\n\n    /**\n     * @param array $data\n     *\n     * @return array\n     */\n    public function transform(array $data)\n    {\n        $columns = array();\n\n        foreach ($data as $address) {\n            /**\n             * @var Address $address\n             */\n            $columns[] = array(\n                $address-\u003egetCity()\n                $address-\u003egetZip()\n                $address-\u003egetStreet()\n                $this-\u003erenderEngine-\u003erender('Address/_list.actions.html.twig', array('address' =\u003e $address)),\n            );\n        }\n\n        return $columns;\n    }\n}\n```\n\n### Repository\n\n```\nclass AddressRepository extends DocumentRepository implements DataTablesRepositoryInterface\n{\n    use DataTablesRepositoryTrait;\n}\n```\n\n### Controller\n\n```\npublic function listAction()\n{\n    $columns = new AddressListColumns();\n\n    return array(\n        'columns' =\u003e $columns,\n    );\n}\n\npublic function ajaxListAction(Request $request)\n{\n    $parameters = Parameters::fromParameterBag($request-\u003equery, new AddressListColumns());\n\n    $data = $this-\u003edataTablesDataProvider-\u003egetData(\n        $parameters,\n        $addressRepository,\n        $addressListColumnTransformer\n    );\n\n    return new JsonResponse($data);\n}\n```\n\n---\n\n### Frontend\n\n#### External JS\n\nInclude `ajax-datatables.js` after loading the official datatables.js plug-in\n\n#### Twig html\n\nMake sure the table has the \"ajaxdatatable\" class\nLoop all the columns in the table head\n\n```\n\u003ctable class=\"ajaxdatatable\"\u003e\n    \u003cthead\u003e\n        \u003ctr\u003e\n            {% for column in columns %}\n                \u003cth\u003e{{ (\"translatable.prefix.\" ~ column.name)|trans }}\u003c/th\u003e\n            {% endfor %}\n        \u003c/tr\u003e\n    \u003c/thead\u003e\n\u003c/table\u003e\n```\n\nDefault sorting needed? Add the class `default_sort` and `asc`|`desc` to the `\u003cth\u003e` to the correct column\n\n```\n\u003cth {% if column.name == 'email' %} class=\"default_sort desc\"{% endif %}\u003e\n    ...\n\u003c/th\u003e\n```\n\n\n#### Twig JS\n\nOn the twig page where the ajax datatable must be loaded, place following script block\n\nThe `filters` are optional, here you can pass searchable/filterable fields\n\n```\n\u003cscript\u003e\n    $(document).ready(function(){\n\n        {# define the ajax call path #}\n        var ajaxCallPath = \"{{ path(\"path_to_your_ajax_call\") }}\";\n\n        {# define which columns are orderable #}\n        var sortable = [\n            {% for column in columns %}\n                {%- if column.orderable -%}\n                    {{ 'null' }}\n                {%- else -%}\n                    {{ '{ \"orderable\": false }'}}\n                {%- endif -%}\n                {{ ',' }}\n            {% endfor %}\n        ];\n\n        {# page load filter fields, do not add .val() since we need the reference #}\n        var filters = {\n            'name': $(\"#js-filter-naam\"),\n            'address.city': $(\"#js-filter-city\")\n        };\n\n        {# on page load, init the datatable #}\n        ajaxDatatable(ajaxCallPath, sortable, filters, stateSaveAffix);\n\n        {# on submit, change, whenever you want #}\n        $(document).on(\"click\", \"#js-filter-submit\", function(){\n            {# no need to pass the parameters again #}\n            ajaxDatatable();\n        });\n    });\n\u003c/script\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintracto%2Fdatatables-backend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fintracto%2Fdatatables-backend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintracto%2Fdatatables-backend/lists"}