Grid taglib ver. 1.6

    Custom JSP taglib lets you implement grids (tables) in your applications. As a source of data you can use ResulSets for JDBC requests, arrays, iterators, enumerations. Also you can use this tag together with DB taglib from Coldtags suite.

Grid includes (may include) header, body (set of rows) and footer. By default tag grid automatically (parameter autoGenerateColumns) outputs data columns. So, for example, in the simplest case you can write some like this:
 


<%
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  Connection conn=DriverManager.getConnection("jdbc:odbc:db1");
  PreparedStatement st=conn.prepareStatement("select * from Test");
  ResultSet rs=st.executeQuery();
%>

<g:grid source="<%=rs%>"/>

and you will get some like this

IDNameQty
120Part 15
121Part 28
156Part 70

You can set parameter autoGenerateColumn to false and define your headers and columns. You can use nested variable rowData as a reference to the current data and rowNumbner as a reference to the current row number. Body tag rows executes own body for the each element in your datasource. For example:
 


<%@ taglib uri="taglib.tld" prefix="g" %>

<%
String a[]={ "A", "B", "C" };
%>

<g:grid source="<%=a%>" autoGenerateColumns="false">

 <g:headers>
  <g:column>Data</g:column>
  <g:column>Row</g:column>
 </g:headers>

 <g:rows>
  <g:column><%=rowData%></g:column>
  <g:column><%=rowNumber%></g:column>
 </g:rows>

</g:grid>

Data Row
A 1
B 2
C 3

You can set your own style for the each other row (parameter is alternatingStyle or alternatingClassName). For example:
 


<%@ taglib uri="taglib.tld" prefix="g" %>

<%
String a[]={ "A", "B", "C" };
%>

<g:grid source="<%=a%>" autoGenerateColumns="false" borderWidth="1" borderColor="green">

 <g:headers>
  <g:column>Data</g:column>
  <g:column>Row</g:column>
 </g:headers>

 <g:rows alternatingStyle="background:gray">
  <g:column><%=rowData%></g:column>
  <g:column><%=rowNumber%></g:column>
 </g:rows>

</g:grid>

Data Row
A 1
B 2
C 3

You do not need special tags (like ButtonColumn in .NET) for HTML forms in your grid. You can use any JSP (HTML) code in columns tag. For example:
 


<%@ taglib uri="taglib.tld" prefix="g" %>

<%
String a[]={ "A", "B", "C" };
%>

<g:grid source="<%=a%>" autoGenerateColumns="false" borderWidth="1">

 <g:headers>
  <g:column>Data</g:column>
  <g:column>Check Box</g:column>
 </g:headers>

 <g:rows>
  <form>
   <g:column><%=rowData%></g:column>
   <g:column><input type="checkbox" name=c<%=rowNumber%>></g:column>
  </form>
 </g:rows>
</g:grid>

DataCheck Box
A
B
C

And of course, you can define your own custom type for rowData variable. So for example if it is your bean, you can write some like rowData.callMethod() in your tags.

For publishing your databases you can use grid with DB taglib. In this case data type for rowData must be com.cj.grid.dbtag. For this type you can use the following methods:

getColumn(int columnNumber) returns data
getColumnName(int columnNumber) returns a name for column
getColumnType(int columnNumber) returns a type for column
getColumnCount() what's the number of columns in the ResultSet?

For example:
 


<sql:openConnection driver="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:db1" id="conn"/>
<sql:setQuery id="conn" res="A" query="select * from Test order by Id"/>

<g:grid source="<%=\"A\"%>" borderWidth="1"/>

or with your own definitions:
 


<sql:openConnection driver="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:db1" id="conn"/>
<sql:setQuery id="conn" res="A" query="select * from Test order by Id"/>

<g:grid source="<%=\"A\"%>" borderWidth="1" type="com.cj.grid.dbtag" idRowData="rowData" autoGenerateColumns="false" to="5">
 <g:headers>
  <g:column><%=rowData.getColumnName(1)%></g:column>
  <g:column><%=rowData.getColumnName(2)%></g:column>
 </g:headers>

 <g:rows type="com.cj.grid.dbtag" idRowData="row">
  <g:column><%=row.getColumn(1)%></g:column>
  <g:column><%=row.getColumn(2)%></g:column>
 </g:rows>

</g:grid>

Tags are:

grid

Body tag, outputs a table (grid). Parameters are:

1) source Describes a data source. Could be an array, iterator, enumeration, ResultSet or string. String describes ID for query results if you are using DB tags.
2) type Optional parameter. Describes a type for elements in datasource. Default value is java.lang.Object. For ResultSet it must be java.sql.ResultSet, for DB tag ID it must be com.cj.grid.dbtag
3) from Optional parameter. Describes an initial element (position) for loop through datasource (row in ResultSet). Starts with 1. Default value is 1 (first element or first row).
4) to Optional parameter. Optional parameter. Describes a last element (position) for loop through datasource (row in ResultSet). By default tag scans all elements or rows (up to last position or row).
5) width Optional parameter. Describes a width for this table
6) height Optional parameter. Describes a height for this table
7) autoGenerateColumns Optional parameter. Possible values are true or false. Default value is true.
8) style Optional parameter. Describes a CSS style
9) className Optional parameter. Describes a CSS class
10) borderWidth Optional parameter. Describes a width for border
11) borderColor Optional parameter. Describes a color for border
12) cellSpacing Optional parameter. Describes a cellspacing parameter for this table
13) cellPadding Optional parameter. Describes a cellpadding parameter for this table
14) idRowData Optional parameter. Describes a name for nested page scope variable with the first element from your datasource (first row).

headers

Body tag, outputs headers. Parameters are:

1) style Optional parameter. Describes a CSS style
2) className Optional parameter. Describes a CSS class

footers

Body tag, outputs footers. Parameters are:

1) style Optional parameter. Describes a CSS style
2) className Optional parameter. Describes a CSS class

rows

Body tag, outputs rows. Tag executes own body for the each element in your data source. Parameters are:

1) style Optional parameter. Describes a CSS style
2) className Optional parameter. Describes a CSS class
3) alternatingStyle Optional parameter. Describes a CSS style for other rows
4) alternatingClassName Optional parameter. Describes a CSS class for other rows
5) idRowNumber Optional parameter. Describes a name for nested page scope variable with row number. Type for this variable is java.lang.Integer. Default name (default value for this parameter) is rowNumber.
6) idRowData Optional parameter. Describes a name for nested page scope variable with current row's data (current element in datasource). Default name (default value for this parameter) is rowData.
7) type Optional parameter. Describes a type for elements in datasource. Default value is java.lang.Object. For ResultSet it must be java.sql.ResultSet, for DB tag ID it must be com.cj.grid.dbtag

column

Body tag, outputs column (for rows, headers, footers). Parameters are:

1) style Optional parameter. Describes a CSS style
2) className Optional parameter. Describes a CSS class
3) width Optional parameter. Describes a width for this column
4) height Optional parameter. Describes a height for this column
5) horizontalAlign Optional parameter. Describes a horizontal align. Possible values are left, right, center.
6) verticalAlign Optional parameter. Describes a vertical align. Possible values are top, bottom, middle.
7) colspan Optional parameter. Describes a colspan parameter for column. Default value is 1.
8) rowspan Optional parameter. Describes a rowspan parameter for column. Default value is 1.
9) nowrap Optional parameter. Describes a nowrap parameter for column. Possible values are true or false. Default value is false.

for downloading:

Library: gridtag.jar    Description: taglib.tld

© Coldbeans      Comments?

See also Coldtags suite - the largest collection of custom JSP tags.