M
M
Michael2018-12-13 10:39:04
Yii
Michael, 2018-12-13 10:39:04

TreeGrid how to remove and leave buttons under certain condition?

Hello everybody! I am asking this question, is it possible to somehow write a condition so that the parent category "User's workplace" has buttons as they are now, but the child categories have only deletion and editing, without an add button?
5c120ae32353f270766116.png

<?= TreeGrid::widget([
    'columns' => [  
        'name',
        [
            'class' => 'yii\grid\ActionColumn',
            'contentOptions' => ['style' => 'white-space: nowrap; text-align: center; letter-spacing: 0.1em; max-width: 7em;'],
            'template' => ($data->parent_id) ? ('{add}') : ('{update} {delete} {add}'), //'{update} {delete} {add}'
            'buttons' => [
                'add' => function ($url, $model, $key)
                {
                    return Html::a('<span class="glyphicon glyphicon-plus"></span>', $url);
                },
            ]
        ],
    ],
]); ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-12-13
@Bally

Good morning.
You can try to do so. If the category does not have a parent id, then this is the parent category - the show button. If there is a parent id, that is, it is not equal to null, 0, or something else, which indicates that this is a child category - do not show the button.

'buttons' => [
       'add' => function ($url, $model, $key)
       {
           if($model->category_id == 0){
               return Html::a('<span class="glyphicon glyphicon-plus"></span>', $url);
           }
       },
 ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question