zoukankan      html  css  js  c++  java
  • YII插件

    dropDownList:

    Yii中可以采用CHtml类来实现,也可以用CActiveForm来实现。
    一、用CHtml来实现。
    VIEW中实现:

    <?php
    echo CHtml::dropDownList('country_id','', array(1=>'USA',7=>'France',3=>'Japan'),
    array(
    'ajax' => array(
    'type'=>'POST', //request type
    'url'=>Yii::app()->createUrl('project/dynamiccities'),
    'update'=>'#city_id', 
    'data'=>array(Yii::app()->request->csrfTokenName=>Yii::app()->request->getCsrfToken(),'country_id'=>'js $("#country_id").val()')
    )));
    
    echo CHtml::dropDownList('city_id','', array());
    
    ?>

    controller中实现:

    public function actionDynamiccities()
    {
      $data=Parts::model()->findAll('prent_id=:prent_id',array(':prent_id'=>(int) $_POST['country_id']));
    
      $data=CHtml::listData($data,'id','name');
      foreach($data as $value=>$name)
      {
        echo CHtml::tag('option',
        array('value'=>$value),CHtml::encode($name),true);
      }
    }

    二、用CActiveForm来实现
    在VIEW中实现:

    <?php echo $form->dropDownList($model,'province_id',$provinceList(这个值可以通过render传递到页面),array(
      'empty'=>'-请选择-',
      'ajax'=>array(
      //指定请求地址
      'url'=>Yii::app()->createUrl('site/dynamicCity'),
      //请求数据
      'data'=>array('pid'=>'js:this.value'),
      //操作元素
      'update'=>'#SosInfo_city_id',(注意这个update的值很容易弄错,它由两部分组成:模型+ID,模型->是指本CActiveForm所承载的model名称)
      ),
    )); ?>
    <?php 
        echo $form->dropDownList($model,'city_id',City::model()->getCityList($model->province_id),array('empty'=>'-请选择-')); 
    ?>

    如果是静态的:
    $form->dropDownList($model, 'field_name', array(1=>'test1', 2=>'test2'))
    如果是动态的:
    $form->dropDownList( $model, 'name', CHtml::listData( modelname::model()->findAll(), 'id', 'name') );

    $form是你view中生成的form, 根据你生成form名字不同,修改它,$model是你对应的模型的名字,也是你自己定, name是和你model数据表中字段的名字一致就行,modelname是模型对应的类名,比如说你的是User,那这里就是 UserRole::model()->findAll()。id和name是你要去列表的那个表中字段,第一个是将作为option的 value,第二个显示名字。

    典型应用场景:
    创建user时,需要选择user的角色,有个表专门定义user的角色,表名是UserRole,User这个表中有一个外键role,它的值是UserRole的主键,UserRole的主键是id,另外一个字段是name,我们将显示给用户。
    那么上面的哪一行代码变成:

    $form->dropDownList( $model, 'name', CHtml::listData( UserRole::model()->findAll(), 'id', 'name') );

    yii jquery折叠、弹对话框、拖拽、滑动条、ol和ul列表、局部内容切换

     <?php Yii::app()->clientScript->registerCoreScript('jquery');?>
     
    <?php
    //yii折叠效果(CJuiAccordion)
    $this->widget('zii.widgets.jui.CJuiAccordion', array(
         'panels'=>array(
             '分类1'=>'分类1的内容',
             '分类2'=>'分类2的内容',
             // 分类可以渲染一个页面,例如分类3
             //'分类3'=>$this->renderPartial('_partial',null,true),
         ),
         'options'=>array(
             'animated'=>'bounceslide',
         ),
     ));
    ?>
     
    <?php
    //按钮加js弹框提示
      $this->widget('zii.widgets.jui.CJuiButton',
            array(
                'name'=>'button',
                'caption'=>'提交',
                'value'=>'asd',
                'onclick'=>'js:function(){alert("提交成功!"); this.blur(); return false;}',
            )
      );
    ?>
     
    <?php
    //谈对话框
    $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
      'id'=>'mydialog',
      // additional javascript options for the dialog plugin
      'options'=>array(
        'title'=>'对话框',
        'autoOpen'=>false,
      ),
    ));
        //输出弹出框的内容
        //echo $this->renderPartial('_form',null,true);
     
    $this->endWidget('zii.widget.jui.CJuiDialog');
     
    //打开对话框的链接
    echo CHtml::link('登录', '#', array(
     'onclick'=>'$("#mydialog").dialog("open"); return false;',
    ));
    ?>
     
    <?php
    //拖拽
     $this->beginWidget('zii.widgets.jui.CJuiDraggable', array(
         // additional javascript options for the draggable plugin
         'options'=>array(
             'scope'=>'myScope',
         ),
     ));
         echo '拖拽的内容!';
     $this->endWidget();
    ?>
     
    <?php
    //ol列表
      $this->widget('zii.widgets.jui.CJuiSelectable', array(
          'items'=>array(
              'id1'=>'Item 1',
              'id2'=>'Item 2',
              'id3'=>'Item 3',
          ),
          // additional javascript options for the selectable plugin
          'options'=>array(
              'delay'=>'300',
          ),
      ));
    //ul列表
      $this->widget('zii.widgets.jui.CJuiSortable', array(
          'items'=>array(
              'id1'=>'Item 1',
              'id2'=>'Item 2',
              'id3'=>'Item 3',
          ),
          // additional javascript options for the accordion plugin
          'options'=>array(
              'delay'=>'300',
          ),
      ));
    ?>
     
    <?php
    //滑动条
      $this->widget('zii.widgets.jui.CJuiSlider', array(
          'value'=>37,
          // additional javascript options for the slider plugin
          'options'=>array(
              'min'=>10,
              'max'=>50,
          ),
          'htmlOptions'=>array(
              'style'=>'height:20px;'
          ),
      ));
    ?>
     
    <?php
    //局部内容切换
      $this->widget('zii.widgets.jui.CJuiTabs', array(
          'tabs'=>array(
              '分类1'=>'分类1',
              '分类2'=>array('content'=>'分类2', 'id'=>'tab2'),
              //'分类3'=>$this->render('_form',null,true),//渲染一个页面
              // panel 3 contains the content rendered by a partial view
              //'AjaxTab'=>array('ajax'=>$ajaxUrl),
          ),
          // additional javascript options for the tabs plugin
          'options'=>array(
              'collapsible'=>true,
          ),
      ));
    ?>

     zii.widgets.grid自定义按钮,ajax触发事件并提示

    我们在用表格展示数据并管理的时候,可能会需要用到按钮来操作某一行数据,比如查看,修改,删除!
    Yii内置了3种按钮:查看,修改和删除,你可以自定义样式、事件。详细配置见类参考:CButtonColumn.
    如果需要自定义按钮绑定指定的事件该怎么办呢?
    幸运的是Yii提供了自定义按钮的办法.看代码:
    在视图文件里面:

    <?php
    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'xx-xx-grid',
        'dataProvider'=>$model->search(),
        'filter'=>$model,
        'pager'=>array(
                'class'=>'CLinkPager',
                'nextPageLabel'=>'下一页',
                'prevPageLabel'=>'上一页',
                'header'=>'',
        ),
        'summaryText'=>'<font color=#0066A4>显示{start}-{end}条.共{count}条记录,当前第{page}页</font>',
        'columns'=>array(
            array(
                 'name'=>'id',
                 'htmlOptions'=>array('width'=>'25'),
                 'sortable'=>false,
            ),
            array(
                'class'=>'CButtonColumn',
                'template'=>'{view} {update}',
                'viewButtonOptions'=>array('title'=>'查看'),
                'updateButtonOptions'=>array('title'=>'修改'),
            ),
            array(
                'class'=>'CButtonColumn',
                'header'=>'首页展示',
                'template'=>'{add} {del}',
                'buttons'=>array(
                        'add' => array(
                                'label'=>'展示',     // text label of the button
                                'url'=>'Yii::app()->controller->createUrl("focus/create",array("id"=>$data->primaryKey,"type"=>1))',       // a PHP expression for generating the URL of the button
                                'imageUrl'=>'http://s.maylou.com/common/images/ysh.jpg',  // image URL of the button. If not set or false, a text link is used
                                'options'=>array('style'=>'cursor:pointer;'), // HTML options for the button tag
                                'click'=>$click,     // a JS function to be invoked when the button is clicked
                                'visible'=>'SiteRecommend::isItemInTypeAndId(1, $data->id)?false:true',
                        ),
                        'del' => array(
                                'label'=>'取消展示',     // text label of the button
                                'url'=>'Yii::app()->controller->createUrl("focus/delete",array("id"=>$data->primaryKey,"type"=>1))',       // a PHP expression for generating the URL of the button
                                'imageUrl'=>'http://s.maylou.com/common/images/yzhu.jpg',  // image URL of the button. If not set or false, a text link is used
                                'options'=>array('style'=>'cursor:pointer;'), // HTML options for the button tag
                                'click'=>$click,     // a JS function to be invoked when the button is clicked
                                'visible'=>'SiteRecommend::isItemInTypeAndId(1, $data->id)?true:false',
                        )
                ),
            ),
        ),
    ));
    ?>

    buttons选项提供了创建按钮的方法,上面创建了2个按钮:add和del,并注册到template里面。其中最主要的是click选项,决定了你的触发条件。这里用ajax触发。在上面的代码前面加上$click内容:

    <?php
        $csrfTokenName = Yii::app()->request->csrfTokenName;
        $csrfToken = Yii::app()->request->csrfToken;
        $csrf = "
    		data:{ '$csrfTokenName':'$csrfToken' },";
        $Confirmation= "你确定要这么做?";
        $afterDelete = 'function(link,success,data){ if(success) alert(data); }';
        $click=<<<EOD
        function() {
        if(!confirm("$Confirmation")) return false;;
        var th=this;
        var afterDelete=$afterDelete;
        $.fn.yiiGridView.update('build-oneprice-grid', {
        type:'POST',
        url:$(this).attr('href'),$csrf
        success:function(data) {
        $.fn.yiiGridView.update('build-oneprice-grid');
            afterDelete(th,true,data);
        },
        error:function(XHR) {
            return afterDelete(th,false,XHR);
        }
        });
        return false;
        }
    EOD;
    ?>

    csrf不用管他,是安全验证,必须要有,否则会400报错.$click是js函数的字符窜,用了文档字符窜形式,注意结束的EOD前面必须没空格,也不能缩进。
    这是Yii内置的yiiGridView Jquery插件,把请求提交到控制器的动作里面处理,然后返回结果并显示。

  • 相关阅读:
    点餐网站学习(EF+MVC)SEO站内结构
    点餐网站学习(EF+MVC)EF工具,自动创建模型并建立数据库映射关系
    个性化定制cms介绍
    EasyUI Munubutton 二级菜单
    程序员也是文艺青年
    EasyUI combobox加入请选择 [.net后台代码]
    LVS配置记录
    查询MYSQL库表使用空间
    python django 数据库操作
    Nginx修改版本信息或隐藏版本号
  • 原文地址:https://www.cnblogs.com/thinksasa/p/3825624.html
Copyright © 2011-2022 走看看