How do I populate a drop down with a list using thymeleaf and spring

前端 未结 5 1897
天涯浪人
天涯浪人 2020-11-29 06:26

I need to populate a drop down with all the values within a list of strings.

Controller Class

@RequestMapping(value = \"/generateIds\", method = Requ         


        
相关标签:
5条回答
  • 2020-11-29 07:07

    I have implemented a similar where i need to populate the dropdown from the database. For that I retrieved the data from the controller as below

    @GetMapping("/addexpense")
    public String addExpense(Expense expense,Model model){
        model.addAttribute("categories",categoryRepo.findAll());
        return "addExpense";
    }
    

    Then in the thymeleaf I have used the below code.

    <div class="form-group col-md-8">
    <label  class="col-form-label">Category </label>
    <select  id="category" name="category" th:field="*{category.id}" >
    <option th:each="category : ${categories}" th:value="${category.id}" th:utext="${category.name}"/>
    </select>
    </div>
    
    0 讨论(0)
  • 2020-11-29 07:13

    First thank to your question and answer! I've done with this solution.

    My Model

    @Entity
    @Table(name = "test")
    public class Test {
    
        @Id
        private String testCode;
        private String testName;
        private int price;
    
        public Test() {}
    
        public Test(String testCode, String testName, int price) {
            this.testCode = testCode;
            this.testName = testName;
            this.price = price;
        }
    
        public String getTestCode() {
            return testCode;
        }
    
        public String getTestName() {
            return testName;
        }
    
        public int getPrice() {
            return price;
        }
    }
    

    My View

        List<Test> test = new ArrayList<>();
        model.addAttribute("test", test);
        List<Test> tests = testRepository.findAll();
        model.addAttribute("tests", tests);
    

    My HTML

    <div class="col-lg-3" th:object="${test}">
        <select class="form-control" id="testOrder" name="testOrder">
            <option value="">Select Test Order</option>
            <option th:each="test : ${tests}"
                    th:value="${test.testCode}"
                    th:text="${test.testCode}+' : '+${test.testName}"></option>
        </select>
    </div>
    

    My Result

    Image - tymeleaf dropdown from model

    0 讨论(0)
  • 2020-11-29 07:19

    You only need this code in your view.

    List<Test> tests = testRepository.findAll();
    model.addAttribute("tests", tests);
    
    0 讨论(0)
  • 2020-11-29 07:20

    This is how I populate the drop down list.I think it may help to you get some idea about it.

    Controller

    List<Operator> operators =  operatorService.getAllOperaors()
    model.addAttribute("operators", operators);
    

    Model

      @Entity
      @Table(name = "operator")
      public class Operator {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        @JsonIgnore
        private Long id;
    
        @NotBlank(message="operator Name cannot be empty")
        @Column(name = "operator_name", nullable = false)
        private String operatorName;
    
        getters and setters ...
    
    }   
    

    View

    <div class="form-group blu-margin">
        <select class="form-control" th:field="${operator.opeId}"  id="dropOperator">
        <option value="0">select operator</option>
        <option th:each="operator : ${operators}" th:value="${operator.id}" th:text="${operator.operatorName}"></option>
        </select>
    </div>
    
    0 讨论(0)
  • 2020-11-29 07:22

    For generating dropdown for the list of String returned in model you just need to use these lines. You don't need to create any model class with extra getter and setter methods. Your code is correct, you just missed the variable name for storing the value returned in countryName list in th:each.

    <select th:field="*{countryName}">
    <option value="">Select Country</option>
    <option th:each="country : ${countryName}" th:value="${country}" th:text="${country}"></option>
    </select>
    
    0 讨论(0)
提交回复
热议问题