繁体   English   中英

Yii CGridview模型搜索使用OR ||

[英]Yii CGridview model search using OR ||

我创建了一个高级搜索表单,该表单将使用ORIN条件搜索模型。 但是,它无法更新CGridView因为它返回了模型属性的数组。 如果我从gridview删除这些列,那么它将起作用,但是我需要这些列在gridview上可见。

模型

public function weeklystatus($arr = array())
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        //$criteria->compare('id',$this->id);
        $criteria->compare('PROJECT',$this->PROJECT,true);
        if(!empty($arr) && isset($arr['PROJCODE'])){
            $str = '';
            foreach($arr['PROJCODE'] as $value) {
                $str .= "PROJCODE = '$value' || ";
            }           
            $criteria->addCondition(substr($str, 0, -3));
        }else
            $criteria->compare('PROJCODE',$this->PROJCODE,true);
        $criteria->compare('PROJID',$this->PROJID);
        $criteria->mergeWith($this->dateRangeSearchCriteria('StartDATE', $this->StartDATE));        
        $criteria->mergeWith($this->dateRangeSearchCriteria('ProjectEndDate', $this->ProjectEndDate));
        $criteria->mergeWith($this->dateRangeSearchCriteria('ActualEndDate', $this->ActualEndDate));
        $criteria->compare('PROCESSOR',$this->PROCESSOR,true);
        $criteria->compare('OFFICE',$this->OFFICE,true);
        $criteria->compare('DEPTCODE',$this->DEPTCODE,true);
        //$criteria->compare('PERCENT',$this->PERCENT,true);        
        $criteria->compare('PERCENTPlanned',$this->PERCENTPlanned,true);
        $criteria->compare('KM',$this->KM,true);
        $criteria->compare('KMPlanned',$this->KMPlanned,true);
        if(!empty($arr) && isset($arr['MC'])){
            $str = '';
            foreach($arr['MC'] as $value) {
                $str .= "MC = '$value'";
            }
            $criteria->addCondition($str);
        }else{
            $criteria->compare('MC',$this->MC,true);
        }
        $criteria->compare('MCSALE',$this->MCSALE);
        $criteria->compare('CATEGORY',$this->CATEGORY,true);
        $criteria->compare('AREA',$this->AREA,true);
        $criteria->compare('COUNTRY',$this->COUNTRY,true);
        $criteria->compare('PROJINFO',$this->PROJINFO,true);
        $criteria->compare('REGION',$this->REGION,true);
        $criteria->compare('ASAAREA',$this->ASAAREA,true);
        $criteria->compare('LORM',$this->LORM,true);

        $date = date('Y-m-d',strtotime("-2 week"));
        $criteria->addCondition("StartDATE > '2013-01-01'"); 
        $criteria->addCondition("StartDATE < '$date' AND PERCENT <100 || StartDATE > '$date' AND PERCENT =100 ");
                /*AND (StartDATE < '$date' AND PERCENT <100)
                || (StartDATE > '$date' AND PERCENT =100) ");*/
        //$criteria->addCondition("PERCENT < 100 AND StartDATE < '$date'");
        //$criteria->addCondition('StartDATE > '.$date . ' AND PERCENT >  -1' );

        $criteria->order = 'ASAAREA';


        return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
                'pagination'=>array(
                        'pageSize'=>25,
                ),
        ));
    }

调节器

public function actionWeeklystatus(){       
        $model=new ViewWebprojectreport('weeklystatus');
        $model->unsetAttributes();  // clear any default values
        $globalController = new GlobalController;

        if(isset($_GET['ViewWebprojectreport'])){

            $model->attributes=$_GET['ViewWebprojectreport'];
        }

        //print_r($model->attributes);
        //exit();

        $processorList = $globalController->getProcessorList();
        $projCodeList = $globalController->getProjectcodeList();
        $asaarea = Yii::app()->db->createCommand("SELECT DISTINCT
                (   view_webprojectreport.ASAAREA   )
                FROM
                view_webprojectreport
                ORDER BY
                view_webprojectreport.ASAAREA ASC")->queryAll();
        $asaareaList = CHtml::listData($asaarea, 'ASAAREA','ASAAREA');
        $officeList = $globalController->getOfficeList();
        $deptCode = $this->getDepartmentCode();
        $deptCodeList = CHtml::listData($deptCode, 'Code','Code');
        $categoryList = $globalController->getCategoryList(array("condition"=>"category_id < 8 || (category_id>8 && category_id < 14) || category_id > 31"));
        $countryList = $globalController->getCountryList();
        $region =Yii::app()->db->createCommand("SELECT DISTINCT
                (   view_webprojectreport.REGION    )
                FROM
                view_webprojectreport
                ORDER BY
                view_webprojectreport.REGION ASC")->queryAll();
        $regionList = CHtml::listData($region, 'REGION','REGION');
        $mcList = array('MC' => "MC","P"=>"Propietary");

        Yii::app()->user->setState('exportModel',$model); //this can be any other storage

        $this->render("weekly_status",array(
                "model"=>$model,    
                'processorList'=>$processorList,
                'asaareaList'=>$asaareaList,
                'projCodeList'=>$projCodeList,
                'officeList'=>$officeList,
                'deptCodeList'=>$deptCodeList,
                'categoryList'=>$categoryList,
                'countryList'=>$countryList,
                'regionList'=>$regionList,
                'mcList'=>$mcList
                ));
    }

视图

<?php 
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
    $('.search-form').toggle();
    return false;
});

