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) {...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答