2016-06-09 13 views
7

Dizelerdeki tüm değerler ile bir açılır menüyü doldurmam gerekiyor. HTML için başlamak bu yüzden buThymeleaf ve yay kullanarak bir listeyle bir açılır listeyi nasıl doldurabilirim

<select th:field="*{countryName }"> 
    <option value="default">Select a country</option> 
    <option th:each="${countryName }" th:value="${countryName }"th:text="${countryName }"/> 
</select> 

ile başladığı yere

Kontrolör Sınıf

@RequestMapping(value = "/generateIds", method = RequestMethod.GET) 
public String launchPage(Model model) { 
    List<Municipality> municipalities = rCountryService.finaAllMunicipalities(); 
    //assume the list is populated with values 
List<String> countryName = new ArrayList<String>(); 
     for(int i=0; i<municipalities.size(); i++){ 
     countryName.add(municipalities.get(i).getCountry().toString()); 
    } 
    model.addAttribute("countryName ", countryName); 

    return "generateIds"; 
} 

benim html/kontrolör tüm öğeleri eklemek nasıl görünmesi gerektiği bilmiyordum listeden açılan seçenekler olarak mı?

Şimdiden teşekkürler!

cevap

7

Açılır listeyi böyle dolduruyorum. Bunun hakkında biraz fikir edinmenize yardımcı olabileceğini düşünüyorum.

Kontrolör

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

Modeli

@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 ... 

} 

Görünüm

<div class="form-group blu-margin"> 
    <select class="form-control" th:field="${operator.opeId}" id="dropOperator"> 
    <option value="0" th:text="select operator" ></option> 
    <option th:each="operator : ${operators}" th:value="${operator.id}" th:text="${operator.operatorName}"></option> 
    </select> 
</div> 
3

Sorunuzun ilk teşekkür ve cevap! Bu çözüm ile yaptım.

Benim Modeli

@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; 
    } 
} 

Benim Görünüm

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

Benim 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> 

Benim Sonucu

Image - tymeleaf dropdown from model

İlgili konular