學習YII2 ---是時候進階下了

 

若是你開始使用yii超過一個月了,你可能有點體會了。各類快速。各類方便。可是這纔是剛剛開始。php

如今就來爲了更好的使用yii,來點基礎知識。進階必備。html

主要就一條:看下基礎類的源碼,object類,yii的建立object方法。魔術方法的重寫。保證全看懂。json

進階實踐,學習思路:後端

1.你該如何經過一個配置。生成一個對象。傳遞當前對象。yii

2.如何組件化。簡單的數據模型配置,構建簡單的增刪改查。例如作一個:字段的組件化,視圖的公共方法的操做ide

3.組件化後的擴展。在已經成組件後還不能完成你的需求,如何擴展。組件化

------學習

關鍵:this

方法;spa

call_user_func();

\Yii::createObject

關鍵字:

static 

view層的option你會使用嗎?

------

貢獻個代碼

使用聯動;

<?= $form->field($model, 'islimited')->widget(SwitchShow::className(), ['willInfluAttr' => ['startexam'=>['1'],'endexam'=>['1']],'dropDownListItems' => ['0'=>'不開啓', '1'=>'開啓'], 'dropDownListOptions' => []]);?>

  
    <?= $form->field($model, 'startexam')->widget(DateTimePicker::className()) ?>

 

聯動基礎類 

<?php

namespace source\core\widgets\form;

use yii\helpers\Html;
use yii\widgets\InputWidget;

class SwitchShow extends InputWidget
{
    //is array ,key is the will change attribute ,value is array,like ['1'], in it will show
    public $willInfluAttr = [];
    public $dropDownListItems = [];
    public $dropDownListOptions = [];

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function init()
    {
        parent::init();
        // $this->model->formName();
        $theAttr = $this->attribute;
        $htmlFormFieldId = strtolower($this->model->formName() . '-' . $this->attribute);
        $needHide = '';
        $html = '';
        if (!empty($this->willInfluAttr)) {
            foreach ($this->willInfluAttr as $key => $value) {
                $influFieldKey = strtolower($this->model->formName() . '-' . $key);
                $showArray = json_encode($value);
                $html .= "if($.inArray($(this).val(),{$showArray}) > -1 ){
                    $('.field-{$influFieldKey}').show();
                }else{
                    $('.field-{$influFieldKey}').hide();
                }";
                if (!in_array($this->model->$theAttr, $value)) {
                    $needHide .= "$('.field-{$influFieldKey}').hide();";
                }
            }
        }
        $jsstr = <<<html

            $('#{$htmlFormFieldId}').change(function(){
                {$html}
            });
            {$needHide}
html;

        $this->view->registerJs($jsstr);

    }
    /**
     * @return string
     */
    public function run()
    {
        $content = [];
        $content[] = $this->dropDownList($this->dropDownListItems, $this->dropDownListOptions);
        return implode("\n", $content);
    }

    public function dropDownList($items, $options = [], $generateDefault = true)
    {

        if ($generateDefault === true && !isset($options['prompt'])) {
            $options['prompt'] = '請選擇';
        }
        Html::addCssClass($options, 'form-control');
        return Html::activeDropDownList($this->model, $this->attribute, $items, $options);
    }

}

 

計劃點:先後端的靜態分離後,文件如何放置。資源發佈是否是要拋棄。

相關文章
相關標籤/搜索