dpndp64206 2017-01-25 12:03
浏览 46
已采纳

在yii网格视图中为两个属性调用该函数一次

I have a common function where I use them for different purposes in my yii grid view. For instance say my attributes are col1 and col2. They both call the same function, based on the value I perform different actions. The function needs to go through millions of data before returning back to my grid view.

I am calling the function twice here once in col1 and once in col2. is there anyway that I can call the function only once and use it for both the attributes, which will be very handy? Any help regarding this will be great. Thank you.

array(
                'header'=>'Phrase Used',
                'htmlOptions'=>array('style'=>'width:10px;text-align:center;'),
                'value'=> function($data){
                            if($data->usedBankPhrase($data->bank_id) == 1) {echo "<span class=\"translated-badge\" title =\"Used \">u</span>";}
                            else{echo "<span class=\"badge\" style=\"background-color:red !important; padding:2px;\" title =\" Not Used \">nu</span>";}
                            },
                'filter'=>'',
            ),

    //delete button
    'remove' => array(
                        'visible'=>'!$data->usedBankPhrase($data->bank_id);',
                        'label' => 'Delete Phrase',
                        'imageUrl'=>  Yii::app()->request->baseUrl.'/images/icons/cross.png',
                        'options'=>array('class'=>'full-bank-delete', 'id'=>'\'remove-banker-\'.$data->bank_id'), //HTML options for the button tag.
                    ),
  • 写回答

1条回答 默认 最新

  • doutuan8887 2017-01-25 12:22
    关注

    In model:

    Define property

    public $storedUsedBankPhrase = null;
    

    Create getter:

    public function getCalculatedUsedBankPhrase() {
       if($this->storedUsedBankPhrase === null) {
          $this->storedUsedBankPhrase= $this->usedBankPhrase($this->bank_id);
       }
       return $this->storedUsedBankPhrase ;
    }
    

    In GridView use:

    'value'=> function($data){
               if($data->calculatedUsedBankPhrase == 1) {...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