開始收集yii2有用的教程:下拉菜單數據來源

本章小夥將 yii2.0 的下拉列表使用方法傳授給你們,但願你們多多捧場,仍是老樣子,直接上代碼。php

yii2.0 <wbr>下拉菜單

  

第一種方法:ActiveForm 類的 dropDownList 方法(優勢,默認使用yii的樣式)html

一、在控制器的方法裏面 ,咱們須要拿到數據,必定是 findAll() 或者是 all() 方法的數據,實例以下:數據庫

 public function actionIndex()數組

    {yii2

        $model = new UserModel();yii

        $data = Customer::find()->all();this

        return $this->render('index', [url

            'model' => $model,spa

            'data' => $data,orm

        ]);

    }

二、在視圖頁面,咱們使用 yii 的表單生成器。


yii2.0 <wbr>下拉菜單
$form->field($model, 'username')->dropDownList(ArrayHelper::map($data,'id', 'customer_name'));

2.一、dropDownList           --->    yii2.0  下拉列表的方法

2.二、ArrayHelper::map()   --->    構建一個(key => value) 的一維或多維數組

2.3.一、 $data                     --->     數據源

2.3.二、 id                           --->     option 的 value 值

2.3.三、 customer_name    --->     option 標籤的 值

第二種方法:Html 類的 activeDropDownList方法(優勢,能夠自定義任何樣式)


一、和第一種方法的第一步同樣,拿到數據。不過多解釋了。

二、\yii\helpers\Html  類爲咱們提供了下拉列表的實現方法  activeDropDownList 方法

yii2.0 <wbr>下拉菜單

Html::activeDropDownList($model, 'username', ArrayHelper::map($data,'id', 'customer_name'), ['style' => 'border:1px solid red;']);

我沒寫 php 標籤,相信寫過新浪博客的程序猿都知道,寫了 php 標籤 整個代碼都被過濾了,因此複製代碼,本身加上標籤

參數和第一種方法的參數含義相同,不作解釋。

第三種方法:Html 類的 dropDownList方法(優勢,能夠自定義任何樣式)



一、和第一種方法的第一步同樣,拿到數據。不過多解釋了。

二、\yii\helpers\Html  類爲咱們提供了下拉列表的實現方法  dropDownList方法


yii2.0 <wbr>下拉菜單
Html::dropDownList('username', null, ArrayHelper::map($data,'id', 'customer_name'), ['class' => 'dropdownlist']);


第四種方法:自定義下拉菜單內容(新增NEW)


咱們不可能全部的下拉菜單數據都是在 數據庫 裏面讀取的,確定有自定義的一些數據,以下圖所示:

yii2.0 <wbr>下拉菜單

這種的下拉菜單怎麼實現那??? 很簡單,直接在 dropDownList() 方法裏面調數據便可。

yii2.0 <wbr>下拉菜單


我沒寫 php 標籤,相信寫過新浪博客的程序猿都知道,寫了 php 標籤 整個代碼都被過濾了,因此複製代碼,本身加上標籤

參數和第一種方法的參數含義相同,不作解釋。

相關文章
相關標籤/搜索