Callback for Yii2's GridView CheckboxColumn

1.7k views Asked by At

I have a GridView (Yii2) and one of the columns is a boolean type of data. I want to be able to toggle that value and have it saved in my database.

I need a callback for that, but I don't see CheckboxColumn having one. How can I achieve this?

2

There are 2 answers

4
Blizz On

Don't go looking too far. Just use the checkboxOptions-property of your column setup to add a specific class to all checkboxes. Then you can use a jQuery event to listen to changes and report them back:

$('.checkbox-column').change(function(e) {
   var checked = $(this).is(':checked');
   $.ajax('route/target', {data: {id: $(this).closest('tr').data('key'), checked: checked}});   
});

Yii's GridView normally renders a data-key-attribute for every row (on the <tr>) that you can use to identify the actual record to update.

As an alternative: $('input:checkbox', $('#w0')).change() could also work, assuming you don't want the extra class and that the GridView is your first widget.

0
ScaisEdge On

All the GridView column can have a callback function. you can set the value attribute of the every singole column with result of the callback function.