himiklab/yii2-gridview-ajaxed-widget

Improved Yii2 GridView widget with support ajax, pjax and modal (Bootstrap)

1.0.4 2022-01-12 15:13 UTC

This package is auto-updated.

Last update: 2024-10-12 21:31:30 UTC


README

Improved Yii2 GridView widget with support ajax, pjax and modal (Bootstrap).

Packagist Packagist license

Installation

The preferred way to install this extension is through composer.

  • Either run
php composer.phar require --prefer-dist "himiklab/yii2-gridview-ajaxed-widget" "*"

or add

"himiklab/yii2-gridview-ajaxed-widget" : "*"

to the require section of your application's composer.json file.

Usage

// index.php
use himiklab\yii2\ajaxedgrid\GridView;

GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'title',
        'author',
        'language',
        'visible:boolean',
    ],
    'jsErrorCallback' => 'function(jqXHR, textStatus) {console.log(jqXHR, textStatus, errorThrown)}',
]);
// _form.php
    <?php $form = ActiveForm::begin(['id' => 'test-form']); ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'author')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'language')->dropDownList($model::getAllLanguages()) ?>

    <?= $form->field($model, 'visible')->checkbox() ?>

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>
// controller
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Page::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionCreate()
    {
        $model = new Page();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return null;
        }

        return $this->renderAjax('_form', [
            'model' => $model,
        ]);
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return null;
            //return '#reload';
            //return '#alert OK!';
            //return '#redirect /';
            //return '#file document.txt ' . \base64_encode('document content');
        }

        return $this->renderAjax('_form', [
            'model' => $model,
        ]);
    }

    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
    }

    protected function findModel($id)
    {
        if (($model = Page::findOne($id)) === null) {
            throw new NotFoundHttpException('Page not found.');
        }

        return $model;
    }

It's all!