### Install Assets
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Run the assets:install command to deploy the bundle's Javascript files to your public folder. This step is optional if using Assetic or Webpack.
```bash
bin/console assets:install
```
--------------------------------
### Install DataTables Bundle via Composer
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Install the bundle using Composer. Ensure you are using Symfony 4.1 or later.
```bash
composer require omines/datatables-bundle
```
--------------------------------
### Basic ORM Adapter Setup
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Use this snippet to set up a basic ORMAdapter for a DataTable. It automatically handles entity selection, joins, and global search if not otherwise specified.
```php
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
$table = $this->createDataTable()
->add('firstName', TextColumn::class)
->add('lastName', TextColumn::class)
->add('company', TextColumn::class, ['field' => 'company.name'])
->createAdapter(ORMAdapter::class, [
'entity' => Employee::class,
]);
```
--------------------------------
### Configure MongoDB Adapter for Collections
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Use the MongoDBAdapter to connect to MongoDB collections as a data source. Ensure the 'mongodb/mongodb' package is installed.
```php
use Omines\DataTablesBundle\Adapter\MongoDB\MongoDBAdapter;
$table = $this->createDataTable()
->add('name', TextColumn::class)
->add('company', TextColumn::class)
->createAdapter(MongoDBAdapter::class, [
'collection' => 'myCollection',
]);
```
--------------------------------
### Configure Elastica Adapter for Elasticsearch
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Use the ElasticaAdapter to connect to Elasticsearch indexes as a data source. Ensure the 'ruflin/elastica' package is installed.
```php
use Omines\DataTablesBundle\Adapter\Elasticsearch\ElasticaAdapter;
$table = $this->createDataTable()
->setName('log')
->add('timestamp', DateTimeColumn::class, ['field' => '@timestamp', 'format' => 'Y-m-d H:i:s', 'orderable' => true])
->add('level', MapColumn::class, [
'default' => 'Unknown',
'map' => ['Emergency', 'Alert', 'Critical', 'Error', 'Warning', 'Notice', 'Info', 'Debug'],
])
->add('message', TextColumn::class, ['globalSearchable' => true])
->createAdapter(ElasticaAdapter::class, [
'client' => ['host' => 'elasticsearch'],
'index' => 'logstash-*',
]);
```
--------------------------------
### Create a Basic DataTable in a Controller
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Prepare a functional DataTables instance in your controller using DataTableFactory. This example uses an ArrayAdapter with static data.
```php
use Omines\DataTablesBundle\Adapter\ArrayAdapter;
use Omines\DataTablesBundle\Column\TextColumn;
use Omines\DataTablesBundle\DataTableFactory;
class MyController extends Controller
{
public function showAction(Request $request, DataTableFactory $dataTableFactory)
{
$table = $dataTableFactory->create()
->add('firstName', TextColumn::class)
->add('lastName', TextColumn::class)
->createAdapter(ArrayAdapter::class, [
['firstName' => 'Donald', 'lastName' => 'Trump'],
['firstName' => 'Barack', 'lastName' => 'Obama'],
])
->handleRequest($request);
if ($table->isCallback()) {
return $table->getResponse();
}
return $this->render('list.html.twig', ['datatable' => $table]);
}
}
```
--------------------------------
### Include DataTables CSS and JS
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Include the DataTables CSS in the
section and the Javascript before the closing tag. This example includes jQuery 3.
```html
```
--------------------------------
### Deploy Documentation
Source: https://github.com/omines/datatables-bundle/blob/master/docs/README.md
Maintainers with push privileges on the 'gh-pages' branch can update the documentation by running this script.
```shell
./deploy.sh
```
--------------------------------
### Initialize DataTables Frontend
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Include this HTML in your page to display a DataTables table. The script initializes the table using the provided settings.
```html
Loading...
```
--------------------------------
### Initialize DataTables with Export Button
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
On the client-side, use initDataTables to initialize DataTables and configure export buttons using the Buttons extension.
```javascript
$(function() {
$('#table').initDataTables({{ datatable_settings(datatable) }}, {
dom:'<"html5buttons"B>lTfgitp',
buttons: [
{
text: 'Export to Excel',
action: $.fn.initDataTables.exportBtnAction('excel', {{ datatable_settings(datatable) }})
}
]
});
});
```
--------------------------------
### Configure Common Column Options
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Set common options for columns such as labels, rendering, visibility, and searchability. Supports string or callable renderers.
```php
# Some example columns
$table
->add('firstName', TextColumn::class, ['label' => 'customer.name', 'className' => 'bold'])
->add('lastName', TextColumn::class, ['render' => '%s', 'raw' => true])
->add('email', TextColumn::class, ['render' => function($value, $context) {
return sprintf('%s', $value, $value);
}]);
```
--------------------------------
### Initialize DataTables with Custom Settings and Events
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Initialize a DataTable instance using JavaScript with server-side settings and custom browser-side configurations. The function returns a Promise that resolves with the DataTables instance, allowing further API interactions.
```javascript
$('#table1').initDataTables({{ datatable_settings(datatable1) }}, {
searching: true,
dom:'<"html5buttons"B>lTfgitp',
buttons: [
'copy',
{ extend: 'pdf', title: 'domains'},
{ extend: 'print' }
]
}).then(function(dt) {
// dt contains the initialized instance of DataTables
dt.on('draw', function() {
alert('Redrawing table');
})
});
```
--------------------------------
### Customize Server-Side Export Filename
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Listen to the PRE_RESPONSE event to customize the filename and disposition of the exported file on the server.
```php
$table = $dataTableFactory->create()
->add(...)
->addEventListener(DataTableExporterEvents::PRE_RESPONSE, function (DataTableExporterResponseEvent $e) {
$response = $e->getResponse();
$response->deleteFileAfterSend(true);
$ext = $response->getFile()->getExtension();
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'custom_filename.' . $ext);
})
->handleRequest($request);
```
--------------------------------
### Create DataTable from Type Class
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Instantiate a DataTable using a fully qualified class name or a registered service. This is useful for creating reusable table configurations.
```php
$table = $this->createDataTableFromType(PresidentsTableType::class)
->handleRequest($request);
```
--------------------------------
### Configure BoolColumn with Custom Values
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Use BoolColumn to render boolean values, allowing custom strings for true, false, and null states. Useful for displaying user preferences.
```php
$table->add('wantsNewsletter', BoolColumn::class, [
'trueValue' => 'yes',
'falseValue' => 'no',
'nullValue' => 'unknown',
]);
```
--------------------------------
### ORM Adapter Event Listener for Caching
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Attach an event listener to the PRE_QUERY event to configure query and result caching for the ORMAdapter before the query is executed.
```php
$table->createAdapter(ORMAdapter::class, [
'entity' => Employee::class,
'query' => function (QueryBuilder $builder) {
$builder
->select('e')
->addSelect('c')
->from(Employee::class, 'e')
->leftJoin('e.company', 'c')
;
},
]);
$table->addEventListener(ORMAdapterEvents::PRE_QUERY, function(ORMAdapterQueryEvent $event) {
$event->getQuery()->useResultCache(true)->useQueryCache(true);
});
```
--------------------------------
### Enable Twig StringLoaderExtension
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Configuration to enable the Twig\Extension\StringLoaderExtension in your Symfony application's services.
```yaml
services:
Twig\Extension\StringLoaderExtension:
tags: [twig.extension]
```
--------------------------------
### Register DataTables Bundle in Kernel
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Register the bundle in your AppKernel.php file. Place it after Symfony's core bundles but before your application bundles.
```php
public function registerBundles()
{
// After Symfony's own bundles
new \Omines\DataTablesBundle\DataTablesBundle(),
// Before your application bundles
}
```
--------------------------------
### Configure TextColumn with Field Mapping
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Use TextColumn for displaying data as plain text. The 'field' option maps the data source field to the column.
```php
$table->add('customerName', TextColumn::class, ['field' => 'customer.name']);
```
--------------------------------
### Add MapColumn for value transformation
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Use MapColumn to transform discrete values into display counterparts. Define a 'map' for transformations and an optional 'default' value.
```php
$table->add('gender', MapColumn::class, [
'default' => 'not provided',
'map' => [
'f' => 'Female',
'm' => 'Male',
],
]);
```
--------------------------------
### Listen to POST_RESPONSE Event
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Hook into the DataTable lifecycle after the response is created but before it is returned to the client. Useful for logging or post-processing tasks.
```php
$table->addEventListener(DataTableEvents::POST_RESPONSE, function (DataTablePreResponseEvent $event) use ($logger) {
$logger->info('Table rendered', ['table' => $event->getTable()]);
});
```
--------------------------------
### Listen to PRE_RESPONSE Event
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Hook into the DataTable lifecycle just before the response is created. Use this to modify the table, such as adding or removing columns, especially in server-side exporting contexts.
```php
$table->addEventListener(DataTableEvents::PRE_RESPONSE, function (DataTablePreResponseEvent $event) {
$table = $event->getTable();
$table
->add('extraColumn', TextColumn::class)
->remove('obsoleteColumn')
;
});
```
--------------------------------
### Customizing ORM Adapter Queries
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Customize the SQL query generated by the ORMAdapter by providing a custom query builder callable. This allows for specific SELECT, JOIN, and FROM clauses.
```php
$table->createAdapter(ORMAdapter::class, [
'entity' => Employee::class,
'query' => function (QueryBuilder $builder) {
$builder
->select('e')
->addSelect('c')
->from(Employee::class, 'e')
->leftJoin('e.company', 'c')
;
},
]);
```
--------------------------------
### Add DateTimeColumn with custom format
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Use DateTimeColumn to render \DateTimeInterface objects. Specify a custom format using the 'format' option.
```php
$table->add('registrationDate', DateTimeColumn::class, ['format' => 'd-m-Y']);
```
--------------------------------
### Customizing ORM Adapter Criteria
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Define custom criteria processors for the ORMAdapter to filter or modify query conditions. Remember to include SearchCriteriaProvider if automatic searching and sorting are still desired.
```php
$table->createAdapter(ORMAdapter::class, [
'entity' => Employee::class,
'criteria' => [
function (QueryBuilder $builder) {
$builder->andWhere($builder->expr()->like('c.name', ':test'))->setParameter('test', '%ny 2%');
},
new SearchCriteriaProvider(),
],
]);
```
--------------------------------
### DataTables Bundle Configuration
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Configure global settings for the DataTables bundle in your Symfony config file. These settings provide a uniform look and feel across all tables.
```yaml
datatables:
# Load i18n data from DataTables CDN or locally
language_from_cdn: true
# Default HTTP method to be used for callbacks
method: POST # One of "GET"; "POST"
# Default options to load into DataTables
options:
option: value
# Where to persist the current table state automatically
persist_state: fragment # One of "none"; "query"; "fragment"; "local"; "session"
# Default service used to render templates, built-in TwigRenderer uses global Twig environment
renderer: Omines\DataTablesBundle\Twig\TwigRenderer
# Default template to be used for DataTables HTML
template: '@DataTables/datatable_html.html.twig'
# Default parameters to be passed to the template
template_parameters:
# Default class attribute to apply to the root table elements
className: 'table table-bordered'
# If and where to enable the DataTables Filter module
columnFilter: null # One of "thead"; "tfoot"; "both"; null
# Default translation domain to be used
translation_domain: messages
```
--------------------------------
### Add TwigColumn with a template
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Use TwigColumn to render cells using a specified Twig template. The template has access to 'value', 'row', and 'column' context.
```php
$table->add('buttons', TwigColumn::class, [
'className' => 'buttons',
'template' => 'tables/buttonbar.html.twig',
])
```
--------------------------------
### Add TwigStringColumn with inline template
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/index.html.md
Use TwigStringColumn to render cells using an inline Twig template string. Requires Twig\Extension\StringLoaderExtension to be enabled.
```php
$table->add('link', TwigStringColumn::class, [
'template' => '{{ row.firstName }} {{ row.lastName }}',
])
```
--------------------------------
### Kittn API Error Codes
Source: https://github.com/omines/datatables-bundle/blob/master/docs/source/includes/_errors.md
The Kittn API utilizes standard HTTP status codes to indicate the success or failure of a request. This section outlines the specific meanings of common error codes returned by the API.
```APIDOC
## Kittn API Error Codes
### Description
The Kittn API uses the following error codes to communicate the outcome of requests.
### Error Codes
Error Code | Meaning
---------- | -------
400 | Bad Request -- Your request is invalid.
401 | Unauthorized -- Your API key is wrong.
403 | Forbidden -- The kitten requested is hidden for administrators only.
404 | Not Found -- The specified kitten could not be found.
405 | Method Not Allowed -- You tried to access a kitten with an invalid method.
406 | Not Acceptable -- You requested a format that isn't json.
410 | Gone -- The kitten requested has been removed from our servers.
418 | I'm a teapot.
429 | Too Many Requests -- You're requesting too many kittens! Slow down!
500 | Internal Server Error -- We had a problem with our server. Try again later.
503 | Service Unavailable -- We're temporarily offline for maintenance. Please try again later.
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.