№10 Dars — Yii da modal va ajaxdan foydalanish.
Ajax so`rovi bilan ma`lumotlar omboriga modaldan foydalanib ma`lumotlar qo`shish masalasini ko`rib chiqamiz. Avvalambor ma`lumotlar omborida jadval yaratishdan boshlaymiz. Buning uchun
quyidagi havoladagi koddan foydalanib jadval yaratamiz. Model hosil qilamiz va index.php viewimizga quyidagicha kod kiritamiz.
/protected/views/site/index.php
widget('bootstrap.widgets.TbButton',array( 'label' => Yii::t('Message', 'Add'), 'type' => 'primary', 'size' => 'medium', 'htmlOptions'=>array( 'onclick'=>' $(".ui-dialog-titlebar > #ui-dialog-title-dialog_form").text("Modal"); $("#dialog_form .update-dialog-content ").html("Loading...
"); addcompany(); $("#dialog_form").dialog("open"); return false;' ), )); ?> beginWidget('zii.widgets.jui.CJuiDialog', array( 'id' => 'dialog_form', 'options' => array( 'title' => 'qale', 'autoOpen' => false, 'modal' => true, 'position' => array(center,60), 'width' => 550, 'resizable' => false, ), 'htmlOptions'=>array('style'=>'opacity:1;'), )); ?> endWidget(); ?>
va «site» controllerimizga quyidagicha action qo`shamiz:
/protected/controllers/site
public function actionCreate() { $model = new Company; if( isset( $_POST['Company'] ) ) { // kelgan POST qiymatlar model attributlariga berilmoqda $model->attributes = $_POST['Company']; if( $model->save() ) { if (Yii::app()->request->isAjaxRequest) { Yii::app()->clientScript->scriptMap['jquery.js'] = false; echo CJSON::encode( array( 'status' => 'success', 'div' => 'created', )); exit(); } else $this->redirect( array( 'add' ) ); } } if (Yii::app()->request->isAjaxRequest) { Yii::app()->clientScript->scriptMap['jquery.js'] = false; Yii::app()->clientScript->scriptMap['bootstrap.min.js'] = false; Yii::app()->clientScript->scriptMap['bootstrap.notify.js'] = false; Yii::app()->clientScript->scriptMap['bootstrap.bootbox.min.js'] = false; echo CJSON::encode( array( 'status' => 'failure', 'div' => $this->renderPartial('add',array( 'model'=>$model, ),true,true), )); exit(); } }
«add» viewimizning ko`rinishi quyidagicha bo`ladi:
/protected/views/site/add.php
beginWidget('bootstrap.widgets.TbActiveForm', array( 'id'=>'add-form', // forma ichidagi komponentlarimizning tartiblanishini belgilash 'type'=>'horizontal', )); ?>endWidget(); ?>
label($model,'company'); ?> textField($model,'company'); ?> error($model,'company'); ?>label($model,'site'); ?> textField($model,'site'); ?> error($model,'site'); ?>
Ko`rib turganingizdek ma`lumotlarni modal komponenti orqali qo`shmoqdamiz. Bu yerda web ilovamiz qayta yuklanmay turib server bilan ishlamoqda. Bu ishimiz web ilovalarni client interfeysini qilayotganimizda ancha foydali bo`ladi. Ancha qulay va samarali.
Manba:
Web Dasturlash
№10 Dars — Yii da modal va ajaxdan foydalanish.