0

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 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Uuh. This could work, but it certainly seems ugly. I'll open an issue about that on Yii2's github.
I guess I disagree with you on that point. The framework can't support every possible scenario so some of the things have to be added manually. Besides, I doubt that a solution directly in Yii would yield a less ugly result as the principle will always be the same: capture the event, report via ajax.
I'm sure a framework shouldn't support every possible scenario, but, imho, a checkbox callback on a editable gridview is not some obscure corner case, but rather a very common thing.
Guess that depends what sort of sites you write. In the 18 years that I've been doing this professionally I've only needed this 1 time. Not that common for me I guess :)
0

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.

Comments

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.