百里香多个选择编辑 [英] thymeleaf multiple selected on edit

查看:105
本文介绍了百里香多个选择编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全改变了这个问题,在这里 Avnish的巨大帮助! 汤姆将我送往正确的方向,谢谢汤姆!

I am totally changing this question, as part of it was answered here with great help of Avnish! Tom sent me to the right direction so thank you Tom!

我的问题是我不知道在编辑时如何告诉Thymeleaf预选对象元素.

My problem is that I do not know how to tell Thymeleaf to preselect object elements when editing it.

让我告诉你:

此解决方案有效:

<select class="form-control" id="parts" name="parts" multiple="multiple">
    <option th:each="part : ${partsAtribute}"
            th:selected="${servisAttribute.parts.contains(part)}"
            th:value="${part.id}"
            th:text="${part.name}">Part name</option>
</select>

我已经尝试过了:

<select class="form-control" th:field="*{parts}" multiple="multiple">
    <option th:each="part : ${partsAtribute}"
            th:field="*{parts}"
            th:value="${part.id}"
            th:text="${part.name}">Part name</option>
</select>

不起作用.我也尝试过这个:

did not work. I also tried this:

<select class="form-control" th:field="*{{parts}}" multiple="multiple">
    <option th:each="part : ${partsAtribute}"
            th:field="*{parts}"
            th:value="${part.id}"
            th:text="${part.name}">Part name</option>
</select>

也不起作用.我尝试从选项标签中删除th:field="*{parts}",结果相同.

did not work either. I have tried removing th:field="*{parts}" from the option tag, same result..

如果我将th:value更改为${part},它可以工作,但是它不会发送回ID字符串,例如[2,4,5,6,...],而是发送回Part实例,例如[Part @ 43b45j,Part @ we43y7,...] ...

If I change th:value to ${part} it works, but then it does not send back string of ids like [2,4,5,6,...], but Part instances like [Part@43b45j, Part@we43y7,...]...

更新:我只是注意到,如果只选择了一部分,这将起作用:

UPDATE: I just noticed that this works if only one part is selected:

<select class="form-control" th:field="*{parts}" multiple="multiple">
    <option th:each="part : ${partsAtribute}"
            th:field="*{parts}"
            th:value="${part.id}"
            th:text="${part.name}">Part name</option>
</select>

如果选择了多个部分,则无法使用...

If multiple parts are selected, it does not work...

推荐答案

在Thymeleaf论坛上进行讨论之后,我在上实现了一个完整的工作示例 https://github.com/jmiguelsamper/thymeleafexamples-selectmultiple

After discussion on the Thymeleaf forum, I implemented a full working example at https://github.com/jmiguelsamper/thymeleafexamples-selectmultiple

我认为最终代码的唯一问题是必须使用双括号语法来调用conversionService:

I think that the only problem with your final code is that you have to use double bracket syntax to invoke the conversionService:

th:value="${{part}}"

在您的Part类中实现适当的equals()和hashcode()方法以确保适当的比较也很重要.

It is also important to implement proper equals() and hashcode() methods in your Part class to assure proper comparison.

我希望我的示例将来能帮助其他遇到类似问题的用户.

I hope my example helps other users with similar problems in the future.

这篇关于百里香多个选择编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