开发者

How to show data from other db table in h:dataTable in JSF?

I've a datatable which is binded to a database table. In each row, I want to show data from other table based on the current row ID (I'll do that). How can I do that? For example, if I have user's table I'd like to show all groups of the user in each table row开发者_C百科. How can I create a method that is called on each table row?


Either you load all the required data at the beginning into your own datamodel or you could implement your own List to be used in the datatable. The List than can load the row data based on your criterias. Something like:

public class MyList extends AbstractList<MyPageEntry> {
    public static Logger log = Logger.getLogger(MyList.class.getName());

    private int total = 0;
    private int pageSize = 100;

    private int pageNumber = 0;
    private List<MyPage> pages = new ArrayList<MyPage>();
    private final DBConnection connection;

    public PageableDebugList(SpiderConnection connection, String sourcename, String debugFilterValue) {
        super();
        this.connection = connection;
        fetchPage(0);
    }

    private synchronized void fetchPage(int pageNumber) {
        int start = pageNumber * pageSize;
        try {
                            // possibly add other criteria here 
            MyPage page = connection.getPage(start, pageSize,...);
            while (pages.size() < pageNumber + 1) {
                pages.add(null);
            }
            pages.set(pageNumber, page);

            total = page.getTotalSize();    

            if (false) {
                preFetchNext(pageNumber + 1, 1);
            }
            log.fine(page.getSize() + " " + total);
        } catch (Exception ex) {
            log.warning("Error fetching page - " + ex);
        }
    }

    public void reset() {
        pages = new ArrayList<Page>();
    }   

    private boolean hasPage() {
        return pages.size() >= pageNumber + 1 && pages.get(pageNumber) != null;
    }

    public boolean isRowAvailable(int rowIndex) {
        if (!hasPage())
            return false;
        if (total > rowIndex && rowIndex >= 0)
            return true;
        else
            return false;
    }

    @Override
    public synchronized MyPageEntry get(int index) {
        pageNumber = index / pageSize;
        if (!hasPage()) {
            try {
                fetchPage(Math.abs(pageNumber));
            } catch (Exception e) {
                log.warning(e.toString());
            }
        }
        int localIndex = index % pageSize;
        if (isRowAvailable(index)) {
            List<PageEntry> resultList = pages.get(pageNumber).getEntries();
            if (localIndex < resultList.size()) {
                MyPageEntry result = resultList.get(localIndex);
                return result;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }   

    @Override
    public int size() {
        return total;
    }   

    private void preFetchNext(final int start, final int count) {
        Thread t = new Thread() {
            @Override
                public void run() {
                for (int i = start; i < start + count; i++) {
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        log.warning(e.toString());
                    }       

                    try {
                        fetchPage(i);
                    } catch (Exception e) {
                        log.warning(e.toString());
                    }
                }
            }
        };
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public List<DebugLinkPage> getPages() {
        return pages;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageSize() {
        return pageSize;
    }

}

In this case a whole Page is loaded at a time. But you could do the fetching also in the get() method and load each row when it is accessed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