数据网格与计算网格_使用Jmesa的智能复杂数据网格。

数据网格与计算网格

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data.

我有一个显示用户工作台的项目要求。该工作台将包含多个数据网格。在每个网格中,用户将能够看到大量数据。

These data grids should allow the user to

这些数据网格应允许用户

1. Sort

1.排序

2. Export the data to Excel, CSV as well as PDF format

2.将数据导出到Excel,CSV和PDF格式

3. User customizable Pagination

3.用户可定制的分页

4. Filter

4.筛选

5. All the grid activities (i.e. 1, 2, 3 and 4) should be Ajax enabled.

5.所有网格活动(即1、2、3和4)都应启用Ajax。

My project was a J2EE project using Struts 1.3.8 as the web framework.  Struts itself does not have a direct support for such features.  I used Jmesa. It is an open source Dynamic HTML Table Rendering API that allows to filter, sort, paginate, export and inline editing of data (if required).  I have configured this API according to my project needs.

我的项目是一个使用Struts 1.3.8作为Web框架的J2EE项目。 Struts本身不直接支持此类功能。 我用过Jmesa。 它是一个开放源代码的Dynamic HTML Table Rendering API ,允许对数据进行过滤,排序,分页,导出和内联编辑(如果需要)。 我已经根据项目需要配置了此API。

The requirements that I have mentioned above (i.e. 1 to 5) are not solved by any single library or API apart from Jmesa.  I have gone through the following solutions below, but none of them have the support for all mentioned features like Jmesa has.

我上面提到的要求(即1到5)无法通过Jmesa之外的任何单个库或API来解决。 我在下面介绍了以下解决方案,但是它们都没有像Jmesa一样支持所有提到的功能。

I used JMesa extensively in my recent  project. I am benefited heavily from this API . So I want to give something back to this wonderful API by contributing some code/tips/techniques related to the usage of JMesa.  Complete source code is attached at the end of theis article.

我在最近的项目中广泛使用了JMesa。 这个API使我受益匪浅。 因此,我想通过贡献一些与JMesa的使用有关的代码/技巧/技术,来回馈这个出色的API。 完整的源代码附在该论文的末尾。

Following are few topics on which I going to focus on.

以下是我要重点关注的几个主题。

1.  Complete example of using Jmesa with Struts (Specifically the Ajax enabled version).

1.使用Jmesa和Struts(特别是启用Ajax的版本)的完整示例。

2.  Practical example /recipe on the row rendering ( i.e. proper usage of HtmlRowRendererImpl )

2.关于行渲染的实际示例/食谱(即HtmlRowRendererImpl的正确用法)

3.  Proper usage of ColumnSort with examples .

3.正确使用带有示例的ColumnSort。

4.  I have a generic comparator (I am working on this currently ), which deals with some real life data type problems in terms of sorting (for example a String containing a date or number, so the sorting becomes weird).  This comparator is capable enough to handle such frequently required real life problem scenarios.

4.我有一个通用比较器(目前正在研究),它在排序方面处理一些现实生活中的数据类型问题(例如,一个包含日期或数字的String,因此排序变得很奇怪)。 该比较器有足够的能力来处理这种经常需要的现实生活问题方案。

1.将Jmesa与Struts(With Ajax)一起使用的完整示例 (1. Complete example of using Jmesa with Struts (With Ajax ))

Brief Summary of what I want to do:

我要做什么的简短摘要:

First, setup an application configuration so that Struts 1.x and Jmesa works together.  Then I will have an action class JmesaDispatchAction.java (I am using DispatchAction here so that if wants same action to works for multiple jmesa table less struts mapping and code is required ) which will have a method load().   This load() method is everything .

首先,设置应用程序配置,以使Struts 1.x和Jmesa一起工作。 然后,我将有一个动作类JmesaDispatchAction.java(我在这里使用DispatchAction,因此,如果要使同一动作对多个jmesa表起作用,则需要较少的struts映射和代码),它将具有方法load()。 这个load()方法就是一切。

Setup

建立

Create the structure for any standard struts project. Noww place Jmesalibrariess in side WEB-INF/lib folder.  Place jmesa.properties inside  WEB-INF. Inside web.xml create its entry like the following:

为任何标准的struts项目创建结构。 现在将Jmesalibrariess放在侧面WEB-INF / lib文件夹中。 将jmesa.properties放在WEB-INF中。 在web.xml内创建其条目,如下所示:

