{"id":13536373,"url":"https://github.com/NullRefExcep/yii2-datatables","last_synced_at":"2025-04-02T03:30:39.694Z","repository":{"id":24140081,"uuid":"27529371","full_name":"NullRefExcep/yii2-datatables","owner":"NullRefExcep","description":"Yii2 Widget for DataTables jQuery plug-in ","archived":false,"fork":false,"pushed_at":"2022-10-07T11:33:26.000Z","size":89,"stargazers_count":72,"open_issues_count":7,"forks_count":46,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-10-30T01:58:38.585Z","etag":null,"topics":["datatables","yii2-extension","yii2-widgets"],"latest_commit_sha":null,"homepage":"http://www.datatables.net/","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/NullRefExcep.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-04T07:48:22.000Z","updated_at":"2023-06-15T15:44:51.000Z","dependencies_parsed_at":"2022-07-06T18:35:02.668Z","dependency_job_id":null,"html_url":"https://github.com/NullRefExcep/yii2-datatables","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NullRefExcep%2Fyii2-datatables","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NullRefExcep%2Fyii2-datatables/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NullRefExcep%2Fyii2-datatables/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NullRefExcep%2Fyii2-datatables/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NullRefExcep","download_url":"https://codeload.github.com/NullRefExcep/yii2-datatables/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222795273,"owners_count":17038797,"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":["datatables","yii2-extension","yii2-widgets"],"created_at":"2024-08-01T09:00:38.003Z","updated_at":"2024-11-03T01:31:18.883Z","avatar_url":"https://github.com/NullRefExcep.png","language":"PHP","funding_links":[],"categories":["PHP","Other Yii2 packages:"],"sub_categories":[],"readme":"Yii2 DataTables\n===============\n\nYii2 Widget for [DataTables](https://github.com/DataTables/DataTables) plug-in for jQuery\n\n## Installation\n\nThe preferred way to install this extension is through composer.\n\nEither run\n\n```\ncomposer require nullref/yii2-datatables\n```\nor add\n```\n\"nullref/yii2-datatables\": \"~1.0\"\n```\nto the require section of your `composer.json` file.\n\n## Basic Usage\n\n```php\n\u003c?= \\nullref\\datatable\\DataTable::widget([\n    'dataProvider' =\u003e $dataProvider,\n    'columns' =\u003e [\n        'id',\n        'name',\n        'email'\n    ],\n]) ?\u003e\n```\n\nFor backwards compatibility the old usage via `data` is still supported\n```php\n\u003c?= \\nullref\\datatable\\DataTable::widget([\n    'data' =\u003e $dataProvider-\u003egetModels(),\n    'columns' =\u003e [\n        'id',\n        'name',\n        'email'\n    ],\n]) ?\u003e\n```\n\n\n\n## DataTable options\nAlso you can use all [Datatables options](https://datatables.net/reference/option/)\n\nTo pass them as widget options:\n```php\n\u003c?= \\nullref\\datatable\\DataTable::widget([\n    'data' =\u003e $dataProvider-\u003egetModels(),\n    'scrollY' =\u003e '200px',\n    'scrollCollapse' =\u003e true,\n    'paging' =\u003e false,\n    'columns' =\u003e [\n        'name',\n        'email'\n    ],\n    'withColumnFilter' =\u003e true,\n]) ?\u003e\n```\n\n## Specifies header label and css class for cell\n\n```php\n    \u003c?= \\nullref\\datatable\\DataTable::widget([\n        'columns' =\u003e [\n            //other columns\n            [\n                'data' =\u003e 'active',\n                'title' =\u003e 'Is active',\n                'sClass' =\u003e 'active-cell-css-class',\n            ],\n        ],\n    ]) ?\u003e\n```\n\n## Specifies datatable id\n\n```php\n\u003c?= \\nullref\\datatable\\DataTable::widget([\n    'data' =\u003e $dataProvider-\u003egetModels(),\n    'id' =\u003e 'your-datatable-id'\n]) ?\u003e\n```\n\n## Add Links to row\n\n```php\n    \u003c?= \\nullref\\datatable\\DataTable::widget([\n        'columns' =\u003e [\n            //other columns\n            [\n                'class' =\u003e 'nullref\\datatable\\LinkColumn',\n                'url' =\u003e ['/model/delete'],\n                'linkOptions' =\u003e ['data-confirm' =\u003e 'Are you sure you want to delete this item?', 'data-method' =\u003e 'post'],\n                'label' =\u003e 'Delete',\n            ],\n        ],\n    ]) ?\u003e\n```\n\nProperties of `LinkColumn`: \n\n- `label` - text placed in `a` tag;\n- `title` - header title of column;\n- `url` - will be passed to `Url::to()`;\n- `linkOptions` - HTML options of the `a` tag;\n- `queryParams` - array of params added to `url`, `['id']` by default;\n- `render` - custom render js function. E.g:\n```php\n//config ...\n    'columns' =\u003e [\n        //...\n        [\n            'class' =\u003e 'nullref\\datatable\\LinkColumn',\n            'queryParams' =\u003e ['some_id'],\n            'render' =\u003e new JsExpression('function render(data, type, row, meta ){\n                return \"\u003ca href=\\\"/custom/url/\"+row[\"some_id\"]+\"\\\"\u003eView\u003c/a\u003e\"\n            }'),\n        ],\n    ],\n//...\n```\n\nYou should pass fields that are using at render function to `queryParams` property\n\n## Column filtering\n\nYou can add column filtering functionality by setting option `withColumnFilter` to `true` :\n\n- By default it generates a text field as filter input. \n- It can be replaced by a combo box using `filter` parameter when defining column. It should be a associative array \n  where key is used as filter (value sent to server) and value for cell rendering\n- It can be avoided by setting `filter` to false\n\n```php\n    \u003c?= \\nullref\\datatable\\DataTable::widget([\n        'columns' =\u003e [\n            'id',\n            //...\n            [\n                'data' =\u003e 'active',\n                'title' =\u003e \\Yii::t('app', 'Is active'),\n                'filter' =\u003e ['true' =\u003e 'Yes', 'false' =\u003e 'No'],\n            ],\n            [\n                'data' =\u003e 'last_connection',\n                'filter' =\u003e false,\n            ],\n        ],\n    ]) ?\u003e\n//...\n```\n\nIn this example above, filter for `active` field sent to server will contains `'true'` or `'false'` but the cell content \nwill be `'Yes'` or `'No'` and the filter will be rendered as a combo box.\n\nNo filter will be generated for `last_connection` attrribute.\n\n## Advanced column definition\n\nCell rendering or filter can be customized using `\\nullref\\datatable\\DataTableColumn` class.\n\n```php\n    \u003c?= \\nullref\\datatable\\DataTable::widget([\n        'columns' =\u003e [\n            //other columns\n            [\n                'class' =\u003e 'nullref\\datatable\\DataTableColumn', // can be omitted\n                'data' =\u003e 'active',\n                'renderFiler' =\u003e new \\yii\\web\\JsExpression('function() { ' .\n                    'return jQuery(\\'\u003cinput type=\"checkbox\" value=\"true\"/\u003e Active only\\'); ' .\n                '}'),\n                'render' =\u003e new \\yii\\web\\JsExpression('function(data, type, row, meta) { ' .\n                    'return jQuery(\\'\u003cinput type=\"checkbox\" value=\"true\" disabled/\u003e\\')' .\n                    '    .prop(\\'checked\\', data == \\'true\\'); ' .\n                    '}'),\n            ],\n        ],\n    ]) ?\u003e\n```\n\n## Styling \n\n`DataTables` supports several styling solutions, including `Bootstrap`, `jQuery UI`, `Foundation`.\n\n```php\n'assetManager' =\u003e [\n    'bundles' =\u003e [\n        'nullref\\datatable\\assets\\DataTableAsset' =\u003e [\n            'styling' =\u003e \\nullref\\datatable\\assets\\DataTableAsset::STYLING_BOOTSTRAP,\n        ]\n    ],\n],\n```\n\n### Bootstrap\n\nBootstrap tables require the class 'table', so you'll need to add the 'table' class using `tableOptions` via the widget config.\n\n```php\n\u003c?= \\nullref\\datatable\\DataTable::widget([\n    'data' =\u003e $dataProvider-\u003egetModels(),\n    'tableOptions' =\u003e [\n        'class' =\u003e 'table',\n    ],\n    'columns' =\u003e [\n        'id',\n        'name',\n        'email',\n    ],\n]) ?\u003e\n```\n\n## Custom assets\nIt's possible to use custom styles and scripts:\n```php\n'nullref\\datatable\\assets\\DataTableAsset' =\u003e [\n    'sourcePath' =\u003e '@webroot/js/plugin/datatables/',\n    'js' =\u003e [\n        'jquery.dataTables-1.10-cust.js',\n        'DT_bootstrap.js',\n    ],\n    'css' =\u003e [\n        'data-table.css',\n    ],\n    'styling' =\u003e false,\n]\n```\n\n## Server-side processing\n\nTo enable server-side processing add `DataTableAction` to controller like this:\n\n```php\nclass SomeController extends Controller\n{\n    public function actions()\n    {\n        return [\n            'datatables' =\u003e [\n                'class' =\u003e 'nullref\\datatable\\DataTableAction',\n                'query' =\u003e Model::find(),\n            ],\n        ];\n    }\n}\n```\n\nSearching and ordering can be customized using closures\n```php\npublic function actions()\n{\n    return [\n         'datatables' =\u003e [\n             'class' =\u003e 'nullref\\datatable\\DataTableAction',\n             'query' =\u003e Model::find(),\n             'applyOrder' =\u003e function($query, $columns, $order) {\n                //custom ordering logic\n                $orderBy = [];\n                foreach ($order as $orderItem) {\n                    $orderBy[$columns[$orderItem['column']]['data']] = $orderItem['dir'] == 'asc' ? SORT_ASC : SORT_DESC;\n                }\n                return $query-\u003eorderBy($orderBy);\n             },\n             'applyFilter' =\u003e function($query, $columns, $search) {\n                //custom search logic\n                $modelClass = $query-\u003emodelClass;\n                $schema = $modelClass::getTableSchema()-\u003ecolumns;\n                foreach ($columns as $column) {\n                    if ($column['searchable'] == 'true' \u0026\u0026 array_key_exists($column['data'], $schema) !== false) {\n                        $value = empty($search['value']) ? $column['search']['value'] : $search['value'];\n                        $query-\u003eorFilterWhere(['like', $column['data'], $value]);\n                    }\n                }\n                return $query;\n             },\n         ],\n    ];\n}\n```\n\nIf you need to get some relation data you can call `join` or similar methods from `$query` in `applyFilter` closure.\n\nYou may also specify a closure for `query` in `DataTableAction` config if you need complex query like in the following code:\n```php\n/** ... */\n'query' =\u003e function() {\n    $calculatedValue = calculate_value_for_query();\n    \n    return Model::find()-\u003ewhere(['calculated_value' =\u003e $calculatedValue]);\n},\n/** ... */\n```\n\nAnd add options to widget: \n\n```php\n\u003c?= \\nullref\\datatable\\DataTable::widget([\n    /** ... */\n    'serverSide' =\u003e true,\n    'ajax' =\u003e '/site/datatables',\n]) ?\u003e\n```\n\n\n## Extra columns\n\nIf need to use some custom fields from your model at your render function at column you could pass `extraColumns` param.\n\nIt available at DataTable widget, column and server side action definition:\n\n```php\n\u003c?= \\nullref\\datatable\\DataTable::widget([\n    /** ... */\n    'data' =\u003e $dataProvider-\u003egetModels(),\n    'extraColumns' =\u003e ['customPrice'],\n    'columns' =\u003e [\n        [\n            'title' =\u003e 'Custom column',\n            'extraColumns' =\u003e ['customField'],\n            'render' =\u003e new JsExpression($customColumnRender),\n        ],\n    ],\n]) ?\u003e\n```\n\n```php\nclass SomeController extends Controller\n{\n    public function actions()\n    {\n        return [\n            'datatables' =\u003e [\n                'class' =\u003e 'nullref\\datatable\\DataTableAction',\n                'query' =\u003e Model::find(),\n                'extraColumns' =\u003e ['customPrice'],\n            ],\n        ];\n    }\n}\n```\n\n```php\n\u003c?= \\nullref\\datatable\\DataTable::widget([\n    /** ... */\n    'extraColumns' =\u003e ['customPrice'],\n]) ?\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNullRefExcep%2Fyii2-datatables","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNullRefExcep%2Fyii2-datatables","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNullRefExcep%2Fyii2-datatables/lists"}