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 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA