How to display the user selected columns from a table?
HI,
Let me explain the criteria..
i am into reporting section in my team and i have 12 selection criterias
i.e. 12 different columns in a table and i need to give the user a convience to select whichever column he wants and accordingly need to display the search result...i.e example :
if i have empname,empid,address,loc etc to search and the user selects only emplid col and some search criteria then i need to display the table dynamically. using jsp. so please suggest me how to do...and if there is any example let me know.
[559 byte] By [
ragvittala] at [2007-11-27 6:40:11]

# 1
Use checkboxes to select columns?
# 2
thank u bala ...but i need to use 2 list boxes where, i should add the elements from 1 list box to other so, i need to take the cols which are in the sec list box.....and if there is any example of checkbox for selecting the cols to viewed ,pls let me know
# 3
His name's BalusC not bala :D
So make two <select> fields with multiple attribute. Then populate the first one with all the columns from the table and when then user selects one and (probably) clicks a button to confirm selection, transfer those column names to the second list.
And when the page submits, use request.getParameterValues() for that second list name and get all the names of columns that the user selected and use them. As simple as that.
More of JavaScript than JSP if you ask me ( if you want it to happen on the client-side and not irritatingly submit each time the user selects/ deselects a column! )
# 4
ya got it but ,how do i generate the tables with these selected columns ?.is there any examples for the same ...pls let me know
# 5
> ya got it but ,how do i generate the tables with
> these selected columns ?.is there any examples for
> the same ...
> pls let me know
Well you'll have the column names now so all you need to do is create the query with them!
<%
String columnsList = "";
String [] selectedValues = request.getParameterValues("column_names_selectbox");
if ( selectedValues == null )
throw new Exception ( "No columns selected");
for ( int i = 0; i < selectedValues.length; i++ )
{
columnsList += selectedValues[i] + ", ";
}
columnsList = columnsList.substring(0, columnsList.lastIndexOf(",")); //the loop will append one extra , so remove that
PreparedStatement psFetchTableValues = con.prepareStatement(" select " + columnsList + " from myTable where someCondition is met");
//so if the user selects employeeid and employeename you'll get the final value of columnsList as "employeeid, employeename" and so your statement would become " select employeeid, employeename from myTable where someCondition is met".
%>
Good enough? You'll have to do the client-side scripting to handle the columns moving from one combo box to the other yourself :D
# 6
thkx boss,
but ,
this logic i have implemented , my prob here is how to display only these colos in the table dynamically i.e. displayable cols in the report should be flexible depending on the selected cols.
for ex:
say i have a,b,c,d,e,f and g are cols in the table and the user wants to view only "a" and "g" specifically .So the resulting table should have only 2 cols in it. ...let me know about this plsssss
# 7
Are you talking about the HTML table that will display the data? If yes, why didn't you say so? :D
Well in that case, you can either use the number of columns that you've received from the select box or you can use the metadata for the resultset to find out how many columns there are.
Once you know that, you only generate that many <td> elements.
If this is not what you're asking then you're going to have to be much more specific coz I can't think of anything else you might mean. Let us know exactly what your problem is!
# 8
yes sir am talking about html tables where i need it to be dynamic ..i.e the cols of this tables should get the values from the user...i think its more detailed now..
# 9
Just identify the columns. Column a, b, c, d, e, f, etc. Pass those identifiers through the checkboxes. If the user selects a and d, then request.getParameterValues("checkboxname") should return {a, d}.
# 10
Ok, I'm just not getting what the problem is.
We've already discussed
1. How the user will select which columns he wants to view
2. How you'll receive those values in the JSP/servlet
3. How, with the values from 2., you'll fetch only the relevant columns from the database.
4. How, since you can count how many columns you've fetched, you will know how many <td> tags to generate to make the columns for the HTML table
What else is left? Could you point out which of these 4 is problem or is there some other step somewhere?
# 11
yes i think its done... i will implement the same and let u know..can i know ur mail-id n ur name ? :D
# 12
Not very comfortable with putting my email id on public forums; if you really want it, you can find it over at nogoodatcoding.googlepages.com