55import com .vaadin .annotations .Theme ;
66import com .vaadin .annotations .VaadinServletConfiguration ;
77import com .vaadin .data .util .BeanItemContainer ;
8+ import com .vaadin .server .FontAwesome ;
89import com .vaadin .server .VaadinRequest ;
910import com .vaadin .server .VaadinServlet ;
1011import com .vaadin .ui .*;
12+ import com .vaadin .ui .themes .ValoTheme ;
1113
1214import java .util .List ;
1315
@@ -23,24 +25,42 @@ public class MyUI extends UI {
2325
2426 private CustomerService service = CustomerService .getInstance ();
2527 private Grid grid = new Grid ();
28+ private TextField filterText = new TextField ();
2629
2730 @ Override
2831 protected void init (VaadinRequest vaadinRequest ) {
2932 final VerticalLayout layout = new VerticalLayout ();
3033
34+ filterText .setInputPrompt ("filter by name..." );
35+ filterText .addTextChangeListener (e -> {
36+ grid .setContainerDataSource (new BeanItemContainer <>(Customer .class , service .findAll (e .getText ())));
37+ });
38+
39+ Button clearFilterTextBtn = new Button (FontAwesome .TIMES );
40+ clearFilterTextBtn .setDescription ("Clear the current filter" );
41+ clearFilterTextBtn .addClickListener (e -> {
42+ filterText .clear ();
43+ updateList ();
44+ });
45+
46+ CssLayout filtering = new CssLayout ();
47+ filtering .addComponents (filterText , clearFilterTextBtn );
48+ filtering .setStyleName (ValoTheme .LAYOUT_COMPONENT_GROUP );
49+
3150 grid .setColumns ("firstName" , "lastName" , "email" );
32- // add Grid to the layout
33- layout .addComponents (grid );
51+ // add the filter and the Grid to the layout
52+ layout .addComponents (filtering , grid );
3453
3554 updateList ();
3655
3756 layout .setMargin (true );
57+ layout .setSpacing (true );
3858 setContent (layout );
3959 }
4060
4161 public void updateList () {
4262 // fetch list of Customers from service and assign it to Grid
43- List <Customer > customers = service .findAll ();
63+ List <Customer > customers = service .findAll (filterText . getValue () );
4464 grid .setContainerDataSource (new BeanItemContainer <>(Customer .class , customers ));
4565 }
4666
0 commit comments