tags:

views:

129

answers:

2

I'm trying to use wicket framework for my project, and some things with components are not clear for me.

For example, I want to create a component — javascript grid (jqGrid). All I need is:

1. create <table id="#jqGrid1"></table>
2. insert javascript at <head> section: 
<script type="text/javascript">
$(document).ready(function () { $('#jqGrid1').jqGrid()  }); 
</script>

So, i created java class 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);
}

}

and the corresponding html for this class JqGrid.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
    <title></title>
</head>
<body>
<wicket:panel>
    <table id="jqGrid1" style="width:100%;height:200px">
    </table>
</wicket:panel>
</body>
</html>

And I can't understand, how I can change the components code. I.e. generate new id for every next grid on a page. I mean, I can't understand principles, how to change html data inside component, if there are not only one tag, but hierarchy of tags inside each other.

A: 

In your Component, you don't need to change your own wicket:id. The "jqGrid1" id is internal to use. when using your Component, you add it to the page hierarchy using some 'external' id.

example:

add(new JqTable("table1");

and in html:

<div wicket:id="table1">this gets replaced with the JqTable component</div>

the generated combined output would be something like:

<div wicket:id="table1"> 
   <table id="jqGrid1" style="width:100%;height:200px">
</table>

So, you can add another JqTable by giving it another id (table2 ...)

hope that helped.

bert
Not a valid solution: There would still be multiple jqGrid1 ids in the document
seanizer
yes. but not on the same level. so yes, it is a valid solution. i have this working many times ..
bert
I mean valid as in 'valid XHTML'. And no, it's not valid to have multiple dom elements with the same id on one page. If it works, that's a browser bug. Not everything that works somewhere is valid. (If you use them on separate pages that's something completely different)
seanizer
Ah, ok. I was talking about the wicket:id, not the id. the last one must of course be unique. So far, i let wicket handle the creation of the id (by using setOutoutMarkupId()) Gruss nach Berlin aus Dresden
bert
Gruss zurück. Wicket in Dresden? Spannend...
seanizer
+2  A: 

You're almost there, but let wicket manage the ids for you:

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

and the html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
    <title></title>
</head>
<body>
<wicket:panel>
    <table wicket:id="grid" style="width:100%;height:200px">
    </table>
</wicket:panel>
</body>
</html>
seanizer