-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I was looking on ZK forum and on documentation a way to export data from listbox to the csv format. I did not find much information about it, so I just implemented something that solved my problem, I am sharing below a simplified version. As the JSP Display Tag component, it would be interesting if the listbox embedded the exporting functionality on future versions.
<?xml version="1.0" encoding="UTF-8"?> <zk xmlns="http://www.zkoss.org/2005/zul"> <window title="Simple listbox content exporting test" width="250px" border="normal"> <zscript> String[] data = new String[30]; for(int j=0; j < data.length; ++j) { data = "option " + j; } ListModel strset = new SimpleListModel(data); public static void export_to_csv(Listbox listbox) { String s = ";"; StringBuffer sb = new StringBuffer(); for (Object head : listbox.getHeads()) { String h = ""; for (Object header : ((Listhead) head).getChildren()) { h += ((Listheader) header).getLabel() + s; } sb.append(h + "\n"); } for (Object item : listbox.getItems()) { String i = ""; for (Object cell : ((Listitem) item).getChildren()) { i += ((Listcell) cell).getLabel() + s; } sb.append(i + "\n"); } Filedownload.save(sb.toString().getBytes(), "text/plain", "gf_dot_analyst_at_gmail_dot_com.csv"); } </zscript> <listbox id="list" rows="10" model="${strset}"> <listhead> <listheader label="Options" sort="auto"/> <listheader label="Values" sort="auto"/> </listhead> </listbox> <button label="Export to csv" onClick="export_to_csv(list)"/> </window> </zk>
Hi,
Thanks for the sharing.
You can also post it to ZK Feature.
/Jumper
Posted to feature request (ID 2807216).
Thanks for your attention,
Genildo.
Hi hamroune,
this solution works for the default mode only. In paging mode, you have to go through each page so that the items are rendered. If you are using databinding/live data, you can export the model of the listbox by changing few lines of the above code.
+1
The CSV Specification can be found at
http://en.wikipedia.org/wiki/Comma-separated_values
There is a couple of framework which already implement the specification,
please see http://www.csvobjects.org/
It would be fine to have like paging an toolbox "Export Options": CSV
EXCEL XML
The toolbox can be hidden or not simple as:
<listbox id="list" export="csv, excel, xml">
So it mean default empty <listbox id="list" export=""> is to not show the
export toolbox
Regards
>> but I have not find a generic method availible for all listbox independant from the model.
Maybe genildof means use reflection for the model (i.e. use apache bean utils). This could be a generic way for the model.
This could be problem. i.e. the list containts person with first- and lastname, the modelbean containts many fields...
Another approach could be (not tested):
If Model
no --> your code
yes --> getModel, get the ListitemRenderer
use the ListitemRender to convert the model to the listitem --> work with your code
something like this
for(Object bean : model) {
Listitem listitem = new Listitem():
lisitemRender.render(listitem, bean);
// yourcode
}
However, i would prefer a popup like a print dialog: export the actual page or all pages....
/Robert
Thanks very much for your sharing...
helpful for me.
Asked: 2009-06-15 15:14:38 +0800
Seen: 6,941 times
Last updated: May 24 '12