TabularForm get context data to delete row

Hello,

I’m now programming with yii since a week and could solve almost all my problems here except one. I have a TabularForm where I have a delete button in to delete an attribute entry. The standard implementation here only removes the row from the gui, but not from the db. So I want to implement this by myself, but I cant figure out from where I get the id’s I need to do this. The callback gives me only the key (row id from the table).

here the code form the table:


echo TabularForm::widget([

    'dataProvider' => $dataProvider,

    'formName' => 'PersonsAssociationsMap',

    'checkboxColumn' => false,

    'actionColumn' => false,

    'id' => 'tablePersonsAssociationsMap',

    'attributeDefaults' => [

        'type' => TabularForm::INPUT_TEXT,

    ],

    'attributes' => [

        'id_persons' => [

            'label' => 'persons',

            'type' => TabularForm::INPUT_WIDGET,

            'widgetClass' => \kartik\widgets\Select2::className(),

            'options' => [

                'data' => \yii\helpers\ArrayHelper::map(\app\models\Persons::find()->orderBy('id')->asArray()->all(), 'id',

                    function($model, $defaultValue) {

                        return $model['lastname'].' '.$model['firstname'];

                    }),

                'options' => ['placeholder' => Yii::t('app', 'Choose Persons')],

            ],

            'columnOptions' => ['width' => '200px']

        ],

        'del' => [

            'type' => 'raw',

            'label' => '',

            'value' => function($model, $key) {

                return Html::a('<i class="glyphicon glyphicon-trash"></i>', '#', ['title' =>  Yii::t('app', 'Delete'),

                    'onClick' => 'delPersonsMap("'.$key.'","baseid","personid","Associations"); return false;',

                    'id' => 'tb-persons-associations-map-del-btn']);

            },

        ],

    ],

    'gridSettings' => [

        'panel' => [

            'heading' => false,

            'type' => GridView::TYPE_DEFAULT,

            'before' => false,

            'footer' => false,

            'after' => Html::button('<i class="glyphicon glyphicon-plus"></i>' . Yii::t('app', 'Add Associations Persons Map'),

                ['value' => Url::to('../persons/chooser?baseid='.$baseID.'&filter=&openerClassName=PersonsAssociationsMap&basename='.urlencode($baseName)), 'class' => 'btn btn-success kv-batch-create', 'id' => 'modalAddPersons'])

                //Html::button('<i class="glyphicon glyphicon-plus"></i>' . Yii::t('app', 'Add Persons Associations Map'), ['type' => 'button', 'class' => 'btn btn-success kv-batch-create', 'onClick' => 'addRowTbPersonsAssociationsMap()']),

        ]

    ]

]);

so perhaps somebody can help me here a little bit

with regards

Georg

Hi Georg, welcome to the forum.

The key is usually the primary key (i.e., "id") of the object representing the row.

I thought this also first, but this table is a cross table and there is no ‘id’ in. only one for the association and one for the person

Georg

Oh, I’m sorry. I failed to read your code carefully.

Is "delPersonMap" a javascript function that handles the deletion?

If so, you could pass the model’s id to that function and call some server action that updates db in it, I guess.

yes this is a js function, but the $model does not give me the expected ids back, I tried this first, looks like its not initialized in this place

Georg

Is the model not yet populated with the id of "id_persons" column when the grid is initialized?

Or, in other words, do you want to pass the id of "id_persons" to the delete function after the user has selected the person? If yes, I think you could use some jQuery selector to read it.

normally in the same line beside the trash icon is a selectbox where the entry is shown, so I would exspect, that I can get the data from this somehow only the model is not populated. To delete the entry I need two ids, while one of them is an id what is the same for all entries in the list I show (the association id). I tried to look into the dom to find the things I need but while im a newbie I could not find the right parts here to pull my data out

georg

ps. thanks for your help

I would try to set the unique "id" to every selecct2 in the "del" column … something like "del-$key-select" … in order to simplify the jQuery selector.