$('.search-form form').submit(function(){
    $.fn.yiiGridView.update('view-weekly-status-grid', {
        data: $(this).serialize(),
    });
    return false;
});

",CClientScript::POS_LOAD);
?>
</head>

<h1>Weekly Status Report</h1>

<div style="font-size: 12pt;">
    <?php //echo CHtml::link('<img src="images/pdficon.gif"/>',array("ViewWebprojectreport/exportToPdf","report"=>"weeklystatus")); ?>
    <?php  echo $this->widget('zii.widgets.jui.CJuiButton', array(       
          'buttonType'=>'link',
          'name'=>'btnDownloadWeeklyStatus',
          'caption'=>'Download Weekly Status',
          'options'=>array('icons'=>'js:{primary:"ui-icon-document"}'),
            "url"=>"index.php?r=ViewWebprojectreport/exportToPdf&report=weeklystatus",
        'htmlOptions'=>array('style'=>"font-size: 12px;height: 30px;"),
            'themeUrl'=>Yii::app()->baseUrl.'/themes',//url of your themes for JUI
        'theme'=>'redmond', //theme name the above url should have a folder named by the theme name specified here eg. /protected/extensions/jui/jquery/css/redmond/
        'cssFile'=>'jquery-ui-1.10.3.custom.min.css', // the css file in the theme folder
         ),true); ?>
</div>


<div id="weekly_status_graph" style="height:800px;display:none;width: 1120px;">

</div>



<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php  $this->renderPartial('_search',array(
    'model'=>$model,
    'processorList'=>$processorList,
    'asaareaList'=>$asaareaList,
    'projCodeList'=>$projCodeList,
    'officeList'=>$officeList,
    'deptCodeList'=>$deptCodeList,
    'categoryList'=>$categoryList,
    'countryList'=>$countryList,
    'regionList'=>$regionList,
    'mcList'=>$mcList
)); ?>
</div><!-- search-form -->


<?php $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'view-weekly-status-grid',
        'dataProvider'=>$model->weeklystatus(),
        'filter'=>$model,
        'rowCssClassExpression'=>'($data->PERCENT == 100)? "yellow" : ($row%2?"even":"odd")',
        //'ajaxUpdate' => TRUE,
        'afterAjaxUpdate'=>'function(id, data){
            $.ajax({
                url: "index.php?r=ViewWebprojectreport/weeklystatusgraph",
                dataType: "json",
                type: "get",
                success: function(data) {
                        if(data.totalprojects>40){
                            $("#weekly_status_graph").css("width","1900px");
                            $("#weekly_status_graph").css("margin-left","-390px");

                        }else if(data.totalprojects>20){        
                            $("#weekly_status_graph").css("width","1250px");
                            $("#weekly_status_graph").css("margin-left","-50px");
                        }else{
                            $("#weekly_status_graph").css("width","1120px");
                            $("#weekly_status_graph").css("margin-left","-0px");
                        }

                        $("#weekly_status_graph").show();
                        buildColumnGraph(data);

                }
            })
        }',
        'columns'=>array(       
                //'MC',
                array(
                        "name"=>"MC",
                        "header"=>"MC/P",
                        "htmlOptions"=>array(
                                "width"=>"20px"
                        )
                ),
                //'OFFICE',
                array(
                        "name"=>"PROJCODE",
                        "header"=>"Center",
                        "htmlOptions"=>array(
                                "width"=>"15px"
                        )
                ),
                //'ASAAREA',
                array(
                        "name"=>"ASAAREA",
                        "header"=>"Region",
                        "htmlOptions"=>array(
                                "width"=>"80px"
                        )
                ),
                //'PROJECT',
                array(
                        "name"=>"PROJECT",
                        "htmlOptions"=>array(
                                "width"=>"150px"
                        )
                ),
                //'LORM',
                array(
                    "name"=>"LORM",
                    "htmlOptions"=>array(
                        "width"=>"20px"
                    )
                ),
                'CATEGORY',
                //'KM',
                array(
                        "name"=>"KM",
                        "header"=>"KM/KM<sup>2</sup>"
                        /*"htmlOptions"=>array(
                                "width"=>"20px"
                        )*/
                ),
                //PROC TYPE
                //'StartDATE',
                array(
                    "name"=>"StartDATE",
                    "htmlOptions"=>array(
                        "width"=>"65px"
                    )
                ),
                //'ProjectEndDate',
                array(
                    "name"=>"ProjectEndDate",
                    "htmlOptions"=>array(
                        "width"=>"65px"
                    )
                ),
                //current date
                /*array(
                    "header"=>"Current date",
                    "value"=>$date
                ),*/
                // planned poc
                // actiaul poc
                //expected
                //'ActualEndDate',
                array(
                    "name"=>"ActualEndDate",
                    "htmlOptions"=>array(
                        "width"=>"65px"
                    )
                ),
                array(
                    "name"=>"PROJINFO",
                    'type'=>'raw',  
                    "value"=>'( strlen($data->PROJINFO) > 200 ) ? CHtml::tag("span", array("title"=>$data->PROJINFO), CHtml::encode(substr($data->PROJINFO, 0, 200)) . "..") : CHtml::encode($data->PROJINFO)',
//                      '$data->GetTruncatedPROJINFO()' ,
                    "htmlOptions"=>array(
                        //"width"=>"300px"
                    )
                ),
                array(
                    'class'=>'CButtonColumn',
                    'template'=>'{view}',
                    'htmlOptions' => array('style'=>'width:5px'),
        ),
    ),
)); ?>

