注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

和申的个人主页

专注于java开发,1985wanggang

 
 
 

日志

 
 

TableFacade  

2009-04-20 15:23:19|  分类: Java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

The TableFacade takes out the complexity of building HTML tables by offering a very high level API to work with. The approach that the TableFacade takes is you declare the whole table with all the defaults, and then customize things as needed.
For instance if you want to customize the previous example you can do this (Spring MVC example):
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    ModelAndView mv = new ModelAndView(successView);
    Collection<Object> items = presidentService.getPresidents();

    TableFacade tableFacade = TableFacadeFactory.createTableFacade(id, request);
    tableFacade.setColumnProperties("name.firstName", "name.lastName", "term", "career", "born");
    tableFacade.setItems(items);
    tableFacade.setExportTypes(response, ExportType.CSV, ExportType.EXCEL);

    Table table = tableFacade.getTable();
    table.setCaption("Presidents");

    Column firstName = table.getRow().getColumn("name.firstName");
    firstName.setTitle("First Name");

    Column lastName = table.getRow().getColumn("name.lastName");
    lastName.setTitle("Last Name");

    Limit limit = tableFacade.getLimit();
    if (limit.isExported()) {
        facade.render();
        return null;
    } else {
        HtmlTable htmlTable = (HtmlTable) table;
        htmlTable.getTableRenderer().setWidth("600px");

        firstName.getCellRenderer().setCellEditor(new CellEditor() {
            public Object getValue(Object item, String property, int rowcount) {
                Object value = new BasicCellEditor().getValue(item, property, rowcount);
                HtmlBuilder html = new HtmlBuilder();
                html.a().href().quote().append("http://www.whitehouse.gov/history/presidents/").quote().close();
                html.append(value);
                html.aEnd();
                return html.toString();
            }
        });

        String html = tableFacade.render();
        mv.addObject("presidents", html);
    }

    return mv;
}For more information about working with the table, row, and columns read the Components section.
Notice how there are no factories to deal with. However any API Object that you need is available through the facade, including the WebContext, CoreContext, Limit, Table, Toolbar, and View. When you ask the facade for a given object it builds everything it needs up to that point. Internally it keeps track of everything you are doing so it also works like a builder.
The TableFacade interface also has setters for all the facade objects including the WebContext, CoreContext, Limit, Toolbar, and View. The reason is if you really need to customize something and want to set your own implementation you can. Your object just goes into the flow of the facade. For instance if you want a custom toolbar just set the Toolbar on the facade and when the render() method is called it will use your Toolbar.
Be sure to also check out the State feature, which allows a user to come back to a table with it sorted, filtered, and paged the way that they left it. I am so used to this feature that I tend to forgot to mention it, but I bet most people would really enjoy this feature!
ComponentFactory Option
There is now an alternative way to create columns using the API. This technique is completely optional but may be nice in some situations, or you may decide this is a more natural way to create tables. Be sure to set the table on the facade after you create the table.
TableFacade tableFacade = TableFacadeFactory.createTableFacade(id, request);
tableFacade.setItems(items);

HtmlComponentFactory factory = new HtmlComponentFactory(tableFacade.getWebContext(), tableFacade.getCoreContext());

HtmlTable table = factory.createTable();

HtmlRow row = factory.createRow();
row.addColumn(factory.createColumn("name.firstName"));
row.addColumn(factory.createColumn("name.lastName"));
row.addColumn(factory.createColumn("term"));

HtmlColumn career = factory.createColumn("career");
career.setFilterable(false);
row.addColumn(career);

table.setRow(row); // be sure to set the row on the table

tableFacade.setTable(table);

  评论这张
 
阅读(703)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016