Home Backend Development PHP Tutorial How to use model pop-up window in Yii2 (basic use)

How to use model pop-up window in Yii2 (basic use)

May 03, 2018 pm 05:43 PM
modal yii2 Pop-ups

This article mainly introduces how to use modal pop-up windows in Yii2 and related basic information. It is very good and has reference value. Friends in need can refer to it

Modal is also a modal window. In layman's terms, it is a pop-up window. It is a bootstrap js plug-in, and the effect is very good.

Needless to say why you should use modal, I don’t believe you if you say you have never used js pop-up windows during the development process of a website! A good pop-up window not only gives people a sense of beauty, but also improves our development efficiency and even makes us feel better!

Let’s see how to use modal in yii2.

For example, when we added data before, we usually click the button to jump to the add page, and then jump to the list page after saving.

Now we hope that when we click the add button, data will be added to the pop-up window on the current page. See the specific implementation.

1、use yii\bootstrap\Modal;

2、创建一个按钮,用于调modal的显示

echo Html::a('创建', '#', [
'id' => 'create',
'data-toggle' => 'modal',
'data-target' => '#create-modal',
'class' => 'btn btn-success',
]);

3、创建modal

<?php 
Modal::begin([
&#39;id&#39; => &#39;create-modal&#39;,
&#39;header&#39; => &#39;<h4 class="modal-title">创建</h4>&#39;,
&#39;footer&#39; => &#39;<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>&#39;,
]); 
$requestUrl = Url::toRoute(&#39;create&#39;);
$js = <<<JS
$.get(&#39;{$requestUrl}&#39;, {},
function (data) {
$(&#39;.modal-body&#39;).html(data);
} 
);
JS;
$this->registerJs($js);
Modal::end(); 
?>

4、修改我们的create操作如下

public function actionCreate()
{
$model = new Test();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect([&#39;index&#39;]);
} else {
return $this->renderAjax(&#39;create&#39;, [
&#39;model&#39; => $model,
]);
}
}
Copy after login

At this time, when we click the button [Create], we will see the modal pop-up window, the screenshot is as follows.

Some students may say that there is no need to load this page asynchronously. Indeed, you can also echo $this->renderAjax(); directly on the page, but you need to be reminded that for this operation, remember to modify the action submitted by the form.

Regarding the use of modal, there are two points that need to be reminded here:

Set the attribute data-toggle="modal" on the control element (such as a button or link), and set data- target="#identifier" or href="#identifier" to specify the specific modal box to be switched (with id="identifier")

Above, we have implemented the basic use of modal in yii2 .

Related recommendations:

Yii implements two methods of adding default values ​​to the model



##

The above is the detailed content of How to use model pop-up window in Yii2 (basic use). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Vue to implement pop-up window effects How to use Vue to implement pop-up window effects Sep 22, 2023 am 09:40 AM

How to use Vue to implement pop-up window effects requires specific code examples. In recent years, with the development of web applications, pop-up window effects have become one of the commonly used interaction methods among developers. As a popular JavaScript framework, Vue provides rich functions and ease of use, and is very suitable for implementing pop-up window effects. This article will introduce how to use Vue to implement pop-up window effects and provide specific code examples. First, we need to create a new Vue project using Vue's CLI tool. open end

What should I do if there are no pop-up reminders for calendar events in Win10? How to recover if calendar event reminders are gone in Win10 What should I do if there are no pop-up reminders for calendar events in Win10? How to recover if calendar event reminders are gone in Win10 Jun 09, 2024 pm 02:52 PM

The calendar can help users record your schedule and even set reminders. However, many users are asking what to do if calendar event reminders do not pop up in Windows 10? Users can first check the Windows update status or clear the Windows App Store cache to perform the operation. Let this site carefully introduce to users the analysis of the problem of Win10 calendar event reminder not popping up. To add calendar events, click the "Calendar" program in the system menu. Click the left mouse button on a date in the calendar. Enter the event name and reminder time in the editing window, and click the "Save" button to add the event. Solving the problem of win10 calendar event reminder not popping up

How to solve the problem of Win11 pop-up windows that cannot be closed How to solve the problem of Win11 pop-up windows that cannot be closed Dec 22, 2023 pm 05:13 PM

I believe that when using the computer, we are all troubled by pop-up windows that pop up unintentionally. Especially after updating the system, we even encountered the problem that the win11 pop-up window cannot be closed. At this time, we can only close it in the task manager. Solution to the problem that the win11 pop-up window cannot be closed: 1. First press the "Win+R" key combination on the keyboard to open Run. 2. Then enter “msconfig” and press Enter to run. 3. Then enter "Startup" and click "Open Task Manager" 4. Then select the application that pops up under the startup options. 5. Finally, click "Disable" in the lower right corner.

How to open the 360 ​​browser pop-up window How to open the 360 ​​browser pop-up window Mar 28, 2024 pm 09:31 PM

Pop-up windows can be opened and used by themselves in 360 Browser. Some users do not know how to open pop-up windows in 360 Browser. Just uncheck the box in the advanced settings to not allow any website to display pop-up windows. This pop-up window Opening the introduction of the setting method will tell you the specific operation method. The following is a detailed introduction, so take a look. How to open the 360 ​​Browser pop-up window? Answer: Uncheck the box in the advanced settings to not allow any website to display pop-up windows. Detailed introduction: 1. Open the 360 ​​Browser and click the [Settings] icon on the upper right. 2. Select [Option]. 3. Click [Advanced Settings] in the list on the left. 4. Uncheck [Do not allow any website to display pop-up windows].

How to remove jquery in yii2 How to remove jquery in yii2 Feb 17, 2023 am 09:55 AM

How to remove jquery from yii2: 1. Edit the AppAsset.php file and comment out the "yii\web\YiiAsset" value in the variable $depends; 2. Edit the main.php file and add the configuration "'yii" under the field "components" \web\JqueryAsset' => ['js' => [],'sourcePath' => null,]," to remove the jquery script.

Why do win11 users keep receiving account control pop-ups? Why do win11 users keep receiving account control pop-ups? Jan 08, 2024 am 11:30 AM

Sometimes when we open software or programs, we will find that the win11 user account control keeps popping up, but we don’t know why. In fact, this is a way of system protection to prevent us from being invaded by bad software. Why does win11 user account control keep popping up: A: Because win11 user account control is turned on. After it is turned on, the system will keep popping up to remind you in order to protect the computer security and prevent it from being invaded by bad software. Win11 User Account Control Keeps Playing Solution 1. If we find it troublesome to keep playing, we want to close it. 2. Then you can click on the "Start Menu" at the bottom. 3. Then search and open "Change User Account Control Settings". 4. Finally, move the left slider to "Never notify" and "OK" to save.

What should I do if pop-up windows always appear in Win11? How to solve the problem of pop-up windows when opening a software in Win11 What should I do if pop-up windows always appear in Win11? How to solve the problem of pop-up windows when opening a software in Win11 Mar 01, 2024 am 08:43 AM

When using Win11 system, sometimes a pop-up window will appear when opening a certain software, which will affect the user experience. This article will introduce how to solve the problem of pop-up windows when opening a software in Win11, and help users solve this problem. Method 1: 1. Press the [Win+S] key combination, or click the [Search icon] next to the start icon on the taskbar. In the opened Windows search, enter [Control Panel] in the search box, and then click to open the system given Best match [Control Panel Application]; 2. For all control panel item windows, switch to the [Large Icons] view mode, then find and click [Security and Maintenance] under Adjust Computer Settings; 3. Security and Maintenance window, left Side click [Change User Account Control Settings]; 4. User

Steps to remove pop-up ads in win10 system Steps to remove pop-up ads in win10 system Mar 27, 2024 pm 06:36 PM

1. Open 360 Security Guard with the mouse and click More in the lower right corner, as shown in the picture: 2. Find pop-up interception in the interface or enter pop-up interception in the search box and click to open, as shown in the picture: 3. In the interface, click Powerful Interception, and then click Enable in the prompt window, as shown in the figure:

See all articles