2017-01-25 12:03
浏览 46


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.

                'header'=>'Phrase Used',
                '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>";}

    //delete button
    'remove' => array(
                        '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.

图片转代码服务由CSDN问答提供 功能建议

我有一个常用的函数,我在yii网格视图中将它们用于不同的目的。 比如说我的属性是col1和col2。 它们都根据我执行不同操作的值调用相同的函数。 在返回到我的网格视图之前,该函数需要经过数百万个数据。

我在col1中调用函数两次,在col2中调用一次。 无论如何,我只能调用该函数一次并将它用于两个属性,这将非常方便吗? 任何关于此的帮助都会很棒。 谢谢。

'header'=&gt;'Phrase Used',
'htmlOptions'=&gt; array  ('style'=&gt;'width:10px; text-align:center;'),
'value'=&gt; function($ data){
 if($ data-&gt; usedBankPhrase($ data-&gt  ; bank_id)== 1){echo“&lt; span class = \”translated-badge \“title = \”Used \“&gt; u&lt; / span&gt;”;} 
 else {echo“&lt; span class =  \“badge \”style = \“background-color:red!important; padding:2px; \”title = \“未使用\”&gt; nu&lt; / span&gt;“;} 
'过滤器'  =&gt;'',
'删除'=&gt;  array(
'visible'=&gt;'!$ data-&gt; usedBankPhrase($ data-&gt; bank_id);',
'imageUrl'=&gt;  Yii :: app() - &gt; request-&gt; baseUrl。'/ images / icons / cross.png',
'options'=&gt; array('class'=&gt;'full-bank-delete',  'id'=&gt;'\'remove-banker  -  \'。$ data-&gt; bank_id'),//按钮标记的HTML选项。
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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) {...
    点赞 评论

相关推荐 更多相似问题