Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    SelectItem multiple items not checked!

    On a ListGrid cell.

    Code:
    public FormItem getEditor(ListGridEditorContext context) {
                final SelectItem selectItemGrid = new SelectItem();
                selectItemGrid.setShowTitle(false);
                selectItemGrid.setMultiple(true);
                selectItemGrid.setMultipleAppearance(MultipleAppearance.PICKLIST);
                selectItemGrid.setMultipleValueSeparator(",");
                selectItemGrid.setValueMap("Read", "Write");
                selectItemGrid.setValues(getDisplayValue().split(","));
                return selectItemGrid;
            }
    setValue() with single item works. Multiple values doesn't work. The checkboxes are all empty.

    Version is 12.1p. Also tried 13.0. 13.0 is actually much worse!

    #2
    The problem with 13.0 is that with addCellSavedHandler

    public void onCellSaved(CellSavedEvent event) {

    The event.getNewValue() is garbage. On 12.1p the value is a String. On 13.0 the value is an Object and there is no way to figure out.

    Comment


      #3
      Hi there, could you explain what you’re trying to do here, specifically, is this field bound to a DataSource, and if so, what is the underlying storage (see also dataSourceField.multipleStorage)?

      Note that, in general, the value of a multiple:true field is expected to be an Array/List. It kind of looks like you are possibly trying to make things work where the value is really just a String? That would not be expected to work, and would definitely cause APIs to behave in a surprising manner if attempted.

      Comment


        #4
        Originally posted by Isomorphic View Post
        Hi there, could you explain what you’re trying to do here, specifically, is this field bound to a DataSource, and if so, what is the underlying storage (see also dataSourceField.multipleStorage)?

        Note that, in general, the value of a multiple:true field is expected to be an Array/List. It kind of looks like you are possibly trying to make things work where the value is really just a String? That would not be expected to work, and would definitely cause APIs to behave in a surprising manner if attempted.
        No, it is not bound to a DataSource. It is just an editor of a cell in ListGrid.

        As you can see, in my code selectItemGrid.setValues takes a String[], which is exactly the return type of String.split.

        The problem is. it only works when passed with an array with ONE element. If I pass an array with two values, nothing is checked!

        It should be very easy to reproduce.

        BTW, in your showcase example, you only setValues with one value, that's why the demo works.

        Comment


          #5
          Your code is incomplete, and we wouldn't know how to flesh it out into runnable code, because it's got usage issues and it's not clear what you're hoping to do here.

          Specifically, normally, editors in a grid edit the value that is already in the Record. So it's not valid to call setValue() in this context, because you are just providing an editor for the record values - the grid already has the record value and will apply it to the editor once it's created.

          If you wanted to simulate a user edit, you could do that via setEditValue(), but it would not be valid to do this in the midst of creating an editor, either. You might do it at some other point in the lifecycle, such as the RowEditorEnter event.

          Finally, you haven't posted any data or any fields, so we don't know whether you are properly storing values for this field as Arrays (vs maybe a comma-delimited string - invalid), and we don't know whether your field is actually declared multiple:true (required) and has a correct "type" attribute.

          We would recommend reading the Grid Editing overview:

          https://smartclient.com/smartclient-...group..editing

          .. and looking into all of the issues we've pointed out above.

          Then, if you still think you've got a framework issue, please post complete, runnable code.

          Comment


            #6
            Appreciate your help very much! But I still couldn't even make your sample code work.

            https://smartclient.com/smartgwt/sho...m_editing_cell

            Could you advise me on how to make the checkbox work with two items set initially?

            Comment


              #7
              If you take a look at our previous response, you'll see that we provided multiple ways of setting multiple values to be checked.

              If you're still having trouble, then, as previously indicated, you should:

              1) tell us which of the suggested approaches you used, and..

              2) show complete code for your attempt

              Comment


                #8
                Below is the code of your showcase sample at https://smartclient.com/smartgwt/sho...m_editing_cell .

                Code:
                new NameValueRecord(6, "SelectItem Editor", "Dog"),
                You set the value "Dog". So when the user clicks and invoke the editor, the item "Dog" is checked?

                How can I set a value of two items? Such as "Cat" and "Dog"? I tried "Cat,Dog", but nothing is checked when the editor is invoked.

                Nothing is checked, even if I insert a line like the code below.

                Code:
                selectItemMultipleGrid.setValueMap("Cat", "Dog", "Giraffe", "Goat", "Marmoset", "Mouse");
                selectItemMultipleGrid.setValues("Cat", "Dog");

                Code:
                /*
                 * Smart GWT (GWT for SmartClient)
                 * Copyright 2008 and beyond, Isomorphic Software, Inc.
                 *
                 * Smart GWT is free software; you can redistribute it and/or modify it
                 * under the terms of the GNU Lesser General Public License version 3
                 * as published by the Free Software Foundation. Smart GWT is also
                 * available under typical commercial license terms - see
                 * http://smartclient.com/license
                 *
                 * This software is distributed in the hope that it will be useful,
                 * but WITHOUT ANY WARRANTY; without even the implied warranty of
                 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
                 * Lesser General Public License for more details.
                 */
                
                /*
                 * Smart GWT (GWT for SmartClient)
                 * Copyright 2008 and beyond, Isomorphic Software, Inc.
                 *
                 * Smart GWT is free software; you can redistribute it and/or modify it
                 * under the terms of the GNU Lesser General Public License version 3
                 * is published by the Free Software Foundation. Smart GWT is also
                 * available under typical commercial license terms - see
                 * http://smartclient.com/license
                 *
                 * This software is distributed in the hope that it will be useful,
                 * but WITHOUT ANY WARRANTY; without even the implied warranty of
                 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
                 * Lesser General Public License for more details.
                 */
                
                import java.util.Date;
                
                import com.smartgwt.client.types.ListGridEditEvent;
                import com.smartgwt.client.types.MultipleAppearance;
                import com.smartgwt.client.widgets.Canvas;
                import com.smartgwt.client.widgets.Slider;
                import com.smartgwt.client.widgets.form.fields.CheckboxItem;
                import com.smartgwt.client.widgets.form.fields.DateItem;
                import com.smartgwt.client.widgets.form.fields.FormItem;
                import com.smartgwt.client.widgets.form.fields.IntegerItem;
                import com.smartgwt.client.widgets.form.fields.PasswordItem;
                import com.smartgwt.client.widgets.form.fields.SelectItem;
                import com.smartgwt.client.widgets.form.fields.SliderItem;
                import com.smartgwt.client.widgets.form.fields.TextItem;
                import com.smartgwt.client.widgets.grid.ListGrid;
                import com.smartgwt.client.widgets.grid.ListGridEditorContext;
                import com.smartgwt.client.widgets.grid.ListGridEditorCustomizer;
                import com.smartgwt.client.widgets.grid.ListGridField;
                import com.smartgwt.client.widgets.grid.ListGridRecord;
                
                import com.google.gwt.core.client.EntryPoint;
                
                public class GridCellEditorCustomizerSample implements EntryPoint {
                
                    public void onModuleLoad() {
                
                        final ListGrid countryGrid = new ListGrid();
                        countryGrid.setWidth(300);
                        countryGrid.setHeight(340);
                        countryGrid.setShowAllRecords(true);
                
                        ListGridField nameField = new ListGridField("name", "Name");
                        nameField.setWidth("*");
                        nameField.setCanEdit(false);
                
                        ListGridField valueField = new ListGridField("value", "Value Field", 170);
                
                        countryGrid.setFields(nameField, valueField);
                
                        countryGrid.setData(getData());
                        countryGrid.setEditorCustomizer(new ListGridEditorCustomizer() {
                            public FormItem getEditor(ListGridEditorContext context) {
                                ListGridField field = context.getEditField();
                                if (field.getName().equals("value")) {
                                    NameValueRecord record = (NameValueRecord) context.getEditedRecord();
                                    int id = record.getID();
                                    switch (id) {
                                        case 1:
                                            TextItem textItem = new TextItem();
                                            textItem.setShowHint(true);
                                            textItem.setShowHintInField(true);
                                            textItem.setHint("Some Hint");
                                            return textItem;
                                        case 2:
                                            return new PasswordItem();
                                        case 3:
                                            return new DateItem();
                                        case 4:
                                            CheckboxItem cbi = new CheckboxItem();
                                            cbi.setShowLabel(false);
                                            return cbi;
                                        case 5:
                                            IntegerItem integerItem = new IntegerItem();
                                            integerItem.setMask("###");
                                            return integerItem;
                                        case 6:
                                            SelectItem selectItemMultipleGrid = new SelectItem();
                                            selectItemMultipleGrid.setShowTitle(false);
                                            selectItemMultipleGrid.setMultiple(true);
                                            selectItemMultipleGrid.setMultipleAppearance(MultipleAppearance.PICKLIST);
                                            selectItemMultipleGrid.setValueMap("Cat", "Dog", "Giraffe", "Goat", "Marmoset", "Mouse");
                                            return selectItemMultipleGrid;
                                        case 7:
                                            Slider sliderProperties = new Slider();
                                            sliderProperties.setMargin(2);
                                            SliderItem sliderItem = new SliderItem();
                                            sliderItem.setMaxValue(10);
                                            sliderItem.setWidth(170);
                                            sliderItem.setAutoChildProperties("slider", sliderProperties);
                                            return sliderItem;
                                        default:
                                            return context.getDefaultProperties();
                                    }
                                }
                                return context.getDefaultProperties();
                            }
                        });
                
                        countryGrid.setCanEdit(true);
                        countryGrid.setEditEvent(ListGridEditEvent.CLICK);
                        countryGrid.setEditByCell(true);
                
                        countryGrid.draw();
                    }
                
                    private ListGridRecord[] getData() {
                        return new ListGridRecord[]{
                                new NameValueRecord(1, "String Editor", "some string"),
                                new NameValueRecord(2, "Password Editor", "donkeykong"),
                                new NameValueRecord(3, "Date Editor", new Date()),
                                new NameValueRecord(4, "Boolean Editor", Boolean.FALSE),
                                new NameValueRecord(5, "Masked Int Editor", 5),
                                new NameValueRecord(6, "SelectItem Editor", "Dog"),
                                new NameValueRecord(7, "Slider Editor", 7)
                        };
                    }
                
                    public static class NameValueRecord extends ListGridRecord {
                
                        public NameValueRecord(int id, String name, Object value) {
                            setID(id);
                            setName(name);
                            setValue(value);
                        }
                
                        public void setID(int id) {
                            setAttribute("ID", id);
                        }
                
                        public int getID() {
                            return getAttributeAsInt("ID");
                        }
                
                        public void setValue(Object value) {
                            setAttribute("value", value);
                        }
                
                        public Object getValue() {
                            return getAttributeAsObject("value");
                        }
                
                        public void setName(String name) {
                            setAttribute("name", name);
                        }
                
                        public String getName() {
                            return getAttribute("name");
                        }
                
                    }
                
                }
                Last edited by weiqj; 26 May 2023, 19:55.

                Comment


                  #9
                  We have already explained that setValue()/setValues() is invalid to call in this context, and explained the correct way to do things:

                  Specifically, normally, editors in a grid edit the value that is already in the Record. So it's not valid to call setValue() in this context, because you are just providing an editor for the record values - the grid already has the record value and will apply it to the editor once it's created.

                  If you wanted to simulate a user edit, you could do that via setEditValue(), but it would not be valid to do this in the midst of creating an editor, either. You might do it at some other point in the lifecycle, such as the RowEditorEnter event.
                  So again, either:

                  1. put the array of values in the Record, as the field value

                  .. or ..

                  2. call setEditValue() passing at array, if you want to simulate a user edit. But note above about when setEditValue() would be valid to call.

                  Comment


                    #10
                    Thanks, set the value attribute on the ListGridRecord with String[] solved the problem.

                    Comment


                      #11
                      If you are talking about calling SelectItem.setValue(), again, this is not a correct solution, and is not supported.

                      This means that, even if it appears to be "working" right now for the limited scenario you're testing, it's very likely to be broken for scenarios you haven't looked at, and is also likely to break unexpectedly if you change something seemingly unrelated about your app, or be broken by patches designed to fix other problems - that's what it means to take an unsupported approach.

                      So to be clear: no one should be taking the approach. Correct, supported approaches have been repeatedly explained above, which are also simpler, and actually make sense in terms of the grid editing use case.

                      Comment

                      Working...
                      X