Giter Club home page Giter Club logo

yii2-ajax's Introduction

yii2-ajax

This extension allows you to create forms in the Twitter Bootstrap modal window.

Documentation is at docs/README.md

Installation

The preferred way to install this extension is through composer.

Run

php composer.phar require antkaz/yii2-ajax

or add

"antkaz/yii2-ajax": "~1.2"

to the require section of your composer.json file.

Basic usage

  1. Аdd the modal widget to your view file:

    <?= \antkaz\ajax\Modal::widget([
        'id' => 'modal',
        'clientEvents' => [
            'submit.success.bs.modal' => 'function(event, body) {$(this).modal("hide")}'
        ]
    ]); ?>
  2. Add a link that will open the modal window:

    <?= \yii\bootstrap\Html::a('Create', ['create'], [
        'class' => 'btn btn-success',
        'data-toggle' => 'ajax-modal', // outputs the result to the modal window
        'data-target' => '#modal', // ID modal
        'data-title' => 'Create item' // custom modal title
    ]); ?>
  3. Add the data-ajax attribute in the ActiveForm options:

    <?php $form = \yii\bootstrap\ActiveForm::begin([
        'options' => [
            'data-ajax' => 1
        ],
    ]); ?>
    
    // your fields
    
    <?php \yii\bootstrap\ActiveForm::end() ?>
  4. Create an action in your controller that will handle this request:

    public function actionCreate()
    {
        $model = new Model();
    
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']); // optional
        }
    
        return $this->renderView('create', [
            'model' => $model,
        ]);
    
    }
    
    protected function renderView($view, $params = [])
    {
        if (Yii::$app->request->isAjax) {
            return $this->renderAjax($view, $params);
        }
        return $this->render($view, $params);
    }

Attributes

Link attributes for loading the modal window:

  • data-toggle - Toggle for enabled ajax-modal (required value ajax-modal);
  • data-target - Bootstrap modal selector;
  • data-title - Modal title;
  • data-gridview - Yii2 GridView selector for group operations.

Events

Added the following events, in addition to those that already exist

  • submit.success.bs.modal - This event is triggered after the form has been successfully submitted. The second parameter is the content of the response:

    $('#myModal').on('submit.success.bs.modal', function (event, bodyResponse) {
      // do something...
    })

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.