Sunday, October 24, 2010

Displaying AJAX Tables in PHP vs Java EE: ZFDataGrid and PrimeFaces DataTable

While developing with PHP + Zend Framework + Doctrine I missed an easy way to display/edit data using a grid/table.

A very useful component I found is ZFDataGrid.

Here's a sample code of how to use ZFDataGrid:

    function simpleAction()

    {         //Zend_Config         $config = new Zend_Config_Ini('./application/grids/grid.ini', 'production');                 //Grid Initialization         $grid = Bvb_Grid::factory('Bvb_Grid_Deploy_Table', $config, 'id');                 //Setting grid source         $grid->setSource(new Bvb_Grid_Source_Zend_Table(new Bugs()));                 //CRUD Configuration         $form = new Bvb_Grid_Form();         $form->setAdd(true)->setEdit(true)->setDelete(true);         $grid->setForm($form);                 //Pass it to the view         $this->view->pages = $grid;         $this->render('index');     }
It looks pretty good too.

Check the ZFDataGrid Live Demo here.

However, working with data grids using JSF 2.0 and PrimeFaces felt much more natural and easier.

Here's a sample code using PrimeFaces' DataTable :

<h:form>

    <p:dataTable var="car" value="#{tableBean.lazyModel}" paginator="true" rows="10" lazy="true"
                 paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                 rowsPerPageTemplate="5,10,15"
                 selection="#{tableBean.selectedCar}" selectionMode="single"
                 onRowSelectComplete="carDialog.show()" onRowSelectUpdate="display">
        <f:facet name="header">
            Displaying 100,000,000 Cars
        </f:facet>
        <p:column headerText="Model">
            <h:outputText value="#{car.model}" />
        </p:column>
        <p:column headerText="Year">
            <h:outputText value="#{car.year}" />
        </p:column>
        <p:column headerText="Manufacturer">
            <h:outputText value="#{car.manufacturer}" />
        </p:column>
        <p:column headerText="Color">
            <h:outputText value="#{car.color}" />
        </p:column>
    </p:dataTable>

    <p:dialog header="Car Detail" widgetVar="carDialog" resizable="false"
              width="200" showEffect="explode" hideEffect="explode">
        <h:panelGrid id="display" columns="2" cellpadding="4">
            <f:facet name="header">
                <p:graphicImage value="/images/cars/#{tableBean.selectedCar.manufacturer}.jpg"/>
            </f:facet>
            <h:outputText value="Model:" />
            <h:outputText value="#{tableBean.selectedCar.model}"/>

            <h:outputText value="Year:" />
            <h:outputText value="#{tableBean.selectedCar.year}"/>

            <h:outputText value="Manufacturer:" />
            <h:outputText value="#{tableBean.selectedCar.manufacturer}"/>

            <h:outputText value="Color:" />
            <h:outputText value="#{tableBean.selectedCar.color}"/>
        </h:panelGrid>
    </p:dialog>

</h:form>

The above code may look verbose, but it packs a lot of functionality and it's very easy and intuitive to customize.
When you click a row it displays a nice dialog with a picture. Furthermore, it's actually lazy loading 100,000,000 rows!! (yes, ONE HUNDRED MILLION ROWS)

Here's how it looks:

You can see for real the PrimeFaces DataTable Lazy-loading Live Demo here.

It's very easy to add lazy-loading support to DataTable:

        lazyModel = new LazyDataModel<Car>() {

/**
* Dummy implementation of loading a certain segment of data.
* In a real application, this method should load data from a datasource
*/
@Override
public List<Car> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String,String> filters) {
logger.log(Level.INFO, "Loading the lazy car data between {0} and {1}", new Object[]{first, (first+pageSize)});

                //Sorting and Filtering information are not used for demo purposes just random dummy data is returned

List<Car> lazyCars = new ArrayList<Car>();
populateLazyRandomCars(lazyCars, pageSize);

return lazyCars;
}
};

        /**
         * In a real application, this number should be resolved by a projection query
         */
        lazyModel.setRowCount(100000000);

Not to disrespect PHP or ZFDataGrid in anyway (I still need to use them for some of my work), but the experience with JSF 2.0 and PrimeFaces wins hands down. I think it's more because of PrimeFaces than JSF 2.0, but they're such a powerful combo (compared to if you use PrimeFaces with JSF 1.2).

I do hope that PrimeFaces provide a utility class that implements LazyDataModel for a Hibernate/HQL or JPA/JPQL query, but for now I can live with the above.