<context-param>
    <param-name>jmesaPreferencesLocation</param-name>
    <param-value>WEB-INF/jmesa.properties</param-value>
 </context-param>
Image for showing the set up for Jmesa

Code the Action class and the JSP:

对Action类和JSP进行编码:

The action class will have two basic block inside the load() method one for normal flow another for Ajax request from the JSP page (with request parametre ajax=true).  In the JSP page named pay attention to the JavaScript method onInvokeAction, the main ajaxifying trick is present there .

动作类将在load()方法内有两个基本块,一个用于正常流程,另一个用于来自JSP页面的Ajax请求(请求参数ajax = true)。 在名为JSP的页面中,请注意JavaScript方法onInvokeAction,其中存在主要的技巧。

2.行渲染配方 (2. Row Rendering Recipe)

假设我想用特定颜色突出显示具有特定颜色的特定行...我该怎么做?

This is an ideal case for using a Custom HtmlRowRenderer which will extend HtmlRowRendererImpl.

这是使用自定义HtmlRowRenderer(扩展HtmlRowRendererImpl)的理想情况。

Now we need to override the default public Object render(Object item, int rowcount) method to our specific requirement.

现在,我们需要将默认的公共Object render(Object item,int rowcount)方法重写为我们的特定要求。

In this case the data list contains a DTO object which has a boolean field named ok. If this field value is true we need to highlight the row with green color.

在这种情况下,数据列表包含一个DTO对象,该对象具有一个名为ok的布尔字段。 如果此字段值为true,则需要用绿色突出显示该行。

Here is the code for that

这是该代码

public class CustomHtmlRowRenderer extends HtmlRowRendererImpl 
{
	public Object render(Object item, int rowcount) 
	{
      	HtmlBuilder html = new HtmlBuilder();
        html.tr(1);
        html.id(getCoreContext().getLimit().getId() + "_row" + rowcount);
        
        //Get the actual value of ok field
        Object okValueObj = ItemUtils.getItemValue(item, "ok");
        String valueStr = String.valueOf(okValueObj).toLowerCase();
        if (valueStr.contains("true")) 
        {
            html.style("background-color:green");
        } 
        else 
        {
            html.style(getStyle());
        } 

        html.styleClass(getStyleClass(rowcount));
        html.append(getRowEvents(item, rowcount));
        html.close();
        return html.toString(); 
	}   

}
         /*
         * Override and customize the sorting of the table facade 
         */
        ColumnSort customColumnSort = new CustomColumnSort();
        tableFacade.setColumnSort(customColumnSort);
Grid before row rendering code added

After row rendering customization, the grid is  like :

自定义行渲染后,网格类似于:

Grid after row rendering code added

3.自定义排序食谱 (3. Custom Sorting Recipe)

如何使用Jmesa根据自己的选择对表格进行排序?

Jmesa comes with its own nice way of sorting.  But for custom sorting of your own you need to write your own  Custom Sorting class   which will implement ColumnSort interface and implement the sortItems method of the said interface to customize the sorting.

Jmesa带有自己的不错的排序方式。 但是对于您自己的自定义排序,您需要编写自己的Custom Sorting类,该类将实现ColumnSort接口并实现所述接口的sortItems方法以自定义排序。

Remember  sortItems() method is the place where you whatever customization.  The code is written below to sort the data on two properties age and displaydate.

请记住,sortItems()方法是您进行任何自定义的地方。 下面编写了代码,以对年龄和显示日期这两个属性进行数据排序。

