胸腺替换Th:野外以符合TH:选定

发布于 2025-02-04 05:20:23 字数 2433 浏览 2 评论 0原文

感谢您通过我的帖子去世。我也在Stackoverflow和Google中搜索了我的以下代码:

我的html代码:

<form th:action="@{/surgerywaitinglist/saveToWaitinList}"
            th:object="${waitinglistDTO}" method="POST">
.
.
.    
<select name="departmentName"
                                        
            th:field="*{department.departmentName}"
            th:with="departmentName = ${waitinglistDTO.department.departmentName}"
            class="form-control" id="departmentJS">
        
        <option value="" th:selected="selected" 
              th:disabled="disabled">select option
        </option>
                                                
        <option th:each="department: ${departments}"
                th:value="${department.departmentName}"
                th:text="${department.departmentName}"> 
        </option>
        
    </select>
.
.
.
<input type="submit" value="Save" class="btn btn-primary" />
</form>

形成其他帖子,例如这篇文章我发现Th:field and th:选择不工作;实际上,需要用其他东西替换字段。请注意,th:对象拥有另一个对象(来自部门类)...

我的DTO类:

public class WaitinglistDTO {

    private Long waitingListId;
    @NotBlank(message = "Please enter a procedure")
    private String waitingListProcedure;
    private String waitingListDiagnosis;
    private Long waitingListPatientId;
    private Long waitingListSurgeonId;
    private Long waitingListDepartmentId;
    @DateTimeFormat(iso=ISO.DATE)
    private Date waitingListAdditionDate;
    @DateTimeFormat(iso=ISO.DATE)
    private Date waitingListActualBookingDate;
    
    
    private Patient patient;
    private Surgeon surgeon;
    private Department department;

您能帮我弄清楚吗?

许多thanxxxxx :)

... ... 更新:

以下图像说明了它的外观,但是,默认选项应为选择选项,该选项应以某种方式被禁用

这是我应用,您可以看到预选的选项是第一个选项,而不是选择选项选项:)

I appreciate your passing by my post. I have searched here in StackOverFlow and google as well to fix my following code:

My HTML code:

<form th:action="@{/surgerywaitinglist/saveToWaitinList}"
            th:object="${waitinglistDTO}" method="POST">
.
.
.    
<select name="departmentName"
                                        
            th:field="*{department.departmentName}"
            th:with="departmentName = ${waitinglistDTO.department.departmentName}"
            class="form-control" id="departmentJS">
        
        <option value="" th:selected="selected" 
              th:disabled="disabled">select option
        </option>
                                                
        <option th:each="department: ${departments}"
                th:value="${department.departmentName}"
                th:text="${department.departmentName}"> 
        </option>
        
    </select>
.
.
.
<input type="submit" value="Save" class="btn btn-primary" />
</form>

form other posts like this post I found out that th:field and th:selected do not work together; in fact, th:field needs to be replace with something else. Notice that the th:object holds another object (from Department class) ...

My DTO class:

public class WaitinglistDTO {

    private Long waitingListId;
    @NotBlank(message = "Please enter a procedure")
    private String waitingListProcedure;
    private String waitingListDiagnosis;
    private Long waitingListPatientId;
    private Long waitingListSurgeonId;
    private Long waitingListDepartmentId;
    @DateTimeFormat(iso=ISO.DATE)
    private Date waitingListAdditionDate;
    @DateTimeFormat(iso=ISO.DATE)
    private Date waitingListActualBookingDate;
    
    
    private Patient patient;
    private Surgeon surgeon;
    private Department department;

Could you help me figure this out?

Many thanxxxxx :)

...
Update:

The following image explains how it should look,,, however, the default option should be the select option which should be somehow disabled
enter image description here

This is the result I get when I apply the suggested code of Rafael da Silva ,, you can see the pre-selected option is the first option rather than the select option option :)
enter image description here

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

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

发布评论

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

