我正在尝试将wicket框架用于我的项目,并且有些组件的东西对我来说并不清楚。
例如,我想创建一个组件--javascript网格(jqGrid)。我只需要:
1. create <table id="#jqGrid1"></table>
2. insert javascript at <head> section:
<script type="text/javascript">
$(document).ready(function () { $('#jqGrid1').jqGrid() });
</script>
所以,我创建了java类JqTable。
package kz.primesource.wicket.component;
import kz.primesource.db.docflow.TableColumn;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.protocol.http.WebApplication;
import org.json.simple.JSONObject;
import java.util.ArrayList;
public class JqTable extends Panel implements IHeaderContributor {
private String id;
private String url;
private String editurl;
private String datatype;
private String mtype;
private String autoencode;
private ArrayList<TableColumn> columns;
private int rowNum;
private ArrayList<Integer> rowList;
private String pager;
private String caption;
private boolean isShrinkToFit;
private int width;
private int height;
private boolean isToolbarVisibile;
private String toolbarPosition;
public JqTable(String id) {
super(id);
this.id = id;
}
public void renderHead(IHeaderResponse response) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("width",new Integer(100));
jsonObject.put("height", new Integer(200));
String options = jsonObject.toJSONString();
String contextPath = WebApplication.get().getServletContext().getContextPath();
response.renderJavascriptReference(contextPath + "/js/jquery.jqGrid.min.js");
response.renderJavascript("$(document).ready(function() { options = " + options + ";$('#jqGrid" + id + "').jqGrid(options); });", id);
this.setMarkupId(id);
this.setOutputMarkupId(true);
}
}
以及此类JqGrid.html的相应html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<wicket:panel>
<table id="jqGrid1" style="width:100%;height:200px">
</table>
</wicket:panel>
</body>
</html>
我无法理解,我如何更改组件代码。即为页面上的每个下一个网格生成新ID。我的意思是,我无法理解原则,如何更改组件内部的html数据,如果不只有一个标签,而是标签内部的层次结构。
答案 0 :(得分:2)
你几乎就在那里,但让检票口为你管理ids:
private Component gridComponent;
public JqTable(final String id){
super(id);
gridComponent = new WebMarkupContainer("grid").setOutputMarkupId(true);
add(gridComponent);
}
@Override
public void renderHead(final IHeaderResponse response){
final String options = "{json}"; // code stripped so I don't need to
// include json in my classpath
final String contextPath = "context"; // dito with servlet api
response.renderJavascriptReference(contextPath
+ "/js/jquery.jqGrid.min.js");
response.renderJavascript("$(document).ready(function() { options = "
+ options + ";$('#" + gridComponent.getMarkupId()
+ "').jqGrid(options); });", gridComponent.getMarkupId());
// btw wicket has it's own domready mechanism, so you could also write it like this:
response.renderOnDomReadyJavascript("options = "
+ options + ";$('#" + gridComponent.getMarkupId()
+ "').jqGrid(options);");
}
和html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<wicket:panel>
<table wicket:id="grid" style="width:100%;height:200px">
</table>
</wicket:panel>
</body>
</html>
答案 1 :(得分:0)
在您的Component中,您无需更改自己的检票口:id。 “jqGrid1”id是内部使用的。使用组件时,使用一些“外部”ID将其添加到页面层次结构中。
示例:
add(new JqTable("table1");
并在html中:
<div wicket:id="table1">this gets replaced with the JqTable component</div>
生成的组合输出类似于:
<div wicket:id="table1">
<table id="jqGrid1" style="width:100%;height:200px">
</table>
因此,你可以通过给另一个id(table2 ...)
添加另一个JqTable 希望有所帮助。