How to use the modal pop-up window instance code of gridview in yii2, yii2gridview_PHP tutorial

Source: Internet
Author: User
How to use the modal pop-up window instance code in yii2 combined with the gridview, yii2gridview. In yii2, The following describes how to use the modal pop-up window instance code of gridview. in the previous article, yii2gridview introduced how to use the modal pop-up window in yii2. How to use modal pop-up window instance code in real yii2 combined with gridview, yii2gridview

In the previous article, I introduced how to use modal pop-up windows (basic usage) in yii2.

In actual development, we often encounter the situation that the list page data is modified to use modal. if it is a general loop display, I believe most people will operate after reading the basic usage of modal, however, it is estimated that some people cannot afford it. let's look at how to solve this problem!

1. add the [update] button to the gridview operation and specify the value of data-toggle data-target class and data-id.

['Class' => 'yii \ grid \ actioncolumn', 'template' => '{update }', 'buttons' => ['update' => function ($ url, $ model, $ key) {return Html: a ('update ','#', ['data-toggle '=> 'modal', 'data-target' =>' # update-modal', 'class' => 'data-Update ', 'data-id' => $ key,]) ;},],],

2. add modal for updates

<? Php use yii \ bootstrap \ Modal; // update operation Modal: begin (['id' => 'UPDATE-modal', 'header' => 'update ', 'footer '=> 'close',]); $ requestUpdateUrl = Url: toRoute ('update'); $ updateJs = <
 
  

JS;

$this->registerJs($updateJs);Modal::end();?>

3. modify our update method

public function actionUpdate($id){$model = $this->findModel($id);if ($model->load(Yii::$app->request->post()) && $model->save()) {return $this->redirect(['index']);} else {return $this->renderAjax('update', ['model' => $model,]);}}

It can be seen that there is no difference in the modal usage in the whole process. However, this is not the end. I believe most people may encounter the following common hard-to-solve problems:

Why is select2 used in yii2 modal not searchable in the search box?

Why does multiple modal pages share one in a single page in yii2?

Yii2 has multiple modals on a single page. We use modal for single page addition and the previous gridview update. for example, when select2 is used, why does the updated select2 fail?

The following describes how to solve these problems one by one:

First, the first problem is that you only need to specify the tabindex of the options option as false when using begin modal. refer to the following:

Modal::begin([// ......'options' => ['tabindex' => false ],]);

The second and third problems are caused by the use of multiple modal in a single page. to illustrate the problem, we use modal in the [create] button in a list and the [update] button in the gridview. According to how we use modal pop-up windows (basic usage) in Yii2 and described in this article, the first question is obviously

$('.modal-body').html(data);

As a result, multiple modal values are assigned to the body of all modal values after we use modal for the first time, so that when other modal values are used later, A bug with the same content is displayed before data is requested. To solve this problem, you only need to assign values to the modal-body after each asynchronous request. the code can be referred to below:

$('#create').on('click', function () {$.get('url', {},function (data) {$('#create-modal').find('.modal-body').html(data);// $('.modal-body').html(data);} );});$('.data-update').on('click', function () {$.get('{$requestUpdateUrl}', { id: $(this).closest('tr').data('key') },function (data) {$('#update-modal').find('.modal-body').html(data);// $('.modal-body').html(data);} );});

Looking at the last question, those who have used select2 should pay attention to it !!!

If the form contains select2, as shown in the example described in this topic, select2 is normal only during [creation, [update] the select2 field "hide" during the operation!

This is actually caused by the same select2 id on the same page. to solve this problem, you only need to remove all existing form items on the page before each asynchronous request for data. See the specific implementation:

$ ('# Create '). on ('click', function () {// effectively avoids the problem of multiply modal select2 // remove the form $$('.doc ument-nav-form '). remove (); $. get ('{$ requestUrl}', {}, function (data) functions ('{create-modal'}.find('.modal-body'}.html (data) ;}); $ ('. data-update '). on ('click', function () {// effectively avoids the problem of multiply modal select2 // remove the form $$('.doc ument-nav-form '). remove (); $. get ('{$ requestUpdateUrl}', {id: $ (this ). closest ('tr '). data ('key')}, function (data) encoding ('{update-modal'}.find('.modal-body'}.html (data );});});

The above is a full description of how to use the modal pop-up window instance code of gridview in yii2. I hope it will help you. if you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

In the previous article, Workshop introduced how to use modal pop-up windows (basic usage) in yii2. Real...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.