ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] #### 数组助手类 ~~~ 常用的就是建立哈希表,map()方法。一般在使用dropDownList的时候,会从查询出来的对象列表中获取到这样的$array供其使用。 $array = [ ['id' => '123', 'name' => 'aaa', 'class' => 'x'], ['id' => '124', 'name' => 'bbb', 'class' => 'x'], ['id' => '345', 'name' => 'ccc', 'class' => 'y'], ); $result = ArrayHelper::map($array, 'id', 'name'); // 结果是: // [ // '123' => 'aaa', // '124' => 'bbb', // '345' => 'ccc', // ] //PS:数组助手类还有很多其他的方法。 可参考http://www.yiichina.com/doc/guide/2.0/helper-array ~~~ #### HTML助手类 > 如果你知道 input 类型,更方便的做法是使用以下快捷方法: yii\helpers\Html::buttonInput() yii\helpers\Html::submitInput() yii\helpers\Html::resetInput() yii\helpers\Html::textInput(), yii\helpers\Html::activeTextInput() yii\helpers\Html::hiddenInput(), yii\helpers\Html::activeHiddenInput() yii\helpers\Html::passwordInput() / yii\helpers\Html::activePasswordInput() yii\helpers\Html::fileInput(), yii\helpers\Html::activeFileInput() yii\helpers\Html::textarea(), yii\helpers\Html::activeTextarea() ~~~ //Radios 和 checkboxes 在方法的声明上有一点点不同: <?= Html::radio('agree', true, ['label' => 'I agree']); <?= Html::activeRadio($model, 'agree', ['class' => 'agreement']) <?= Html::checkbox('agree', true, ['label' => 'I agree']); <?= Html::activeCheckbox($model, 'agree', ['class' => 'agreement']) //Dropdown list 和 list box 将会如下渲染: <?= Html::dropDownList('list', $currentUserId, ArrayHelper::map($userModels, 'id', 'name')) ?> <?= Html::activeDropDownList($users, 'id', ArrayHelper::map($userModels, 'id', 'name')) ?> <?= Html::listBox('list', $currentUserId, ArrayHelper::map($userModels, 'id', 'name')) ?> <?= Html::activeListBox($users, 'id', ArrayHelper::map($userModels, 'id', 'name')) ?> //更多参考 http://www.yiichina.com/doc/guide/2.0/helper-html ~~~ #### HTML Activeform表单部件 > 文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dropDownList(); 隐藏域:hiddenInput(); 文本域:textarea(['rows'=>3]); 文件上传:fileInput(); 提交按钮:submitButton(); 重置按钮:resetButtun(); ~~~ <?php $form = ActiveForm::begin(['action' => ['test/getpost'],'method'=>'post',]); ?> <? echo $form->field($model, 'username')->textInput(['maxlength' => 20]) ?> <? echo $form->field($model, 'create_at')->widget(DatePicker::className(),['clientOptions' => ['dateFormat' => 'yy-mm-dd']])->textInput(['placeholder' => '创建时间']) ?> <? echo $form->field($model, 'password')->passwordInput(['maxlength' => 20]) ?> <? echo $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女']) ?> <? echo $form->field($model, 'edu')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'], ['prompt'=>'请选择','style'=>'width:120px']) ?> <? echo $form->field($model, 'file')->fileInput() ?> <? echo $form->field($model, 'hobby')->checkboxList(['0'=>'篮球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?> <? echo $form->field($model, 'info')->textarea(['rows'=>3]) ?> <? echo $form->field($model, 'userid')->hiddenInput(['value'=>3]) ?> <? echo Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?> <? echo Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?> <?php ActiveForm::end(); ?> ~~~ #### URL助手类 ~~~ //返回首页 $relativeHomeUrl = Url::home(); // /index.php?r=site/index echo Url::to(['site/index']); // /index.php?r=site/index&src=ref1#name echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']); // /index.php?r=post/edit&id=100 assume the alias "@postEdit" is defined as "post/edit" echo Url::to(['@postEdit', 'id' => 100]); // the currently requested URL echo Url::to(); // /images/logo.gif echo Url::to('@web/images/logo.gif'); // images/logo.gif echo Url::to('images/logo.gif'); // http://www.example.com/images/logo.gif echo Url::to('@web/images/logo.gif', true); // https://www.example.com/images/logo.gif echo Url::to('@web/images/logo.gif', 'https'); //更多详情参考http://www.yiichina.com/doc/guide/2.0/helper-url ~~~ #### 面包屑小部件 ~~~ <?php //摘自view文件的代码 $this->params['breadcrumbs'][] = ['label' => '文章管理', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; //摘自layout文件的代码 echo Breadcrumbs::widget([ 'tag'=>'ol', 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], ]); ?> ~~~