Hi everybody. I’m using Yii2 advanced, Kartik grid/export menu extensiones, Bootstrap 4 and AdminLTE3 theme. Everything renders properly but when I click on an export menu option nothing happens. If I check all the js libraries included, export and dialog library are missing. I’m quite sure is related to the AJAX call and the way I return data from my controller. When I use normal “render” method, everything works properly.
Here is my gridview code
echo GridView::widget([
'dataProvider' => $dataProvider,
'headerRowOptions' => ['class' => 'kartik-sheet-style'],
'pjax' => true,
'toolbar' => $toolbar,
'toggleDataOptions' => [
'all' => [
'label' => Yii::t('kvgrid', 'Todo'),
'class' => 'btn btn-secondary',
'title' => 'Mostrar todos los resultados'
],
'page' => [
//'icon' => 'resize-small',
'label' => Yii::t('kvgrid', 'Pagina'),
'class' => 'btn btn-secondary',
'title' => 'Mostrar la primera pagina'
],
],
'columns' => $gridColumns,
'showPageSummary' => false,
'summary' => ($dataProvider->getPagination()!=null)?"Mostrando <strong>{begin}</strong> - <strong>{end}</strong> de <strong>{totalCount}</strong> resultados.":$resultados,
'panel' => [
'after'=> $afterGrid,
],
'responsive'=>true,
'options' => ['class' => 'grilla'],
'hover'=>true,
'export' => [
'label' => "Exportar",
'header' => '<li role="presentation" class="dropdown-header">Exportar informacion</li>',
],
'exportConfig' => [
GridView::EXCEL => true,
GridView::CSV => true,
],
'emptyText'=>'No se encontraron resultados.']);
Here is my controller code
public function actionIndex()
{
$model = $this->createFormModel();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if (Yii::$app->request->isAjax) {
$model->load(Yii::$app->request->post());
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$POST_VARIABLE = Yii::$app->request->post('DynamicModel');
$facultad = $POST_VARIABLE['facultad'];
$titulos = $POST_VARIABLE['titulo'];
$data = Egresados::getFilterData($facultad, $titulos);
$seriesRows = $data['seriesRows'];
$dataProvider = $data['provider'];
$gridColumns = $this->buildColumnsGrid();
$table = \GridFormat::buildGrid($dataProvider, $gridColumns);
return [
'seriesRows' => $seriesRows,
'table' => $table,
];
}
} else {
return $this->render('index', [
'model' => $model,
]);
}
}
My js code
function renderData(form) {
$.ajax({
url: form.attr("action"),
type: "post",
dataType: "json",
data: form.serialize(),
success: function (res) {
let chartDumbbellRef = document.getElementById("chart-dumbbell");
$("#table").html(res.table);
},
error: function (xhr, ajaxOptions, thrownError) {
$("#error").closest(".card").removeClass("hidden");
},
});
}
$("#form").on("beforeSubmit", function () {
renderData($(this));
return false;
});