2
2
3
3
import com .vaadin .annotations .Theme ;
4
4
import com .vaadin .annotations .Title ;
5
- import com .vaadin .server .FontAwesome ;
5
+ import com .vaadin .data .provider .GridSortOrder ;
6
+ import com .vaadin .data .provider .QuerySortOrder ;
7
+ import com .vaadin .event .SortEvent ;
8
+ import com .vaadin .icons .VaadinIcons ;
6
9
import com .vaadin .server .VaadinRequest ;
10
+ import com .vaadin .shared .data .sort .SortDirection ;
7
11
import com .vaadin .spring .annotation .SpringUI ;
8
12
import com .vaadin .ui .Button ;
9
13
import com .vaadin .ui .Button .ClickEvent ;
10
- import com .vaadin .ui .TextField ;
11
14
import com .vaadin .ui .UI ;
12
15
import crud .backend .Person ;
13
16
import crud .backend .PersonRepository ;
14
- import org . springframework . beans . factory . annotation . Autowired ;
17
+ import java . util . List ;
15
18
import org .springframework .data .domain .PageRequest ;
16
19
import org .springframework .data .domain .Sort ;
17
20
import org .vaadin .spring .events .EventBus ;
20
23
import org .vaadin .viritin .button .ConfirmButton ;
21
24
import org .vaadin .viritin .button .MButton ;
22
25
import org .vaadin .viritin .components .DisclosurePanel ;
23
- import org .vaadin .viritin .fields .MTable ;
24
26
import org .vaadin .viritin .fields .MTextField ;
27
+ import org .vaadin .viritin .grid .MGrid ;
25
28
import org .vaadin .viritin .label .RichText ;
26
29
import org .vaadin .viritin .layouts .MHorizontalLayout ;
27
30
import org .vaadin .viritin .layouts .MVerticalLayout ;
32
35
public class MainUI extends UI {
33
36
34
37
private static final long serialVersionUID = 1L ;
35
-
38
+
36
39
PersonRepository repo ;
37
40
PersonForm personForm ;
38
41
EventBus .UIEventBus eventBus ;
39
-
40
- private MTable <Person > list = new MTable <>(Person .class )
42
+
43
+ private MGrid <Person > list = new MGrid <>(Person .class )
41
44
.withProperties ("id" , "name" , "email" )
42
45
.withColumnHeaders ("id" , "Name" , "Email" )
43
- .setSortableProperties ("name" , "email" )
46
+ // not yet supported by V8
47
+ //.setSortableProperties("name", "email")
44
48
.withFullWidth ();
45
-
46
- private TextField filterByName = new MTextField ()
47
- .withInputPrompt ("Filter by name" );
48
- private Button addNew = new MButton (FontAwesome .PLUS , this ::add );
49
- private Button edit = new MButton (FontAwesome . PENCIL_SQUARE_O , this ::edit );
50
- private Button delete = new ConfirmButton (FontAwesome . TRASH_O ,
49
+
50
+ private MTextField filterByName = new MTextField ()
51
+ .withPlaceholder ("Filter by name" );
52
+ private Button addNew = new MButton (VaadinIcons .PLUS , this ::add );
53
+ private Button edit = new MButton (VaadinIcons . PENCIL , this ::edit );
54
+ private Button delete = new ConfirmButton (VaadinIcons . TRASH ,
51
55
"Are you sure you want to delete the entry?" , this ::remove );
52
56
53
57
public MainUI (PersonRepository r , PersonForm f , EventBus .UIEventBus b ) {
54
58
this .repo = r ;
55
59
this .personForm = f ;
56
60
this .eventBus = b ;
57
61
}
58
-
62
+
59
63
@ Override
60
64
protected void init (VaadinRequest request ) {
61
65
DisclosurePanel aboutBox = new DisclosurePanel ("Spring Boot JPA CRUD example with Vaadin UI" , new RichText ().withMarkDownResource ("/welcome.md" ));
@@ -67,32 +71,32 @@ protected void init(VaadinRequest request) {
67
71
).expand (list )
68
72
);
69
73
listEntities ();
70
-
71
- list .addMValueChangeListener (e -> adjustActionButtonState ());
72
- filterByName .addTextChangeListener (e -> {
73
- listEntities (e .getText ());
74
+
75
+ list .asSingleSelect (). addValueChangeListener (e -> adjustActionButtonState ());
76
+ filterByName .addValueChangeListener (e -> {
77
+ listEntities (e .getValue ());
74
78
});
75
79
76
80
// Listen to change events emitted by PersonForm see onEvent method
77
81
eventBus .subscribe (this );
78
82
}
79
-
83
+
80
84
protected void adjustActionButtonState () {
81
- boolean hasSelection = list .getValue () != null ;
85
+ boolean hasSelection = ! list .getSelectedItems (). isEmpty () ;
82
86
edit .setEnabled (hasSelection );
83
87
delete .setEnabled (hasSelection );
84
88
}
85
-
86
- static final int PAGESIZE = 45 ;
87
-
89
+
88
90
private void listEntities () {
89
91
listEntities (filterByName .getValue ());
90
92
}
91
-
93
+
94
+ final int PAGESIZE = 45 ;
95
+
92
96
private void listEntities (String nameFilter ) {
93
97
// A dead simple in memory listing would be:
94
98
// list.setRows(repo.findAll());
95
-
99
+
96
100
// But we want to support filtering, first add the % marks for SQL name query
97
101
String likeFilter = "%" + nameFilter + "%" ;
98
102
list .setRows (repo .findByNameLikeIgnoreCase (likeFilter ));
@@ -101,40 +105,41 @@ private void listEntities(String nameFilter) {
101
105
// Spring Repository. This approach uses less memory and database
102
106
// resources. Use this approach if you expect you'll have lots of data
103
107
// in your table. There are simpler APIs if you don't need sorting.
104
- // list.lazyLoadFrom(
105
- // // entity fetching strategy
106
- // (firstRow, asc, sortProperty) -> repo.findByNameLikeIgnoreCase(
107
- // likeFilter,
108
- // new PageRequest(
109
- // firstRow / PAGESIZE,
110
- // PAGESIZE,
111
- // asc ? Sort.Direction.ASC : Sort.Direction.DESC,
112
- // // fall back to id as "natural order"
113
- // sortProperty == null ? "id" : sortProperty
114
- // )
115
- // ),
116
- // // count fetching strategy
117
- // () -> (int) repo.countByNameLike(likeFilter),
118
- // PAGESIZE
119
- // );
108
+ list .setDataProvider (
109
+ // entity fetching strategy
110
+ (sortOrder , offset , limit ) -> {
111
+ final List <Person > page = repo .findByNameLikeIgnoreCase (likeFilter ,
112
+ new PageRequest (
113
+ offset / limit ,
114
+ limit ,
115
+ sortOrder .isEmpty () || sortOrder .get (0 ).getDirection () == SortDirection .ASCENDING ? Sort .Direction .ASC : Sort .Direction .DESC ,
116
+ // fall back to id as "natural order"
117
+ sortOrder .isEmpty () ? "id" : sortOrder .get (0 ).getSorted ()
118
+ )
119
+ );
120
+ return page .subList (offset % limit , page .size ()).stream ();
121
+ },
122
+ // count fetching strategy
123
+ () -> (int ) repo .countByNameLike (likeFilter )
124
+ );
120
125
adjustActionButtonState ();
121
-
126
+
122
127
}
123
-
128
+
124
129
public void add (ClickEvent clickEvent ) {
125
130
edit (new Person ());
126
131
}
127
-
132
+
128
133
public void edit (ClickEvent e ) {
129
- edit (list .getValue ());
134
+ edit (list .asSingleSelect (). getValue ());
130
135
}
131
-
132
- public void remove (ClickEvent e ) {
133
- repo .delete (list .getValue ());
134
- list .setValue ( null );
136
+
137
+ public void remove () {
138
+ repo .delete (list .asSingleSelect (). getValue ());
139
+ list .deselectAll ( );
135
140
listEntities ();
136
141
}
137
-
142
+
138
143
protected void edit (final Person phoneBookEntry ) {
139
144
personForm .setEntity (phoneBookEntry );
140
145
personForm .openInModalPopup ();
@@ -145,5 +150,5 @@ public void onPersonModified(PersonModifiedEvent event) {
145
150
listEntities ();
146
151
personForm .closePopup ();
147
152
}
148
-
153
+
149
154
}
0 commit comments