0

I want to hide yii2 gridview empty column if value is null but my code is not working.

I have tried this code:

[
 'attribute'=>'division',
 'value'=>'divisionName.name',
 'visible' => function ($data) {
  if ('divisionName.name' == NULL) {
   return '0'; // or return true;
  } else {
   return '1'; // or return false;
  }
 },
]

And then tried this:

[
 'attribute'=>'division',
 'value'=>'divisionName.name',
 'visible' => function ($data) {
  if ('divisionName.name' == NULL) {
   return  true;
  } else {
   return  false;
  }
 },
]

Above both line of codes not working.

If divisionName.name equal to null then hide entire column from gridview, what is wrong with this code?

2
  • This is totally confused, IMO. You cannot set a function for 'visible', only true or false is allowed. And this property is only evaluated once, it does not check for each row if there is a value or not and decides whether the column should be shown or not. And 'divisionName.name' == NULL is false - you compare a string with NULL. And if(...) return true; else return false; ... don't you know that this is absolutely superfluous? Commented Jul 12, 2016 at 12:28
  • @robsch in 'visible' attrinute you can assingn alos the result of a test .. Commented Jul 12, 2016 at 12:37

1 Answer 1

1

Seems you have also a logical problem because you would show a null value and hide a not null value ..

The right solution is based on an check for the hide/show condition before the widget is showed

$showDivision = myFunctioForShow(...);

[
     'attribute'=>'division',
      .....
      'visible' => $showDivision ,
] ,   
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks I will try.
'division' == null is always false.
@robsch why 'division' == NULL is always false?
Don't you compare a string with NULL?
@robsch in this case 'division' si not a string but a key taht the gridview widget should convert the related column value to the proper value .. 'division' is not a literal string but the key for column gridview ..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.