yii2杂记

一.
1.引入静态资源
静态资源css,或js,img放在项目底下的web目录下的assets中
在视图中加入

<?php
use yii\helpers\Html;
use yii\helpers\Url;
例如下面的引入
 <?php echo Html::cssFile('@web/assets/css/H-ui.min.css')?>
 <?php echo Html::jsFile('@web/assets/js/H-ui.min.js')?>
 <?php echo Url::to('@web/assets/images/logo.gif');?>
?>

2.如果多个视图要引入一样的js和css,我们可以这么做
在assets目录里的AppAsset.php中加入
定义需要共同引入的css,js,路径就是在项目下的web目录下的资源路径

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\assets;

use yii\web\AssetBundle;

/**
 * Main application asset bundle.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [

        'assets/static/h-ui/css/H-ui.min.css',
        'assets/static/h-ui.admin/css/H-ui.admin.css',
        'lib/Hui-iconfont/1.0.8/iconfont.css',
        'assets/static/h-ui.admin/skin/default/skin.css',
        'assets/static/h-ui.admin/css/style.css'
    ];
    public $js = [
        'assets/lib/jquery/1.9.1/jquery.min.js',
        'assets/lib/layer/2.4/layer.js',
        'assets/static/h-ui/js/H-ui.min.js',
        'assets/static/h-ui.admin/js/H-ui.admin.js',
        'assets/lib/jquery.contextmenu/jquery.contextmenu.r2.js',

    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];


}

在视图开头引入

<?php 
use app\assets\AppAsset;
AppAsset::register($this);
?>

并且在视图中加入

<?php $this->beginPage() ?>
<?php $this->beginBody() ?>
//your codes...
<?= $contents; ?>
//your codes...
<?php $this->endBody() ?>
<?php $this->endPage() ?>

在视图中的header部分加入就引入资源了

 <?php $this->head(); ?>

二.
1. yii2 的ActiveForm组件使用

//做一个笔记方便以后查看
文本框:textInput();
密码框:passwordInput();
单选框:radio(),radioList();
复选框:checkbox(),checkboxList();
下拉框:dropDownList();
隐藏域:hiddenInput();
文本域:textarea(['rows'=>3]);
文件上传:fileInput();
提交按钮:submitButton();
重置按钮:resetButtun(); 

例如

<?php
    use yii\helpers\Html;
    use yii\bootstrap\ActiveForm;

    $form = ActiveForm::begin([
        'action' => ['test/getpost'],
        'method'=>'post',
        'id'=>'form-article-add',
        'options'=>['class'=>'form form-horizontal'],
        'fieldConfig'=>['template'=>"{input}"]
    ]); 
?>

<? echo $form->field($model, 'username')->textInput(['maxlength' => 20]) ?>
<? 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(); ?>

如果表单按照上面生成会自己生成一个div包住input造成样式走偏,如下这里写图片描述
这里写图片描述
所以要在field()方法里加入

['options'=>['tag'=>false]]

例如

 <?php echo $form->field($model,'column',['options'=>['tag'=>false]])->textInput()?>

才会生成正常的表单。如下
这里写图片描述
参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值