lundi 2 avril 2018

How to make check box in each row selected by default in Vaadin table?

I am new to Vaddin. How to make check box in each row selected by default in Vaadin table? I have gone through some Vaadin example, which specifies by writing "field.setValue(true)" will make check boxes in each row selected. But the solution is not working for me. Kindly help me to find out where I am going wrong with below code. Here I have used DefaultFieldFactory to generate fields for each row.

public class ReviewTimesheetTable extends ViewComponent {

private Map<ReviewTimesheetTableDto, HashMap<String, AbstractField<?>>> tableItem = new HashMap<ReviewTimesheetTableDto, HashMap<String, AbstractField<?>>>();
BeanItemContainer<ReviewTimesheetTableDto> data = new BeanItemContainer<ReviewTimesheetTableDto>(
        ReviewTimesheetTableDto.class);
private Table table;

public void init() {
    // Create a data source and bind it to a table
    table = new Table("", data);
    table.addStyleName("generateColumnTable");
    table.setWidth("100%");
    table.setPageLength(table.getItemIds().size());
    table.setVisibleColumns(new Object[] { "chkBox", "taskDate", "employeeId", "taskId", "crJiraId",
            "taskDesc","hour","minute"/*, "Delete" */});

    table.setColumnHeader("chkBox", "Select");
    table.setColumnHeader("taskDate", "Task Date");
    table.setColumnHeader("employeeId", "Employee ID");
    table.setColumnHeader("taskId", "Task ID");
    table.setColumnHeader("crJiraId", "Subtask ID");
    table.setColumnHeader("taskDesc", "Task Description");
    table.setColumnHeader("hour", "Hour");
    table.setColumnHeader("minute", "Minute");

    table.setEditable(true);

    table.setTableFieldFactory(new ImmediateFieldFactory());
}
public class ImmediateFieldFactory extends DefaultFieldFactory {

    @Override
    public Field<?> createField(Container container, Object itemId,
            Object propertyId, Component uiContext) {
        final ReviewTimesheetTableDto timesheetDetail = (ReviewTimesheetTableDto) itemId;

        Map<String, AbstractField<?>> tableRow = null;

        if(tableItem.get(timesheetDetail) == null)
        {
            tableItem.put(timesheetDetail,
                    new HashMap<String, AbstractField<?>>());

        }
        tableRow = tableItem.get(timesheetDetail);

        if ("taskId".equals(propertyId)) {
            GComboBox box = new GComboBox();
            tableRow.put("taskId", box);
            setTaskValues(box, timesheetDetail);
            //addTaskListener(box);
            box.setData(timesheetDetail);
            if(timesheetDetail != null && timesheetDetail.getTaskId() != null) {
                box.setDescription(timesheetDetail.getTaskId().getValue());
            }
            if(isSubmit){
                box.setReadOnly(true);
            }

            if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
                box.setReadOnly(false);
            }
            return box;
        }else if ("crJiraId".equals(propertyId)) {
            TextField field = new TextField();
            field.setEnabled(true);
            field.setWidth("200px");
            field.setNullRepresentation("");
            if(isSubmit){
                field.setReadOnly(true);
            }
            if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
                field.setReadOnly(false);
            }
            field.setMaxLength(50);

            field.setDescription(timesheetDetail.getCrJiraId());
            tableRow.put("crJiraId", field);
            return field;
        }else if ("taskDesc".equals(propertyId)) {
            TextField field = new TextField();
            field.setEnabled(true);
            field.setWidth("300px");
            field.setNullRepresentation("");
            field.setReadOnly(false);
            tableRow.put("taskDesc", field);
            field.setMaxLength(2000);
            field.setData(timesheetDetail);
            addDetailPopupForTaskDesc(field, null);

            if(timesheetDetail.getTaskDesc() != null){
                field.setDescription(timesheetDetail.getTaskDesc());
            }
            field.setReadOnly(true);
            if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
                field.setReadOnly(false);
            }
            return field;
        }else if ("hour".equals(propertyId)) {
            GComboBox box = new GComboBox();
            box.setWidth("60px");
            tableRow.put("hour", box);
            box.addValueChangeListener(addHourListener(box));
            setHourValues(box, timesheetDetail);
            box.setData(timesheetDetail);
            if(isSubmit){
                box.setReadOnly(true);
            }
            if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
                box.setReadOnly(false);
            }
            return box;
        }else if ("minute".equals(propertyId)) {
            GComboBox box = new GComboBox();
            box.setWidth("80px");
            tableRow.put("minute", box);
            box.addValueChangeListener(addMinuteListener(box));
            setMinuteValues(box, timesheetDetail);
            box.setData(timesheetDetail);
            if(isSubmit){
                box.setReadOnly(true);
            }
            if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
                box.setReadOnly(false);
            }
            return box;
        }else if ("employeeId".equals(propertyId)) {
            /*TextField field = new TextField();
            field.setEnabled(true);
            field.setWidth("100%");
            field.setNullRepresentation("");
            field.setReadOnly(true);
            field.setMaxLength(50);
            field.setDescription(timesheetDetail.getEmpId());
            tableRow.put("empId", field);
            return field;*/

            GComboBox box = new GComboBox();
            tableRow.put("employeeId", box);
            setEmpValues(box, timesheetDetail);
            box.setData(timesheetDetail);
            if(timesheetDetail != null && timesheetDetail.getEmployeeId() != null) {
                box.setDescription(timesheetDetail.getEmployeeId().getValue());
            }
            if(isSubmit){
                box.setReadOnly(true);
            }
            if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
                box.setReadOnly(false);
            }
            /*if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
                box.setReadOnly(false);
            }*/
            return box;
        }else if ("taskDate".equals(propertyId)) {
            DateField field = new DateField();
            field.setEnabled(true);
            field.setWidth("110px");
            field.setReadOnly(true);
            if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
                field.setReadOnly(false);
            }
            field.setDateFormat("dd-MM-yyyy");

            tableRow.put("taskDate", field);
            return field;
        }else if ("chkBox".equals(propertyId)) {
            CheckBox field = new CheckBox();
            field.setEnabled(true);
            field.setWidth("30px");
            field.setDescription(SHAConstants.SELECT);

            **field.setValue(true);**

            tableRow.put("chkBox", field);
            return field;
        }else {
            Field<?> field = super.createField(container, itemId,
                    propertyId, uiContext);

            if (field instanceof TextField)
                field.setWidth("100%");
            field.setEnabled(true);
            return field;
        }
    }
}

Your contribution will be very much helpful. Thanks in advance. Kindly find Output of above code below. Output of above code below




Aucun commentaire:

Enregistrer un commentaire