评论(2

请叫√我孤独 2025-02-11 05:20:23

您可以这样做:

<select name="departmentName" th:field="*{department.departmentName}" th:with="departmentName = ${waitinglistDTO.department.departmentName}" class="form-control" id="departmentJS">
    <option selected="selected" disabled="disabled">select option</option>
    <option th:each="department : ${departments}" th:value="${department.departmentName}" th:text="${department.departmentName}"></option>
</select>

这对我有用。

you can do like this:

<select name="departmentName" th:field="*{department.departmentName}" th:with="departmentName = ${waitinglistDTO.department.departmentName}" class="form-control" id="departmentJS">
    <option selected="selected" disabled="disabled">select option</option>
    <option th:each="department : ${departments}" th:value="${department.departmentName}" th:text="${department.departmentName}"></option>
</select>

this worked for me.

跨年 2025-02-11 05:20:23

我不明白为什么您会将所选的选项添加到空白选项中,如果您的模型对象没有设置,并且如果采取正常行为,则始终选择第一个选项。 应该解决。

如果您希望选择始终以默认选项启动,即使模型具有另一个值或想将其设置为其他方式。您可以设置th:id =“*{dectiber.departmentName}” and th:name =“*{dectiber.departmentName}”,而不是手动handle th :选择

作为附带说明,无需使用th版本th:selected =“ Selected”如果使用静态值selectedselected =“选择在那种情况下会用

我没有工作电脑

代码编辑,而不会测试,因为

<select name="departmentName" th:name="*{department.departmentName}" th:id="*{department.departmentName}" class="form-control" id="departmentJS">
    <option selected disabled>select option</option>
    <option th:each="department : ${departments}" th:value="${department.departmentName}" th:text="${department.departmentName}"></option>
</select>

。想要填写现有数据,但默认为第一个选项

<select name="departmentName" th:field="*{department.departmentName}"  class="form-control" id="departmentJS">
    <option disabled>select option</option>
    <option th:each="department : ${departments}" th:value="${department.departmentName}" th:text="${department.departmentName}"></option>
</select>

,我不确定使用禁用的数据,但我认为它应该有效,就个人而言,我只能在控制器验证中选择它并在您的bean上执行@notnull。
th:使用注释我什么都看不到,我可以看到什么th:with用于定义一个新的变量,适用于嵌套在您定义它的Elment中的所有元素。例子:

<div th:with="newVar='someString'">
    <span th:text="${newVar}"></span>
</div>

I don't see why you would add the selected to the blank option, the first option is always selected if your model object has nothing set and if it does normal behaviour would be to have that item selected, which th:field should resolve.

In case you want the select to always start at the default option, even if the model has another value or want to set it some other way. you can set th:id="*{department.departmentName}" and th:name="*{department.departmentName}" and than manualy handle th:selected.

As a side note theres no need to use the th version th:selected="selected" if using static values just selected or selected="selected would sufice in that case

Edit with code, not tested as i don't have my work pc where i am.

If you want to always have the first option selected even when editing with previous data

<select name="departmentName" th:name="*{department.departmentName}" th:id="*{department.departmentName}" class="form-control" id="departmentJS">
    <option selected disabled>select option</option>
    <option th:each="department : ${departments}" th:value="${department.departmentName}" th:text="${department.departmentName}"></option>
</select>

If you want to fill with existing data but default to the first option

<select name="departmentName" th:field="*{department.departmentName}"  class="form-control" id="departmentJS">
    <option disabled>select option</option>
    <option th:each="department : ${departments}" th:value="${department.departmentName}" th:text="${department.departmentName}"></option>
</select>

I'm not 100% sure on the use of disabled but i think it should work, personally i would just leave it selectable and enforce a @notnull on your bean in controller validation.
the th:with annotation does nothing asfar i can see, what th:with is used for is defining a new variable for all elements nested inside the elment you define it on. example:

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