Vaadin:带有表单的自定义布局 [文本字段标题和所需指示符]

发布于 2024-11-11 18:38:15 字数 670 浏览 0 评论 0原文

我们正在开发一些Form,但是在Vaadin Forms中使用CustomLayout。我们在 HTML 中使用了 Field 标签,并且想要隐藏 TextField 标题。还想在文本字段的左侧添加必填属性。

图形设计师和开发人员之间的任务是分离的,因此自定义布局对我们来说很方便,因此我们现在不能使用任何其他布局。

目前我的测试屏幕如下,[在表单中仅添加了两个字段以进行自定义布局] 正如您所看到的,“hello”标题位于文本框的顶部,我想将其完全删除。此标题仅用于说明目的,否则我将其设为空,但表格中仍然有空间。我还想让所需的指示器与文本框对齐。

我做了一个 Form Field 工厂并尝试了很多选择但没有成功。

如果需要任何特定的 CSS 更改,请清楚地告诉我如何添加它,因为我是 CSS 新手。根据之前对论坛的讨论,我在 TextField 中添加了以下样式,但没有成功,

myLabel.setStyleName("inline");

并使用 CSS:

.inline, .inline div { display: inline; }

在此处输入图像描述 [我的情况是我将其添加到 TextField 对象中}

We are developing some Form, but using CustomLayout in Vaadin Forms. we used Label of Field in HTML and want to suppress the TextField Caption. Also want to make required attribute in the left side of text field.

There is separation of tasks between graphics designer and developer, so Custom layout is convenient for us, so we cannot use any other layout right now.

Currently my test screen is as follows, [added only two fields in Form for custom layout]
As you can see that "hello" caption is top of Text Box, which I want to completely removed. This caption is just for illustration purpose, otherwise I make it empty, but still there is space in table. Also I want to make required indicator aligned with Text box.

I made a Form Field factory and tried many options but no luck.

if there is any specific CSS changes required please clearly tell me how to add it since I am new on CSS. based on previous discussion of Forum I added following style in my TextField without any sucess,

myLabel.setStyleName("inline");

and using CSS:

.inline, .inline div { display: inline; }

enter image description here
[my case I am adding it into TextField object }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青萝楚歌 2024-11-18 18:38:15

“还想在文本字段的左侧设置必需的属性”如果你想让字段成为必需的,你应该为其设置属性 .setRequired(true);

如果您想手动处理表单中的所有元素位置,您应该执行以下操作:

public class YourFormextends Form{

private YourForm() {

        setImmediate(true);
        setValidationVisible(false);
        setValidationVisibleOnCommit(false);
        setValidationVisible(false);
        setValidationVisibleOnCommit(false);
        setWriteThrough(false);

        VerticalLayout formLayout = createFormLayout();
        setLayout(formLayout);

        setFormFieldFactory(yourFormFieldFactory());
    }

public static VerticalLayout createFormLayout() {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);

        final VerticalLayout formErrorLayout = new VerticalLayout();
        final VerticalLayout formFieldLayout = formFieldLayout();

        layout.addComponent(formErrorLayout, CREATE_FORM_ERROR_LAYOUT_INDEX);
        layout.addComponent(formFieldLayout, CREATE_FORM_FIELD_LAYOUT_INDEX);

        return layout;
    }

    private static VerticalLayout formFieldLayout() {
        VerticalLayout rootLayout = new VerticalLayout();
        rootLayout.setSpacing(true);
        rootLayout.setMargin(false, false, true, false);
        GridLayout layout = new GridLayout();

        layout.setRows(1);
        layout.setColumns(3);

        rootLayout.addComponent(layout);

        return rootLayout;

    }

private FormFieldFactory activityFormFieldFactory() {
    FormFieldFactory factory = new FormFieldFactory() {

        private static final long serialVersionUID = 1L;

        @SuppressWarnings("unchecked")
        @Override
        public Field createField(Item item, Object propertyId, Component uiContext) {
            String fieldName = (String) propertyId;
            if (fieldName.equals(FIELD_NAME_HELLO)) {

                if (EDIT_MODE) {
                    TextField textField = new TextField(FIELD_NAME_HELLO);
                    textField.setRequired(true);
                    return textField;
                }
            }
        }
    }

@Override
protected void attachField(Object propertyId, Field field) {
    String fieldName = (String) propertyId;

    VerticalLayout formFieldLayout = getFormFieldLayout();

        if (fieldName.equals(FIELD_NAME_HELLO)) {
            GridLayout layout = (GridLayout) formFieldLayout.getComponent(0);
            layout.addComponent(fieldCaption, 0, 0);
            layout.addComponent(field, 1, 0);
layout.addComponent(fieldDescription, 2, 0);
        }
}
}


}

"Also want to make required attribute in the left side of text field" if you want make field required you should set for it property .setRequired(true);

If you want handle all element position in your form manually you should do something like this:

public class YourFormextends Form{

private YourForm() {

        setImmediate(true);
        setValidationVisible(false);
        setValidationVisibleOnCommit(false);
        setValidationVisible(false);
        setValidationVisibleOnCommit(false);
        setWriteThrough(false);

        VerticalLayout formLayout = createFormLayout();
        setLayout(formLayout);

        setFormFieldFactory(yourFormFieldFactory());
    }

public static VerticalLayout createFormLayout() {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);

        final VerticalLayout formErrorLayout = new VerticalLayout();
        final VerticalLayout formFieldLayout = formFieldLayout();

        layout.addComponent(formErrorLayout, CREATE_FORM_ERROR_LAYOUT_INDEX);
        layout.addComponent(formFieldLayout, CREATE_FORM_FIELD_LAYOUT_INDEX);

        return layout;
    }

    private static VerticalLayout formFieldLayout() {
        VerticalLayout rootLayout = new VerticalLayout();
        rootLayout.setSpacing(true);
        rootLayout.setMargin(false, false, true, false);
        GridLayout layout = new GridLayout();

        layout.setRows(1);
        layout.setColumns(3);

        rootLayout.addComponent(layout);

        return rootLayout;

    }

private FormFieldFactory activityFormFieldFactory() {
    FormFieldFactory factory = new FormFieldFactory() {

        private static final long serialVersionUID = 1L;

        @SuppressWarnings("unchecked")
        @Override
        public Field createField(Item item, Object propertyId, Component uiContext) {
            String fieldName = (String) propertyId;
            if (fieldName.equals(FIELD_NAME_HELLO)) {

                if (EDIT_MODE) {
                    TextField textField = new TextField(FIELD_NAME_HELLO);
                    textField.setRequired(true);
                    return textField;
                }
            }
        }
    }

@Override
protected void attachField(Object propertyId, Field field) {
    String fieldName = (String) propertyId;

    VerticalLayout formFieldLayout = getFormFieldLayout();

        if (fieldName.equals(FIELD_NAME_HELLO)) {
            GridLayout layout = (GridLayout) formFieldLayout.getComponent(0);
            layout.addComponent(fieldCaption, 0, 0);
            layout.addComponent(field, 1, 0);
layout.addComponent(fieldDescription, 2, 0);
        }
}
}


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文