mercredi 14 décembre 2016

Java - How to bound checkbox table column with database?

I have an editable Vaadin table which has a checkbox column.

When the condition of the item is getISACTIVE = 1 (Long type, taken from the database), I want the checkboxes to be checked.

On the other hand, when I change the value of the checkbox, I want the ISACTIVE condition to be changed accordingly (setISACTIVE=1 being checked and setISACTIVE=0 not being checked).
Until now, I tried to Map the checkboxes but still the checkboxes are not checked even though the condition =1.

Any idea what should I do?

    table.addGeneratedColumn("Is Active", new Table.ColumnGenerator() {

                private static final long serialVersionUID = 1L;
                @Override
                public Object generateCell(final Table source, final Object itemId,
                        Object columnId) {

                    final CheckBox checkBox = new CheckBox("");

    //ITERATE THROUGH DATABASE, THEN getPS_SECTION that is the ID Bean Property

                            for (int i = 0; i < list.size(); i++) {
                                PS_SECTION section = list.get(i);
                                Long sc = section.getPS_SECTION();

                                FillIt(sc,checkBox);

                                    if (section.getISACTIVE() == 1L) {                              
                                        value = true;
                                        CheckIt(sc,value);
                                    }
                                    else if(section.getISACTIVE() == 0L){
                                        value = false;
                                        CheckIt(sc,value);
                                    }
                            }

                    checkBox.addValueChangeListener(new ValueChangeListener() {                 
                        @Override
                        public void valueChange(ValueChangeEvent event) {

                            Boolean isTrue = checkBox.getValue();
                            if (isTrue){
                                isChecked = true;
                                Notification.show("Is activated" );

                               //setISACTIVE = 1
                            }
                            else{               
                                isChecked = false;
                                Notification.show("Is deactivated" );

                               //setISACTIVE = 0
                            }                           
                        }
                    });             
                    return checkBox;                
                }               
            }); 

            table.setTableFieldFactory(new DefaultFieldFactory() {

                public Field<?> createField(Container container, final Object itemId, Object propertyId, Component uiContext) {

                                if (CHECKBOX_COLUMN.equals(propertyId)) {
                                    CheckBox field1 = new CheckBox();   
                                    field1.setImmediate(true);
                                }   
                    return super.createField(container, itemId, propertyId, uiContext);
                }
            });

And here the methods FillIt() and CheckIt():

   private void CheckIt(Long id, Boolean value) {
        CheckBox checkBox = itemIdToCheckbox.get(id);
        checkBox.setValue(value);
    }

    private void FillIt(Long id, CheckBox checkBox) {
        itemIdToCheckbox.put(id,checkBox );     
    }

//When itemIdToCheckBox is

protected Map<Long, CheckBox> itemIdToCheckbox = new LinkedHashMap<Long, CheckBox>();

Aucun commentaire:

Enregistrer un commentaire