0

Model:

public $FnGdiff;

public function getFnGdiff() {
    return $this->FnG - $this->fd;
}

ModelSearch:

public function rules() {
    return [
        [['fnGdiff'], 'safe'],
    ];
}

now if I add fnGdiff to gridview, there is always a number(?!) by default in the textfield where we can do filtering. It is zero, -6, etc. Is it a feature, or a bug, or have I forgotten something to adjust? Many thanks in advance!

SOLUTION:

Model:

public function getFnGdiff() {
    return $this->FnG - $this->fd;
}

ModelSearch:

public $fnGdiff;

public function rules() {
    return [
        [['fnGdiff'], 'safe'],
    ];
}
...

(So this strange number is disappeared, however it's not possible to filter a calculated virtual attribute this way, you have to select such a field from DB in order to be able to do that)

user2511599
  • 796
  • 1
  • 13
  • 38

2 Answers2

0

If you add fnGdiff t your gridView .. you invoke the function getFnGdiff() .. that seems return 0 by default ..

 could be You want show  $FnGdiff

be careful with naming convention for function and vars

    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        .....
        'FnGdiff',   // $FnGdiff content
        'fnGdiff',  // function getFnGdiff() result 

and you should move the code for vars and function to searchModel

ModelSearch

 public $FnGdiff;

 public function getFnGdiff() {
   return $this->FnG - $this->fd;
}

and remove this code from model ..

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • I assume I have to add `fnGdiff` because `FnGdiff` is empty. – user2511599 Mar 20 '17 at 10:08
  • what do you mean with your comment .. obvious FnGdiff .. is empty .. explain better .. .. asnwer updated with sume suggestion .. – ScaisEdge Mar 20 '17 at 10:10
  • If not like this, then how should I define a calculated virtual attribute, to be able to filter it, without any numbers by default in the textfield? – user2511599 Mar 20 '17 at 10:23
  • here you can find some sample .. http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/ ... but is another question respect to your ..above .. – ScaisEdge Mar 20 '17 at 10:35
  • Thanks I have seen this article already, and I don't find any details that would make it clear what have I done wrong, or what should I do otherwise to make this strange behavior disappear. – user2511599 Mar 20 '17 at 10:38
  • the part related to calculated value for fullname is what you should eval deeply – ScaisEdge Mar 20 '17 at 10:39
  • I have update the answer adding a brief (but important ) suggestion .. – ScaisEdge Mar 20 '17 at 10:42
  • if I remove this code from Model, then I get **UnknownPropertyException** – user2511599 Mar 20 '17 at 11:39
  • then you are referring to the calculated value in model ... i suggest you to take another deep exam of the link http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/ .. – ScaisEdge Mar 20 '17 at 11:54
  • You are right: ModelSearch: public $fnGdiff2; rules: [['fnGdiff2'], 'safe'], Model: function. Now it works as expected. Thank you very much! – user2511599 Mar 20 '17 at 12:34
0

Filter filed is render for search model and their attributes passed to GridView. When you create that model, virtual attribute $FnGdiff is set by specified value when model is initial before send query. Your attribute $FnGdiff is deafault set 0 ($this->FnG - $this->fd, in new model it is: null - null = 0), and that value is render in text filter field. So you should return specified value only for no new record. Try this:

public function getFnGdiff() {
    return $this->isNewRecord ? '' : $this->FnG - $this->fd;
}
Marcin Gordel
  • 321
  • 1
  • 8