5 comments:

Anonymous said...

"I do hope that PrimeFaces provide a utility class that implements LazyDataModel for a Hibernate/HQL or JPA/JPQL query"

JPA, JSF and Primefaces are naturally interconnected

coach factore outlet said...

Here we play with all sorts of influences - coach factory outlet of desired shapes tiffany jewelry to prioritize the tiffany and co manner in which I go about finding what I need," the coach factory outlet tells Ecouterre.More than 1,000 runners began the race.Not just in true religion jeans, but also in making sure you stay happy until the cheap jerseys rain every day. Hey, $350 is way less than those coach outlet. For those unfamiliar, the coach factory online (only the first coach factory count toward the rankings this year) in coach outlet online carry all their own alexander wang shoes for a daily water ration and michael kors outlet tent to sleep under;The coach factory outlet is yours, but coach factory outlet is to just be yourself and be coach outlet store online.The sleek ensemble came on show through sac burberry. Choose from brands like Bernardo, michael kors outlet and others.which saw everything he could to shake off that 'alexander wang bags' image, just got the treatment from the michael kors himself, marc by marc jacobs outlet department. the company was coach factory outlet online that it can really tell that you're turned on, so michael kors should definitely be saved for the bedroom.which has seen him take on projects from labels like michael kors outlet online, to things like designing boats.

Anonymous said...

Keep sharing such ideas in the future as well. This was actually what I was looking for, and I am glad to came here! Thanks for sharing the such information with us.rn-ds-partnership.co.uk |

prepaybank.co.uk |

calleija.co.uk |

dontdothedodo.co.uk |

intelligentformulation.co.uk |

caphe-vn.co.uk |

futrets.co.uk |

auction-expert.co.uk |

sail-lizziemay.co.uk |

dubai-realestate.co.uk |

polo outlet said...

http://www.superbagsmarket.com/
http://www.superbagsmarket.com/louis-vuitton-bags-c-1.html
http://www.superbagsmarket.com/hermes-bags-c-2.html
http://www.superbagsmarket.com/prada-bags-c-3.html
http://www.superbagsmarket.com/chanel-bags-c-4.html
http://www.superbagsmarket.com/gucci-bags-c-5.html
http://www.superbagsmarket.com/burberry-bags-c-6.html
http://www.superbagsmarket.com/celine-bags-c-7.html
http://www.superbagsmarket.com/balenciaga-bags-c-8.html
http://www.superbagsmarket.com/christian-dior-bags-c-9.html
http://www.superbagsmarket.com/chloe-bags-c-10.html
http://www.superbagsmarket.com/bvlgari-bags-c-11.html
http://www.superbagsmarket.com/bally-bags-c-12.html
http://www.superbagsmarket.com/coach-bags-c-13.html
http://www.superbagsmarket.com/michael-kors-bags-c-14.html
http://www.superbagsmarket.com/mcm-bags-c-15.html
http://www.superbagsmarket.com/fendi-bags-c-16.html
http://www.superbagsmarket.com/mulberry-bags-c-17.html
http://www.superbagsmarket.com/marc-jacobs-bags-c-18.html
http://www.superbagsmarket.com/miu-miu-bags-c-19.html
http://www.superbagsmarket.com/ysl-bags-c-20.html
http://www.superbagsmarket.com/tory-burch-bags-c-21.html
http://www.superbagsmarket.com/givenchy-bags-c-22.html
http://www.superbagsmarket.com/ferragamo-bags-c-23.html
http://www.superbagsmarket.com/lancel-bags-c-24.html
http://www.superbagsmarket.com/loewe-bags-c-25.html
http://www.superbagsmarket.com/tods-bags-c-26.html
http://www.superbagsmarket.com/paul-smith-bags-c-27.html
http://www.superbagsmarket.com/dg-bags-c-28.html
http://www.superbagsmarket.com/alexander-wang-bags-c-29.html
http://www.superbagsmarket.com/bottega-veneta-bags-c-30.html
http://www.superbagsmarket.com/specials.html
http://www.guccishoesuk-2014.com/
http://www.burbagssale2014.com/
http://www.polo-ralphlaurenonline.com/
http://www.thebeatsbydre.net/

Getit said...

Download Zapya for PC, Zapya free download, Zapya download, Zapya apk to share files and folders easily.

Post a Comment

 
Copyright 2009 Spring vs Java EE Web Dev. Powered by Blogger Blogger Templates create by Deluxe Templates. WP by Masterplan