2013-02-15 25 views
5

Tôi có một combobox vaadin được làm đầy với một containerdatasourcethêm văn bản vào một combobox với một nguồn dữ liệu

setContainerDataSource(container); 

bây giờ tôi muốn chèn một văn bản tĩnh nơi nào đó trong danh sách kết quả.


Ví dụ:

Một Combobox được làm đầy với một container và các mục nhập đầu tiên bật lên trong danh sách kết quả là một số loại tiêu đề:

Người:
Thomas S .
Lucas B.
Alex X.

Tôi có thể đạt được điều đó bằng cách thao tác với vùng chứa hoặc com bobox?

Tôi vừa cố gắng đặt nguồn vùng chứa và thêm Chuỗi/Nhãn qua addItem() vào ComboBox, nhưng điều đó dường như không hoạt động. Tôi rất mới mẻ với điều này, vì vậy tôi không biết làm thế nào để tiếp tục.

+0

Nó có thể hữu ích nếu bạn đăng những gì bạn đã thử hoặc nghiên cứu cho đến nay. –

+0

tôi chỉ cố gắng để thiết lập nguồn container và thêm một String/Label thông qua addItem() vào ComboBox, nhưng điều đó dường như không hoạt động. Tôi rất mới mẻ với điều này, vì vậy tôi không biết làm thế nào để tiếp tục. – luuksen

Trả lời

0

Bạn nên thực hiện thay đổi trong vùng chứa (ví dụ: thêm mục ...) và gọi setContainerDataSource (container) một lần nữa trên combobox (để nó được truyền tới máy khách).

+0

đã cố gắng rằng tốt, tôi có một Container đầy người và đã thử container.addItem (new Sring ("test")); "kiểm tra" sẽ không được hiển thị trong danh sách, nhưng phần còn lại của vùng chứa sẽ. – luuksen

+0

vui lòng thêm mã của bạn vào văn bản câu hỏi ... –

2

Nếu mã của bạn là tương tự như sau:

BeanItemContainer<Person> container = new BeanItemContainer<Person>(Person.class); 
container.addAll(myPersonList); 
ComboBox combobox = new ComboBox(); 
combobox.setContainerDataSource(container); 
combobox.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY); 
combobox.setItemCaptionPropertyId("name"); // the person's name field will be shown on the UI 

// imho if you want to add a static text (String) into a container 
// which populated with Person objects then you have to make a fake Person object 
Person staticText = new Person(); 
staticText.setName("My static text"); 
combobox.addItem(staticText); 
// if you want to specify the index of the item, add them one by one in for cycle 
// and insert the static text item in the appropritate place 
6

Nếu bạn đang sử dụng ComboBox như ngay lập tức và không muốn "Person:" để được xử lý như một người thực sự, bạn có thể sử dụng để setNullSelectionItemId xác định người giả là một đối tượng giả thực sự. Tuy nhiên, giải pháp này có giới hạn mà bạn chỉ có thể thêm một đối tượng giả.

Đây là ví dụ của tôi có thêm "Person:" ở đầu danh sách và xử lý nó dưới dạng giá trị null. Lưu ý rằng tôi đang sử dụng Vaadin 7.

import com.vaadin.data.Property; 
import com.vaadin.data.Property.ValueChangeEvent; 
import com.vaadin.data.util.BeanItemContainer; 
import com.vaadin.server.VaadinRequest; 
import com.vaadin.ui.AbstractSelect; 
import com.vaadin.ui.ComboBox; 
import com.vaadin.ui.Notification; 
import com.vaadin.ui.UI; 
import com.vaadin.ui.VerticalLayout; 

/** 
* The Application's "main" class 
*/ 
@SuppressWarnings("serial") 
public class MyVaadinUI extends UI { 

    @Override 
    protected void init(VaadinRequest request) { 
     final VerticalLayout layout = new VerticalLayout(); 
     layout.setMargin(true); 
     setContent(layout); 

     BeanItemContainer<Person> container = new BeanItemContainer<Person>(Person.class); 
     Person nullPerson = new Person(0, "Person:"); 
     container.addBean(nullPerson); 
     container.addBean(new Person(1, "Django")); 
     container.addBean(new Person(2, "Schultz")); 

     ComboBox combobox = new ComboBox(); 
     combobox.setImmediate(true); 
     combobox.setNullSelectionItemId(nullPerson); // Define the null person as a dummy. 
     combobox.setContainerDataSource(container); 
     combobox.setItemCaptionMode(AbstractSelect.ItemCaptionMode.PROPERTY); 
     combobox.setItemCaptionPropertyId("name"); // the person's name field will be shown on the UI 
     combobox.addValueChangeListener(new Property.ValueChangeListener() { 
      @Override 
      public void valueChange(ValueChangeEvent event) { 
       // Will display 'null selected' when nullPerson is selected. 
       Notification.show(event.getProperty().getValue() + " selected"); 
      } 
     }); 

     layout.addComponent(combobox); 
    } 
} 
+0

Tuyệt vời nhưng tôi muốn biết cách lấy id của người đó – Bourkadi

Các vấn đề liên quan