Columna Expandible de Kartik Categoría Detalle
1.- Crear las tablas en la base de datos:
CREATE TABLE `yii2_proyecto`.`author` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(255) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
CREATE TABLE `yii2_proyecto`.`author` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(255) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
CREATE TABLE `yii2_proyecto`.`author_book` ( `id` INT NOT NULL AUTO_INCREMENT , `author_id` INT NOT NULL , `book_id` INT NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
ALTER TABLE `author_book` ADD FOREIGN KEY (`author_id`) REFERENCES `author`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `author_book` ADD FOREIGN KEY (`book_id`) REFERENCES `book`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
2.- Crear el módulo:
Module Class:
frontend\modules\library\Module;
Module ID:
library
Añadir en el main.php:
'library' =>
[
'class' => 'frontend\modules\library\Module',
],
3.- Crear el modelo de Author en el gii
Table Name:
author
Model Class Name:
Author
Namespace:
frontend\modules\library\models\Author
4.- Crear CRUD Generator de author:
Model Class:
frontend\modules\library\models\Author\Author
Search Model Class:
frontend\modules\library\models\Author\AuthorSearch
Controller Class:
frontend\modules\library\controllers\AuthorController
View Path:
@frontend/modules/library/views/author
5.- Crear el modelo en el gii de la tabla book
Table Name:
book
Model Class Name:
Book
Namespace:
frontend\modules\library\models\Book
6.- Crear CRUD Generator de book:
Model Class:
frontend\modules\library\models\Book\Book
Search Model Class:
frontend\modules\library\models\Book\BookSearch
Controller Class:
frontend\modules\library\controllers\BookController
View Path:
@frontend/modules/library/views/book
7.- Crear el modelo en el gii de la tabla author_book
Table Name:
author_book
Model Class Name:
AuthorBook
Namespace:
frontend\modules\library\models\AuthorBook
8.- Añadir en el modelo el use a las claves foraneas
use frontend\modules\library\models\Book\Book;
use frontend\modules\library\models\Author\Author;
9.- Añadimos las columnas en una variable, donde vamos a crear la columna expandible para ver los detalles
<?php
$gridColumns = [
[
'class'=>'kartik\grid\SerialColumn',
'width'=>'20px',
],
[
'class' => 'kartik\grid\ExpandRowColumn',
'width' => '50px',
'value' => function ($model, $key, $index, $column) {
return GridView::ROW_COLLAPSED;
},
'detailUrl' => Url::to(['/library/author/book-detail']),
'headerOptions' => ['class' => 'kartik-sheet-style']
],
[
'attribute' => 'name',
'vAlign' => 'middle',
'hAlign' => 'center'
],
[
'class' => 'kartik\grid\ActionColumn',
],
];
?>
10.- Colocar en el gridView la columna
'columns' =>$gridColumns,
10.1 Añadir el use del Url:
use yii\helpers\Url;
11.- Crear en el AuthorController el metodo que va a renderizar la vista
public function actionBookDetail() {
if (isset($_POST['expandRowKey'])) {
$models = AuthorBook::find()->where(['author_id'=>$_POST['expandRowKey']]) ->all();
return $this->renderPartial('book-detail', [
'models'=>$models
]);
} else {
return '<div class="alert alert-danger">No data found</div>';
}
}
12.- Añadir el use del modelo AuthorBook
use frontend\modules\library\models\AuthorBook\AuthorBook;
13.- Crear la vista book-detail
<table class="responsive" width="100%">
<tr>
<td>
Libros
</td>
</tr>
<?php
foreach ($models as $value)
{
?>
<tr>
<td>
<?= $value->book->name ?>
</td>
</tr>
<?php
}
?>
</table>
Puedes ver los detalles en el canal de YouTube: https://youtu.be/fdEYjt4PQa0
Comentarios
Publicar un comentario