Why can't I change the value in GridView?

Alexander Kolovsky

Trying to replace the input value with the users table column status to blocked, if it is equal to 0 and active, if is 10.

 GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'username',
        'email:email',
        'status',
            'value' => function ($model){
                    return $model->status==10 ? "Active":"Blocked";
                },


        ['class' => 'yii\grid\ActionColumn'],
    ],
]);

But displays an error: array_merge(): Argument #2 is not an array What am I doing wrong, please tell me

arogachev

Attribute status should be declared like this:

[
    'attribute' => 'status',
    'value' => function ($model) {
         return $model->status == 10 ? 'Active' : 'Blocked';
    },
],

So the whole GridView will look like this:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'username',
        'email:email',
        [        
            'attribute' => 'status',
            'value' => function ($model) {
                return $model->status == 10 ? 'Active' : 'Blocked';
            },
        ],
        ['class' => 'yii\grid\ActionColumn'],
    ],
]) ?>

But another way of doing this is recommended.

Place in your model:

const STATUS_BLOCKED = 0;

const STATUS_ACTIVE = 10;

/**
 * @return array
 */
public static function getStatusesList()
{
    return [
        self::STATUS_BLOCKED => 'Blocked',
        self::STATUS_ACTIVE => 'Active',
    ];
}

/**
 * @return string
 */
public function getStatusLabel()
{
    return static::getStatusesList()[$this->status];
}

And now you can replace your closure content to this:

return $model->getStatusLabel();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I change the value in GridView?

From Dev

Why can't I change the value of a Json

From Dev

Why can't I change the value of String.Empty?

From Dev

Why can't I change the default value of a function after it was defined?

From Dev

why i can't change value of a text field by javascript?

From Java

Why can I change value of a constant in javascript

From Dev

Why can't I change objects in a vector?

From Dev

Why can't I change this BufferedImage?

From Dev

Why can't I change pointers for methods?

From Dev

Why can't I change toolbar textsize?

From Dev

Why can't I change this BufferedImage?

From Dev

Why I can´t change the wifi card

From Dev

I can't change the value of my variable

From Dev

I can't change the value of line_count at the right of pipe , why?

From Dev

I can't change the value of line_count at the right of pipe , why?

From Dev

Why I use list as a parameter of a function and the function can't change the value of the actual parameter but only the formal parameter?

From Dev

Why I can't change a variable's value inside an if-then statement?

From Dev

Why can't I assign value on didSet?

From Dev

Why can't I multiply a hash value?

From Dev

Why can't I select a listbox value?

From Dev

Why can't I get the input value?

From Dev

Why can't I change a set I'm iterating over?

From Dev

Why can't we change a particular value of a string in python

From Dev

Why I can change/reassigned a constant value that Instantiated from a class

From Dev

Why I can not change the value of global variable in WatchKit - swift?

From Dev

Why I can change/reassigned a constant value that Instantiated from a class

From Java

Why can't I store a value and a reference to that value in the same struct?

From Dev

I can change the iOS splash screen but I can't change the Android splash screen. Why?

From Dev

I can change the iOS splash screen but I can't change the Android splash screen. Why?

Related Related

  1. 1

    Why can't I change the value in GridView?

  2. 2

    Why can't I change the value of a Json

  3. 3

    Why can't I change the value of String.Empty?

  4. 4

    Why can't I change the default value of a function after it was defined?

  5. 5

    why i can't change value of a text field by javascript?

  6. 6

    Why can I change value of a constant in javascript

  7. 7

    Why can't I change objects in a vector?

  8. 8

    Why can't I change this BufferedImage?

  9. 9

    Why can't I change pointers for methods?

  10. 10

    Why can't I change toolbar textsize?

  11. 11

    Why can't I change this BufferedImage?

  12. 12

    Why I can´t change the wifi card

  13. 13

    I can't change the value of my variable

  14. 14

    I can't change the value of line_count at the right of pipe , why?

  15. 15

    I can't change the value of line_count at the right of pipe , why?

  16. 16

    Why I use list as a parameter of a function and the function can't change the value of the actual parameter but only the formal parameter?

  17. 17

    Why I can't change a variable's value inside an if-then statement?

  18. 18

    Why can't I assign value on didSet?

  19. 19

    Why can't I multiply a hash value?

  20. 20

    Why can't I select a listbox value?

  21. 21

    Why can't I get the input value?

  22. 22

    Why can't I change a set I'm iterating over?

  23. 23

    Why can't we change a particular value of a string in python

  24. 24

    Why I can change/reassigned a constant value that Instantiated from a class

  25. 25

    Why I can not change the value of global variable in WatchKit - swift?

  26. 26

    Why I can change/reassigned a constant value that Instantiated from a class

  27. 27

    Why can't I store a value and a reference to that value in the same struct?

  28. 28

    I can change the iOS splash screen but I can't change the Android splash screen. Why?

  29. 29

    I can change the iOS splash screen but I can't change the Android splash screen. Why?

HotTag

Archive