how to select an individual row in a table without checkboxes

Hi, Can someone plz help me to select a row in a table with out using checkboxes? The data in table is populated through DB query and changes dynamically. My boss is nailing me down for this. Plz help.Thanks,
[243 byte] By [PaviEluri20a] at [2007-11-27 5:25:03]
# 1
What about links or buttons to select a single row immediately?If you want to select multiple rows without checkboxes, you have to write a lot of JavaScript.
BalusCa at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
There should not be any buttons/ checkboxes. Just a simple table with rows that can be selected using single click. Only one row can be selected at any time.Message was edited by: PaviEluri20
PaviEluri20a at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Simply call a javascript function on event onClick of the row. Append the row id to the url and submit the form. In your action class retrieve the row id from request to identify which row was selected or clicked.SirG
SirGenerala at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Thank you very much. I have one more question- what if the table is dynamically generated- has no fixed number of rows? how can I write on click event for each <tr> then?
PaviEluri20a at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
I am kind of newbie to programming. can u please eloborate its working in detail like appending the id to URL?Message was edited by: PaviEluri20
PaviEluri20a at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Are u using struts framework?
SirGenerala at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Yes. There are 3 -4 other JSP's which are combined in home page. In one of the JSP's I have this table. I am also confused on this- How can a javascript retrieve an ID associated with table row (don't know how to select a row either). We are plannig to store row ID in the DB.

Thanks,

PaviEluri20a at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

This is how it goes:

Your JSP

<nested:form action="yourAction.do">

.... .... .... .... .... .... .... .... ....

.... .... .... .... .... .... .... .... ....

<table>

//Form bean should have a collection variable "rows", with getters and setters

<nested:present property="rows">

<nested:iterate property="rows">

//uniqueId and longName are variables of your collection attribute objects

<nested:hidden property="uniqueId"/>

<tr>

<TD class="formData" onclick="javascript:selectRow('yourAction.do?uid=<nested:write property="uniqueId"/>')">

<nested:hidden property="longName"/>

<nested:write property="longName"/>

</TD>

</tr>

</nested:iterate>

</nested:present>

</table>

.... .... .... .... .... .... .... .... ....

.... .... .... .... .... .... .... .... ....

Your javascript function in jsp

<script type="text/javascript">

function selectRow(URL) {

document.getElementsByTagName('BODY')[0].style.cursor = 'wait';

document.rowForm.action = URL;

document.rowForm.submit();

}

</script>

Your action class

.... .... .... .... .... .... .... .... ....

.... .... .... .... .... .... .... .... ....

String rowUniqueId = request.getParamater("uid");

//Thus you know which row has been selected

//Store your row id in DB

.... .... .... .... .... .... .... .... ....

.... .... .... .... .... .... .... .... ....

Hope that makes things clear. Use nested form, it makes easier to manage multiple forms in your jsp. For reference on struts tags you can refer to:

http://struts.apache.org/1.x/struts-taglib/tlddoc/index.html

SirG

SirGenerala at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
You are truly excellent. Thank you very much.
PaviEluri20a at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

> how can I write on click event for each <tr> then?

Just write it :) Make use of the 'this' property.

HTML<form name="formname">

<input type="hidden" name="selectedRowId" />

<table>

<tr id="someAutogeneratedId" onclick="selectRow(this);">

...

The "someAutogeneratedId" should be an unique identifier.

JS<script>

function selectRow(tr) {

document.formname.selectedRowId = tr.id;

document.formname.submit();

}

</script>

Servlet:String selectedRowId = request.getParameter("selectedRowId");

Edit hm, I have to write faster, I guess :o)

SirGeneral: the "javascript:" declaration is not needed as the onclick actually always expects JavaScript.

Message was edited by:

BalusC

BalusCa at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
Thank you very much sir,It was excellent.
PaviEluri20a at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

The above code takes to particular link just by clicking on the row itself. I need to have 3-4 options associated with click event.

Also, all these options and the row must be populated in GUI - for example in this post if some one clicks on reply and enters text, the text is displayed in a row with default values like reply- edit and send it as an e-mail.

PaviEluri20a at 2007-7-12 14:44:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
Your requirement is not clear. Can you elaborate it a bit?SirG
SirGenerala at 2007-7-12 14:44:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14
I recently had this completed using Javascript. I am not sure if I can send the row ID back to DB but right now I am able to access it using JS. Thank you very much for your participation. Regards,
PaviEluri20a at 2007-7-12 14:44:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...