Vaadin InlineDateFormat解决步骤

时间:2016-11-11 17:12:57

标签: java vaadin

对于Vaadin的服务器端InlineDateField,可以将分辨率设置为分钟,如下所示:

timeField = new InlineDateField( caption );
timeField.setResolution(Resolution.MINUTE);

如何设置步骤,例如:我希望以15分钟的间隔获得分钟,而不是获得60分钟的完整列表。

https://vaadin.com/docs/-/part/framework/components/components-datefield.html

1 个答案:

答案 0 :(得分:0)

你不能用这个组件做到这一点。

但您可以将其与Resolution.DAY(默认)一起使用,并为小时和分钟选择器生成您自己的组合框。

示例组件:

public class MyDatePicker extends VerticalLayout {

    private InlineDateField dateField;
    private NativeSelect hourPicker;
    private NativeSelect minutePicker;

    public MyDatePicker(String caption) {
        setSizeUndefined();

        dateField = new InlineDateField(caption);
        hourPicker = new NativeSelect();
        minutePicker = new NativeSelect();

        HorizontalLayout hourLayout = new HorizontalLayout();
        hourLayout.setDefaultComponentAlignment(Alignment.MIDDLE_CENTER);
        Label colonLabel = new Label("<b>:</b>");
        colonLabel.setContentMode(ContentMode.HTML);
        hourLayout.addComponents(hourPicker, colonLabel, minutePicker);

        addComponents(dateField, hourLayout);
        setComponentAlignment(hourLayout, Alignment.MIDDLE_CENTER);

        setValues();
    }

    private void setValues() {
        hourPicker.setContainerDataSource(new BeanItemContainer<>(String.class, new ArrayList<>(Arrays.asList("01", "02", "...", "24"))));
        minutePicker.setContainerDataSource(new BeanItemContainer<>(String.class, new ArrayList<>(Arrays.asList("00", "15", "30", "45"))));

        hourPicker.select("01");
        minutePicker.select("00");
    }

    public Date getValue() {
        Date date = dateField.getValue();

        // set hour and minutes to date

        return date;
    }

}