public class CustomColumnSort implements ColumnSort 
{
    public Collection<?> sortItems(Collection<?> items, Limit limit) 
    {
        ComparatorChain chain = new ComparatorChain();
        SortSet sortSet = limit.getSortSet();
        Iterator sortItr= sortSet.getSorts().iterator();
        while(sortItr.hasNext())
        {
            Sort sort =(Sort)sortItr.next();
            GenericComparator genericComparator = null;
			
            if (sort.getOrder() == Order.ASC) 
            {
                if(sort.getProperty().equalsIgnoreCase("age"))
                {
                    genericComparator= new GenericComparator(sort.getProperty(),true,GenericComparator.NUMERIC_STRING);
                }
                if(sort.getProperty().equalsIgnoreCase("displayDate"))
                {
                    genericComparator= new GenericComparator(sort.getProperty(),true,GenericComparator.DATE_STRING);
                }
            } 
            else if (sort.getOrder() == Order.DESC) 
            {
                /*
                 * For descending sort one extra parameter sorting mode (value false)
                 * is passed to the GenericComparator.
                 * By default its value is true.
                 * 
                 */
                if(sort.getProperty().equalsIgnoreCase("age"))
                {
                    genericComparator= new GenericComparator(sort.getProperty(),true,GenericComparator.NUMERIC_STRING,false);
                }
                if(sort.getProperty().equalsIgnoreCase("displayDate"))
                {
                    genericComparator= new GenericComparator(sort.getProperty(),true,GenericComparator.DATE_STRING,false);
                }
            }
			
            /*
             * This is the main place where the Generic comparator is added to the
             * ComparatorChain for sorting
             * 
             */
            chain.addComparator(genericComparator);

        }
		
        if (chain.size() > 0) 
        {
            Collection((List<?>) items, chain);
        }
        return items;
    }
}
        HtmlTable table = (HtmlTable) tableFacade.getTable();
        HtmlRow row = table.getRow();
        
        /*
         * Override and customize the rendering of the table facade 
         */
        CustomHtmlRowRenderer customHtmlRowRenderer = new CustomHtmlRowRenderer();
        row.setRowRenderer(customHtmlRowRenderer) ;

4.使用GenericComparator处理现实生活中的排序问题 (4. Handle real life sorting issues with GenericComparator)

我想写一个唯一的比较器,它将处理所有类型的排序。

Even if you have that there are some situations where the comparator will fail.  For e.g. you have some DTO objects within a list which you want to sort. Now the DTO has a displaydate field which is of type String but displays a Date.

即使您有某些情况下比较器也会失败。 例如,您在列表中有一些要排序的DTO对象。 现在,DTO具有一个显示日期字段,该字段的类型为String,但显示日期。

Now when the sorting takes place the comparator will try to sort it as a String but the user will be expecting a sort on date !

现在,当排序发生时,比较器将尝试将其排序为字符串,但是用户将期望按日期排序!

I mention these particular field types as hybrid data type. We can say displaydate had a Date String hybrid datatype.  Similarly, we may face problems with Numeric String, Boolean String etc etc.

我将这些特定的字段类型称为混合数据类型。 我们可以说displaydate具有Date String混合数据类型。 同样,我们可能会遇到数字字符串,布尔字符串等问题。

Here comes my brain child custom Generic Comparator. Just inform the comparator about the hybrid data type and let it do its sorting job.

这是我的大脑孩子定制的通用比较器。 只需告知比较器有关混合数据类型的数据,然后让它完成其排序工作即可。

Inside sorItems method of CustomColumnSort I have used generic comparator .

在CustomColumnSort的sorItems方法内部,我使用了通用比较器。

For proper java data type you just need to create the comparator like

对于适当的Java数据类型,您只需要创建类似的比较器

GenericComparator comparator= new GenericComparator(<String fieldNameToBeSorted>)
public GenericComparator(<String fieldNameToBeSorted>,<boolean hybridDataType>,<String hybridDataTypeName>)

Inside CustomColumnSort I have shown how to deal with hybrid data type like the following

在CustomColumnSort内部,我已经显示了如何处理混合数据类型,如下所示

GenericComparator genericComparator= new GenericComparator(sort.getProperty(),true,GenericComparator.NUMERIC_STRING);
GenericComparator  genericComparator= new GenericComparator(sort.getProperty());

Please note:

请注意:

I've attached the complete example source code in the form of a war file.  Please have a look.  In case you need any reference you may go to the URL:

我已经以war文件的形式附加了完整的示例源代码。 请看一看。 如果您需要任何参考,可以访问以下URL:

http://code.google.com/p/jmesa/   http://code.google.com/p/jmesa/

and have a look.  I have written an article about Jmesa usage with Struts over there and my sample code is also uploaded there.

看看吧 我写了一篇关于Struts在Jmesa上的用法的文章,我的示例代码也上传到了那里。

Warfile.doc Warfile.doc

翻译自: https://www.experts-exchange.com/articles/3023/Smart-Sophisticated-Data-grids-using-Jmesa.html

数据网格与计算网格

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值