Reputation: 62
HomePage.java
Table table = new Table();
List<String> listOfString = (List<String>) table.getList(); //accessing list from Table.java
System.out.println("listOfString" + listOfString); //List -[name, abc, id, 101, photo, true, name, pqr, id, 102, photo, true, name, bcd, id, 103, photo, false ]
List<UserList> listOfBean = new ArrayList<UserList>();
System.out.println("list of my Bean" + listOfBean);
for(int i=0;i<listOfString.size()-1;i+=2)
{
UserBean mybean = new UserBean(listOfString.get(i), listOfString.get(i+1));
listOfBean.add(mybean);
System.out.println("list of loop bean:" + listOfBean);
}
System.out.println("list of bean:" + listOfBean);
add(new ListView<UserBean>("listview", listOfBean) {
protected void populateItem(ListItem<UserBean> item) {
UserList myBean1 = item.getModelObject();
item.add(new Label("name", myBean1.getName()));
item.add(new Label("id", myBean1.getId()));
}
});
UserBean.java
public class UserList {
private String name1;
private String type1;
private Boolean photo;
Table table = new Table("table");
List<String> list1 = (List<String>) table.getList();
public UserBean(String name1,String type1)
{
super();
this.name1 = name1;
this.type1 = type1;
}
public UserBean(String name1,String type1,Boolean photo)
{
super();
this.name1 = name1;
this.type1 = type1;
this.setPhoto(photo);
}
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public String getType1() {
return type1;
}
public void setType1(String type1) {
this.type1 = type1;
}
public Boolean getPhoto() {
return photo;
}
public void setPhoto(Boolean photo) {
this.photo = photo;
}
}
HomePage.html
<table class="table">
<tr wicket:id="listview2">
<td><span wicket:id="name"></span></td>
<td><span wicket:id="id"></span></td>
</tr>
</table>
I am accessing the List values in HomePage.java from Table.java(which converts json in the Database into a LIST). However with the above code..I am able to successfully add the rows dynamically without changing the code but not the Columns dynamically. Each time I need to define a constructor UserBean.java and instance in HomePage.java for adding column.
Can anyone please suggest an alternative method of how to add Columns dynamically in ListView for display in Wicket Table.
I am just a beginner.Any help would be appreciated.Thankyou in advance.
EDIT-
Table.java
String query="select row_to_json(t) from "
+ "(select array_to_json(array_agg(row_to_json(t))) as schema from (select name,type,photo from sample) t ) t";
JsonToWidget widget=new JsonToWidget();
JSONObject json=widget.getJsonForTableContent(query);
json.toString();
System.out.println(json);
JSONArray jsonarray = new JSONArray();
jsonarray.toString();
Iterator<String> iter=json.keys();
JSONArray array = json.getJSONArray("schema");
System.out.println("array"+array);
while(iter.hasNext())
{
String key = iter.next();
System.out.println("key:"+key);
JSONArray value = json.getJSONArray(key);
System.out.println("value"+value); // prints [{"name":"pqr","photo":false,"id":"101"},{"name":"bcd","photo":true,"id":"102"},{"name":"sadl","photo":false,"id":"103"},{"name":"123","photo":true,"id":"104"},{"name":"1234","photo":true,"id":"105"},{"name":"abc","photo":true,"id":"106"}]
for(int i=0;i<value.length();i++)
{
Iterator it = value.getJSONObject(i).keys();
while(it.hasNext())
{
String k = (String) it.next(); //prints keys in json
String v = value.getJSONObject(i).get(k).toString(); //prints values in json
list.add(k);
list.add(v);
}
}
System.out.println("array list:"+list); //prints - [name, pqr,photo, false, name, bcd, photo, true, name, sadl, photo, false, name, 123, photo, true, name, 1234 , photo, true, name, abc, photo, true]
}
}
public List<String> getList()
{
return list;
}
}
Is there any other approach of using JSON key-value pairs directly in add ListView(i.e read json in add ListView in HomePage.java) for display in Wicket Table without using Beans and creating instances so as to add Columns and rows dynamically from JSON from the Database.
Upvotes: 0
Views: 1332
Reputation: 484
From what I read in your previous question, I think that you want to do something like this:
HomePage.html
<table class="table">
<tr wicket:id="listview2">
<td><span wicket:id="name"></span></td>
<td><span wicket:id="id"></span></td>
<td><span wicket:id="hasPhoto"></span></td>
</tr>
</table>
UserBean.java
public class UserBean {
private String name;
private String id;
private boolean hasPhoto;
public UserBean(String name, String id, boolean hasPhoto) {
super();
this.name = name;
this.id = id;
this.hasPhoto = hasPhoto;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public boolean isHasPhoto() {
return hasPhoto;
}
public void setHasPhoto(boolean hasPhoto) {
this.hasPhoto = hasPhoto;
}
}
HomePage.java
Table table = new Table();
List<String> listOfString = (List<String>) table.getList();
List<UserBean> listOfUserBean = new ArrayList<UserBean>();
for (int i = 1; i < listOfString.size(); i += 6) {
UserBean userBean = new UserBean(listOfString.get(i),
listOfString.get(i + 2), Boolean.parseBoolean(listOfString
.get(i + 4)));
listOfUserBean.add(userBean);
}
add(new ListView<UserBean>("listview2", listOfUserBean) {
protected void populateItem(ListItem<UserBean> item) {
UserBean userBean = item.getModelObject();
item.add(new Label("name", userBean.getName()));
item.add(new Label("id", userBean.getId()));
item.add(new Label("hasPhoto", String.valueOf(userBean
.isHasPhoto())));
}
});
Upvotes: 1
Reputation: 17513
You need a more flexible data structure than a Java bean.
The simplest solution is to use List<List<Object>>
instead. I.e. each element in the list would be another list. The first inner list would bring the columns' names. The rest elements would bring the cell values.
I hope you get the idea! Good luck!
Upvotes: 1