PHP code example of m-comscience / yii2-widget-datatables
1. Go to this page and download the library: Download m-comscience/yii2-widget-datatables library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
m-comscience / yii2-widget-datatables example snippets
use mcomscience\datatables\DataTables;
<?=
DataTables::widget([
'options' => [
'id' => 'example',
],
'clientOptions' => [
"deferRender" => true,
"responsive" => true,
"autoWidth" => false,
"lengthMenu" => [[10, 25, 50, -1], [10, 25, 50, "All"]],
"drawCallback" => new \yii\web\JsExpression("function ( settings ) {
var api = this.api();
// Output the data for the visible rows to the browser's console
console.log( api.rows( {page:'current'} ).data() );
// Row grouping
var rows = api.rows( {page:'current'} ).nodes();
var last=null;
api.column(groupColumn, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {
$(rows).eq( i ).before(
'<tr class="group"><td colspan="5">'+group+'</td></tr>'
);
last = group;
}
} );
})"),
"footerCallback" => new \yii\web\JsExpression("
function( tfoot, data, start, end, display ) {
var api = this.api();
// Remove the formatting to get integer data for summation
var intVal = function ( i ) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
total = api
.column( 4 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total over this page
pageTotal = api
.column( 4, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 4 ).footer() ).html(
'$'+pageTotal +' ( $'+ total +' total)'
);
}
")
],
]);
use mcomscience\datatables\DataTables;
<?=
DataTables::widget([
'options' => [
'id' => 'example',
],
'extensions' => [
'responsive', // Dynamically show and hide columns based on the browser size.
'auto-fill', // Excel-like click and drag copying and filling of data.
'buttons', // A common framework for user interaction buttons.
'col-reorder', // Click-and-drag column reordering.
'fixed-columns' // Fix one or more columns to the left or right of a scrolling table.
'fixed-header', // Sticky header and / or footer for the table.
'key-table', // Keyboard navigation of cells in a table, just like a spreadsheet.
'row-group', // Show similar data grouped together by a custom data point.
'row-reorder', // Click-and-drag reordering of rows.
'scroller', // Virtual rendering of a scrolling table for large data sets.
'select', // Adds row, column and cell selection abilities to a table.
],
'clientOptions' => [
"ajax" => [
"url" => Url::base(true) . "/settings/data-source",
"type" => "GET",
"complete" => new JsExpression('function(jqXHR, textStatus) {
var table = $(\'#example\').DataTable();
$(table.buttons(0)[0].node).button(\'reset\');
}'),
],
"deferRender" => true,
"responsive" => true,
"autoWidth" => false,
"lengthMenu" => [[10, 25, 50, -1], [10, 25, 50, "All"]],
"autoFill" => true,
"colReorder" => true,
"buttons" => [
[
"text" => 'Reload',
"action" => new JsExpression('function ( e, dt, node, config ) {
$(node).button(\'loading\');
dt.ajax.reload();
}'),
"init" => new JsExpression('function ( dt, node, config ) {
var that = this;
$(node).removeClass("dt-button").addClass("btn btn-sm btn-success"); // custom bootstrap button
}'),
],
],
],
]);