HTML Builder AJAX
The ajax() method configures how DataTables fetches data from the server.
Basic Usage
Using a Route
use Yajra\DataTables\Html\Builder; $html = $builder->ajax(route('users.data'));
Using Current URL
// Uses the current URL$html = $builder->ajax('');
AJAX Configuration
Array Configuration
use Yajra\DataTables\Html\Builder; $html = $builder->ajax([ 'url' => route('users.data'), 'type' => 'GET', 'data' => 'function(d) { d.key = "value"; }',]);
Available Options
| Option | Type | Description |
|---|---|---|
url |
string | URL to fetch data from |
type |
string | HTTP method (GET/POST) |
data |
string | JavaScript function for custom data |
dataSrc |
string | Property in JSON response containing data |
POST AJAX
use Yajra\DataTables\Html\Builder; $html = $builder->postAjax(route('users.data'));
Pipeline AJAX
use Yajra\DataTables\Html\Builder; $html = $builder->pipeline(route('users.data'), 5);
Minified AJAX
Minify the AJAX URL by removing unnecessary parameters:
$html = $builder->minifiedAjax(); // With custom URL$html = $builder->minifiedAjax(route('users.data')); // With additional data$html = $builder->minifiedAjax(route('users.data'), null, ['key' => 'value']); // With custom AJAX parameters$html = $builder->minifiedAjax(route('users.data'), null, [], ['cache' => true]);
AJAX with Form Data
Using Form Selector (GET)
Automatically include form data in the AJAX request:
$html = $builder->ajaxWithForm(route('users.data'), '#search-form');
Using Form Selector (POST)
$html = $builder->postAjaxWithForm(route('users.data'), '#search-form');
Get AJAX URL
Retrieve the configured AJAX URL:
$url = $builder->getAjaxUrl();
See Also
- HTML Builder - Main HTML Builder documentation
- HTML Builder Table - Table generation