Wicket - 在wicket表中动态添加列

时间:2016-12-06 11:55:07

标签: java html json listview wicket

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>

我从Table.java访问HomePage.java中的List值(它将数据库中的json转换为LIST)。 但是使用上面的代码..我能够动态地成功添加行而无需动态更改代码而不更改列。 每次我需要在HomePage.java中定义构造函数UserBean.java和实例以添加列。

任何人都可以建议一种替代方法,如何在ListView中动态添加列,以便在Wicket Table中显示。

我只是一个初学者。任何帮助都会受到赞赏。提前谢谢。

修改 -

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;

   }
}

是否有任何其他方法直接在添加ListView中使用JSON键值对(即在HomePage.java中添加ListView中读取json),以便在Wicket Table 中显示,而不使用Beans并创建实例,至于从数据库中JSON动态添加列和行

2 个答案:

答案 0 :(得分:1)

您需要比Java bean更灵活的数据结构。

最简单的解决方案是使用List<List<Object>>代替。即列表中的每个元素都是另一个列表。第一个内部列表将带来列的名称。其余元素将带来单元格值。

我希望你明白这个主意!祝你好运!

答案 1 :(得分:1)

根据我在上一个问题中所读到的内容,我认为你想要做这样的事情:

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())));
        }
    });