Spring/MVC Thymeleaf th:selected not working

前端 未结 5 2081
盖世英雄少女心
盖世英雄少女心 2021-02-09 14:34

I am building an application using Spring, Hibernate and of course Thymeleaf for the views. The application is centered around an Activity domain class (which is literally an i

相关标签:
5条回答
  • 2021-02-09 14:43

    'th: field' automatically generates the 'id' and 'name' attributes, so if you want to use 'th: selected', you should remove 'th: field' and manually set them to work.

    I had the same problem and check the forum, 'th: selected' and 'th: field' does not work at the same time. check the forum

    <div class="row">
    <div class="input-field col s12">
        <select id="doc" name="doc" th:with="doc=*{doc}">
                        <option value="" th:disabled="disabled" selected="true"
                           th:text="${status==true}? 'Seleccione tipo de documento' : ${doc}">Seleccione
                           tipo de documento</option>
                        <option th:each="tipoDoc : ${tipoDocs}" th:text="${tipoDoc}"
                           th:value="${tipoDoc}" />
                  </select>
        <label>Documento</label>
    </div>
    

    0 讨论(0)
  • 2021-02-09 14:50

    This solutions is working for me

    <div class="form-group">
    <label>Employee's Status</label>
    <select id="statusid" name="statusid" th:field="*{statusid}" class="form-control col-md-7 col-xs-12"  required="true" />
    <option th:value="''" th:text="Select"></option>
    <option th:each="i : ${allStatus}" th:value="${i.id}" th:text="${i.statusname}" th:selected="${i.id} == ${employee.statusid}"></option>
    </select>
    </div>
    
    0 讨论(0)
  • 2021-02-09 14:53

    I know its a bit late,. but anyone searching for the solution...

    To use the *{fieldname} select option you should use the th:value with 2 curly brackets

    <select th:field="*{roadmap}" required>
    <option th:each="rm : ${roadmaps}" th:value="${{rm}}" th:text="${rm.title}">
    </option>
    </select>
    

    You need to add a formatter:

    public class RoadmapFormatter implements Formatter<Roadmap> {
        @Override
        public Roadmap parse(String id, Locale locale) throws ParseException {
            Roadmap r = new Roadmap();
            r.setId(Long.parseLong(id));
            return r;
        }
    
        @Override
        public String print(Roadmap r, Locale locale) {
            return String.valueOf(r.getId());
        }
    }
    

    Last but not least register the formatter in the configuration class:

    @Configuration
    public class Configuration implements WebMvcConfigurer {
    
        @Override
        public void addFormatters(FormatterRegistry formatterRegistry) {
            formatterRegistry.addFormatter(new RoadmapFormatter());
        }
    }
    
    0 讨论(0)
  • 2021-02-09 14:59

    I found that thymeleaf auto selected when I wanted to edit an item, it loaded form with predefined values, my code like this:

    <select th:field="*{type}" class="form-control" name="stockType">
    <option value="1">Type1</option>
    <option value="2">Type2</option>
    <option value="4">Type4</option></select>
    

    Works like a charm

    0 讨论(0)
  • 2021-02-09 15:02

    according to the thymeleaf forum (post) th:field doesn't work with th:selected

    try something like this (not tested)

    <select name="category">
        <option th:each="choice : ${allCategories}" 
         th:value="${choice.id}" 
         th:attr="choiceid=${choice.id}, categoryid=*{category.id}, showselected=(${choice.id} == *{category.id})" 
         th:selected="(${choice.id} == *{category.id})" 
         th:readonly="(${choice.id} == *{category.id})" 
         th:text="${choice.description}"></option>
    </select>
    
    0 讨论(0)
提交回复
热议问题