Step 1 view set helper
use yii\helpers\Html;
use yii\helpers\Url;
Step 2 Table design in view
<table id="dtofficer" class="table table-striped table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th>Sr. No.</th>
<th> Employee Name</th>
<th> Employee Salary</th>
<th> Employee Age</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
</table>
Step 3 Call Script in view
<?php
$this->registerCssFile(Yii::$app->urlManager->baseUrl . '/css/dk/dataTables.bootstrap.css');
$this->registerJsFile(Yii::$app->urlManager->baseUrl . '/css/dk/jquery.dataTables.min.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJsFile(Yii::$app->urlManager->baseUrl . '/css/dk/dataTables.bootstrap.min.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJsFile(Yii::$app->urlManager->baseUrl . '/css/dk/datatables.responsive.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJsFile(Yii::$app->urlManager->baseUrl . '/css/dk/fnReloadAjax.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$urlOne = Url::to(['site/officerdata']);
$script = <<< JS
$(document).ready(function() {
var dataTable = $('#dtofficer').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
url: "$urlOne",
type: "post",
error: function()
{
alert('error');
}
}
});
$('#dtofficer tbody').on( 'click', '.selrow', function () {
$("#fmfirereport-fire_detail_id").val($(this).text());
});
});
JS;
$this->registerJs($script);
?>
use yii\helpers\Html;
use yii\helpers\Url;
Step 2 Table design in view
<table id="dtofficer" class="table table-striped table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th>Sr. No.</th>
<th> Employee Name</th>
<th> Employee Salary</th>
<th> Employee Age</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
</table>
Step 3 Call Script in view
<?php
$this->registerCssFile(Yii::$app->urlManager->baseUrl . '/css/dk/dataTables.bootstrap.css');
$this->registerJsFile(Yii::$app->urlManager->baseUrl . '/css/dk/jquery.dataTables.min.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJsFile(Yii::$app->urlManager->baseUrl . '/css/dk/dataTables.bootstrap.min.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJsFile(Yii::$app->urlManager->baseUrl . '/css/dk/datatables.responsive.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJsFile(Yii::$app->urlManager->baseUrl . '/css/dk/fnReloadAjax.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$urlOne = Url::to(['site/officerdata']);
$script = <<< JS
$(document).ready(function() {
var dataTable = $('#dtofficer').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
url: "$urlOne",
type: "post",
error: function()
{
alert('error');
}
}
});
$('#dtofficer tbody').on( 'click', '.selrow', function () {
$("#fmfirereport-fire_detail_id").val($(this).text());
});
});
JS;
$this->registerJs($script);
?>
Step -4 Controller
public function actionOfficerdata() {
$requestData = $_REQUEST;
$columns = array(
0 => 'id',
1 => 'employee_name',
2 => 'employee_salary',
3 => 'employee_age'
);
$sql = "SELECT * from employee where 1=1";
$data = Yii::$app->db->createCommand($sql)->queryAll();
$totalData = count($data);
$totalFiltered = $totalData;
// $sql.="WHERE 1=1";
if (!empty($requestData['search']['value']))
{
$sql.=" AND ( employee_name LIKE '" . $requestData['search']['value'] . "%' ";
$sql.=" OR employee_salary LIKE '" . $requestData['search']['value'] . "%'";
$sql.=" OR employee_age LIKE '" . $requestData['search']['value'] . "%')";
}
$data = Yii::$app->db->createCommand($sql)->queryAll();
$totalFiltered = count($data);
$sql.=" ORDER BY " . $columns[$requestData['order'][0]['column']] . " " . $requestData['order'][0]['dir'] . " LIMIT " . $requestData['start'] . " ," .
$requestData['length'] . " ";
$result = Yii::$app->db->createCommand($sql)->queryAll();
$data = array();
$i=1;
foreach ($result as $key => $row)
{
$nestedData = array();
$url = Url::to(['employee/update', 'id' => $row['id']]);
$nestedData[] = $i;
$nestedData[] = $row["employee_name"];
$nestedData[] = $row["employee_salary"];
$nestedData[] = $row["employee_age"];
$nestedData[] = '<a href="'.$url.'"><span class="glyphicon glyphicon-pencil"></span></a>';
$data[] = $nestedData;
$i++;
}
$json_data = array(
"draw" => intval($requestData['draw']),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFiltered),
"data" => $data // total data array
);
echo json_encode($json_data);
}
if i have more then lakh records will this data-table can handle the records.??
ReplyDeleteYes it can handle records upto 500,000 rows easily, It might go with more records, I have never tested with more records, You can give it a try. but for me, datatables server side processing worked with 500K records.
ReplyDelete