symbiote / silverstripe-gridfieldextensions

A collection of useful grid field components.
BSD 3-Clause "New" or "Revised" License
96 stars 126 forks source link

GridFieldEditableColumns & Checkboxes #77

Open clyonsEIS opened 9 years ago

clyonsEIS commented 9 years ago

Using this component for a many_many_extrafield (Boolean or Int) with a CheckboxField allows me to set the value, but not unset.

I've tested with both array & closure, same results. Is this a bug in GFE?

clyonsEIS commented 9 years ago

More info - checkboxes aren't sent when they're empty, so this fails: https://github.com/silverstripe-australia/silverstripe-gridfieldextensions/blob/master/code/GridFieldEditableColumns.php#L61

Thus the saveInto is never executed. I'm unsure of the best solution since the test is surely needed for some cases.

camfindlay commented 9 years ago

@clyonsEIS I have been playing with the code you passed on to me. I stripped it right back to a proof of concept which is here: https://gist.github.com/camfindlay/93372c1d9fee069431b1

The strange thing I have found is that the above code acts I believe as you are after, selecting or unselecting the featured many_many_extrafield is persistent however if I change the type of field to a ReadOnlyField at https://gist.github.com/camfindlay/93372c1d9fee069431b1#file-foopage-php-L36 then the functionality breaks. Seems if wants all the fields passed as readable entities when you have editable columns on which seems weird.

Anyway, hopefully you can use the above code to further prove your point and perhaps see if you can get a resolve... I think this is a bug or at very least is not acting how I would expect it to given the code passed in.

I don't know this module well enough to provide much more to go on. @nyeholt would you perhaps have any clues?

clyonsEIS commented 9 years ago

@camfindlay Right you are, thanks very much for following up on this. My GridFieldConfig includes the GridFieldDataColumns component, which supplies read-only fields. Replacing the component completely with EditableColumns works, but it would be better to allow selective readonly / editable columns.

thomasbnielsen commented 9 years ago

I just ran into this problem, wasted a few hours trying to figure out what was going on. Editable columns seems tp need atleast one other textfield (that is not readonly) when saving an unchecked checkboxfield.

camfindlay commented 9 years ago

Might be worth submitting some docs about this.

RVXD commented 8 years ago

Still running into this issue. I'm using a gridfield with checkboxes in multiple columns, no editable text fields. It is not possible to save unchecked checkboxes. Any solutions? It looks like this: screen shot 2016-04-01 at 17 28 53

RVXD commented 8 years ago

Workaround: It works if you use optionsetfield instead of checkbox:

    $SConfig->getComponentByType('GridFieldEditableColumns')->setDisplayFields(array(
                'Active' => array(
                    'callback' => function ($record, $column, $grid) {
                        return new OptionsetField($column,$column,array(1=>'Yes',0=>'No'));
                    },
                    'title' => 'Actief'
                ) // ...... etc

Looks like this: screen shot 2016-04-01 at 17 45 33

Added some styling to the optionset to make it compact:

Requirements::customCSS('.ss-gridfield .optionset li{width:auto;margin-right:10px;}');
ElGabbu commented 8 years ago

@RVXD thanks man that works like a charm :)

mlewis-everley commented 7 years ago

I have just run into this issue as well, using an OptionsetField like @RVXD suggests works but does take up more space (which isn't always ideal) :-s

micschk commented 5 years ago

4+ years but just ran into this problem with a CheckboxSetField as well. Worked around it by appending a hidden input when a checkbox gets unchecked (so a '' value gets submitted).

(Old fashioned jQuery & Entwine:)

// make empty/unchecked checkboxfields (inside GridFieldEditableColumns) submit ''
// instead of not submit anything
$('.ss-gridfield-item input[type="checkbox"]').entwine({
    onchange: function (e) {
        // insert/remove a hidden input with value '' to submit unchecked checkboxes
        if(this.prop( "checked" )){
            this.siblings('.checkbox_zero_input').remove();
        } else {
            $('<input>').attr({
                class: 'checkbox_zero_input',
                type: 'hidden',
                name: this.attr('name')
            }).insertAfter(this);
        }
        this._super();
    }
});
ScopeyNZ commented 5 years ago

Hey @micschk are you still using SS3 or have you managed to come across this issue in SS4 as well?

micschk commented 5 years ago

SS3, but should work for 4 as well (could be that the class/selector needs to be slightly changed).

ScopeyNZ commented 5 years ago

Sorry I was trying to understand whether you've encountered the reported bug in SS4.

Unfortunately, SilverStripe 3 has entered limited support in June 2018. This means we'll only be fixing critical bugs and security issues for SilverStripe 3 going forward.

You can read the SilverStripe Roadmap for more information on our support commitments.

If this bug is still prevalent in SS4 then I'm happy to reopen this issue 🙂

RVXD commented 5 years ago

In SS4 this issue is still prevalent.

RVXD commented 4 years ago

Still an issue in 2020.

Solution for SS 4:

Add this to config.yml:

SilverStripe\Admin\LeftAndMain:
  extra_requirements_javascript:
    - 'app/client/dist/js/cms.js'

Create file app/client/dist/js/cms.js with content as @micschk suggested:

(function ($){
  $('.ss-gridfield-item input[type="checkbox"]').entwine({
    onchange: function (e) {
      // insert/remove a hidden input with value '' to submit unchecked checkboxes
      if(this.prop( "checked" )){
        this.siblings('.checkbox_zero_input').remove();
      } else {
        $('<input>').attr({
          class: 'checkbox_zero_input',
          type: 'hidden',
          name: this.attr('name')
        }).insertAfter(this);
      }
      this._super();
    }
  });
})(jQuery);