ASAAREA我创建了一个复选框,所以我可以在1列上使用2个条件搜索模型。 它返回结果,但渲染页面失败,因为我在ASAAREA上显示CGridView 我该如何解决这个问题?

如果我注释掉//'filter'=>$model,在视图上可以正常工作。 但是我不想这样做。

如果我不评论filter ,它将产生堆栈跟踪

Error 500: <h1>PHP Error [2]</h1>
<p>htmlspecialchars() expects parameter 1 to be string, array given (/opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php:103)</p>
<pre>#0 unknown(0): CWebApplication->handleError()
#1 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(103): htmlspecialchars()
#2 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(2610): encode()
#3 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(158): renderAttributes()
#4 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(2255): tag()
#5 /opt/yii-1.1.14.f0fee9/framework/web/helpers/CHtml.php(1434): activeInputField()
#6 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CDataColumn.php(103): activeTextField()
#7 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridColumn.php(116): CDataColumn->renderFilterCellContent()
#8 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(532): CDataColumn->renderFilterCell()
#9 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(510): CGridView->renderFilter()
#10 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/grid/CGridView.php(480): CGridView->renderTableHeader()
#11 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(167): CGridView->renderItems()
#12 unknown(0): CGridView->renderSection()
#13 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(150): preg_replace_callback()
#14 /opt/yii-1.1.14.f0fee9/framework/zii/widgets/CBaseListView.php(135): CGridView->renderContent()
#15 /opt/yii-1.1.14.f0fee9/framework/web/CBaseController.php(173): CGridView->run()
#16 /opt/paradox/protected/views/viewWebprojectreport/weekly_status.php(314): ViewWebprojectreportController->widget()
#17 /opt/yii-1.1.14.f0fee9/framework/web/CBaseController.php(126): require()
#18 /opt/yii-1.1.14.f0fee9/framework/web/CBaseController.php(95): ViewWebprojectreportController->renderInternal()
#19 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(869): ViewWebprojectreportController->renderFile()
#20 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(782): ViewWebprojectreportController->renderPartial()
#21 /opt/paradox/protected/controllers/ViewWebprojectreportController.php(970): ViewWebprojectreportController->render()
#22 /opt/yii-1.1.14.f0fee9/framework/web/actions/CInlineAction.php(49): ViewWebprojectreportController->actionWeeklystatus()
#23 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(308): CInlineAction->runWithParams()
#24 /opt/yii-1.1.14.f0fee9/framework/web/filters/CFilterChain.php(133): ViewWebprojectreportController->runAction()
#25 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(291): CFilterChain->run()
#26 /opt/yii-1.1.14.f0fee9/framework/web/CController.php(265): ViewWebprojectreportController->runActionWithFilters()
#27 /opt/yii-1.1.14.f0fee9/framework/web/CWebApplication.php(282): ViewWebprojectreportController->run()
#28 /opt/yii-1.1.14.f0fee9/framework/web/CWebApplication.php(141): CWebApplication->runController()
#29 /opt/yii-1.1.14.f0fee9/framework/base/CApplication.php(180): CWebApplication->processRequest()
#30 /opt/paradox/index.php(31): CWebApplication->run()
</pre>

如果需要显示作为数组的模型的属性,并且例如要在网格的单元格中显示所有“值”,请考虑使用clistview。 您的高级搜索仍将起作用,并且您将能够为每个单元格编写自定义html。

另一个解决方案是在cgridview中呈现一个虚拟变量,该虚拟变量可序列化您的数组属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM