/** * @return true if table is currently displaying paginated rows */ public boolean isPaginated() { return getPaginationMode() != PaginationMode.NONE && getRowsPerPage() > 0; }
@Test public void testRowsPerPageOptionsClearedAndRowsPerPageZero() { WTable table = new WTable(); table.setTableModel(createModel(new String[100][1])); table.setPaginationMode(PaginationMode.CLIENT); // Change to null table.setRowsPerPageOptions(Arrays.asList(0, 1, 2)); Assert.assertNotNull("Rows per options should not be null", table.getRowsPerPageOptions()); table.setRowsPerPage(0); Assert.assertEquals("Rows per page should be 0", 0, table.getRowsPerPage()); table.setRowsPerPageOptions(null); Assert.assertNull("Rows per options should be null", table.getRowsPerPageOptions()); Assert.assertEquals("Rows per page should be the default after the options are set to null", 10, table.getRowsPerPage()); // Change to empty table.setRowsPerPageOptions(Arrays.asList(0, 1, 2)); Assert.assertNotNull("Rows per options should not be null", table.getRowsPerPageOptions()); table.setRowsPerPage(0); Assert.assertEquals("Rows per page should be 0", 0, table.getRowsPerPage()); table.setRowsPerPageOptions(Collections.EMPTY_LIST); Assert.assertNull("Rows per options should be null", table.getRowsPerPageOptions()); Assert. assertEquals( "Rows per page should be the default after the options are set to empty", 10, table.getRowsPerPage()); }
int prevRows = getRowsPerPage();
/** * @param currentPage the currentPage to set. Zero based. */ public void setCurrentPage(final int currentPage) { if (currentPage < 0) { throw new IllegalArgumentException("Page number must be greater than or equal to zero."); } WTableComponentModel model = getOrCreateComponentModel(); model.currentPage = Math.min(model.getMaxPage(), currentPage); // Notify the table model that the page has changed TableModel dataModel = getTableModel(); if (dataModel instanceof ScrollableTableModel) { if (!isPaginated()) { throw new IllegalStateException( "Table with no pagination tried to set the current page on a ScrollableTableModel."); } int startIndex = getCurrentPage() * getRowsPerPage(); int endIndex = Math.min(startIndex + getRowsPerPage(), dataModel.getRowCount()) - 1; ((ScrollableTableModel) dataModel).setCurrentRows(startIndex, endIndex); } }
xml.appendOptionalAttribute("rowsPerPage", table.getRowsPerPage() > 0, table. getRowsPerPage()); xml.appendAttribute("currentPage", table.getCurrentPage());
/** * {@inheritDoc} */ @Override public Object getBean(final BeanProviderBound beanProviderBound) { TableModel dataModel = table.getTableModel(); int rowCount = dataModel.getRowCount(); if (rowCount == 0) { return Collections.emptyList(); } int startIndex = 0; int endIndex = rowCount - 1; if (PaginationMode.DYNAMIC == table.getPaginationMode() && table.isPaginated()) { int rowsPerPage = table.getRowsPerPage(); int currentPage = table.getCurrentPage(); // Only render the rows on the current page // If total row count has changed, calc the new last page startIndex = Math. min(currentPage * rowsPerPage, rowCount - (rowCount % rowsPerPage)); endIndex = Math.min(startIndex + rowsPerPage, rowCount) - 1; } if (endIndex < startIndex) { // No data return Collections.EMPTY_LIST; } return table.getRowIds(startIndex, endIndex, false); } }
@Test public void testRowsPerPageOptionsChangeRowsPerPage() { WTable table = new WTable(); table.setTableModel(createModel(new String[100][1])); table.setPaginationMode(PaginationMode.CLIENT); table.setRowsPerPage(40); Assert.assertEquals("Rows per page should be 40", 40, table.getRowsPerPage()); table.setRowsPerPageOptions(Arrays.asList(1, 2, 3)); Assert.assertEquals("Rows per page should be the first option as it was not a valid option", 1, table.getRowsPerPage()); }
int rowsPerPage = getRowsPerPage(); int currentPage = getCurrentPage();
/** * Sets the table model which provides the row/column data. * * @param tableModel the table model. */ public void setTableModel(final TableModel tableModel) { getOrCreateComponentModel().tableModel = tableModel; getOrCreateComponentModel().rowIndexMapping = null; setSelectedRows(null); setExpandedRows(null); clearPrevExpandedRows(); clearPrevRenderedRows(); if (tableModel instanceof BeanBoundTableModel) { ((BeanBoundTableModel) tableModel). setBeanProvider(new BeanBoundTableModelBeanProvider(this)); ((BeanBoundTableModel) tableModel).setBeanProperty("."); } if (tableModel instanceof ScrollableTableModel) { if (!isPaginated()) { throw new IllegalStateException( "Set a ScrollableTableModel on a table that is not paginated."); } int startIndex = getCurrentPage() * getRowsPerPage(); int endIndex = startIndex + getRowsPerPage() - 1; ((ScrollableTableModel) tableModel).setCurrentRows(startIndex, endIndex); } // Flush the repeater's row contexts and scratch maps repeater.reset(); }
@Test public void testDoPaintPaginatedDynamic() throws IOException, SAXException, XpathException { WTable component = new WTable(); component.addColumn(new WTableColumn(COL1_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL2_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL3_HEADING_TEST, WTextField.class)); TableModel tableModel = createTableModel(); component.setTableModel(tableModel); component.setVisible(true); component.setPaginationMode(PaginationMode.DYNAMIC); setActiveContext(createUIContext()); assertXpathEvaluatesTo("dynamic", "//ui:table/ui:pagination/@mode", component); assertXpathEvaluatesTo((new Integer(component.getCurrentPage())).toString(), "//ui:table/ui:pagination/@currentPage", component); assertXpathEvaluatesTo((new Integer(component.getRowsPerPage())).toString(), "//ui:table/ui:pagination/@rowsPerPage", component); assertXpathEvaluatesTo((new Integer(tableModel.getRowCount())).toString(), "//ui:table/ui:pagination/@rows", component); }
@Test public void testDoPaintWithRowsPerPageOptions() throws IOException, SAXException, XpathException { WTable component = new WTable(); component.addColumn(new WTableColumn(COL1_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL2_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL3_HEADING_TEST, WTextField.class)); TableModel tableModel = createTableModel(); component.setTableModel(tableModel); component.setVisible(true); component.setPaginationMode(PaginationMode.DYNAMIC); component.setRowsPerPageOptions(Arrays.asList(10, 20, 30)); setActiveContext(createUIContext()); assertXpathEvaluatesTo("dynamic", "//ui:table/ui:pagination/@mode", component); assertXpathEvaluatesTo((new Integer(component.getCurrentPage())).toString(), "//ui:table/ui:pagination/@currentPage", component); assertXpathEvaluatesTo((new Integer(component.getRowsPerPage())).toString(), "//ui:table/ui:pagination/@rowsPerPage", component); assertXpathEvaluatesTo((new Integer(tableModel.getRowCount())).toString(), "//ui:table/ui:pagination/@rows", component); assertXpathEvaluatesTo("10", "//ui:table/ui:pagination/ui:rowsselect/ui:option[1]/@value", component); assertXpathEvaluatesTo("20", "//ui:table/ui:pagination/ui:rowsselect/ui:option[2]/@value", component); assertXpathEvaluatesTo("30", "//ui:table/ui:pagination/ui:rowsselect/ui:option[3]/@value", component); }
@Test public void testDoPaintPaginatedClient() throws IOException, SAXException, XpathException { WTable component = new WTable(); component.addColumn(new WTableColumn(COL1_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL2_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL3_HEADING_TEST, WTextField.class)); TableModel tableModel = createTableModel(); component.setTableModel(tableModel); component.setVisible(true); component.setPaginationMode(PaginationMode.CLIENT); setActiveContext(createUIContext()); assertXpathEvaluatesTo("client", "//ui:table/ui:pagination/@mode", component); assertXpathEvaluatesTo((new Integer(component.getCurrentPage())).toString(), "//ui:table/ui:pagination/@currentPage", component); assertXpathEvaluatesTo((new Integer(component.getRowsPerPage())).toString(), "//ui:table/ui:pagination/@rowsPerPage", component); assertXpathEvaluatesTo((new Integer(tableModel.getRowCount())).toString(), "//ui:table/ui:pagination/@rows", component); assertXpathNotExists("//ui:table/ui:pagination/@controls", component); component.setPaginationLocation(PaginationLocation.BOTTOM); assertXpathEvaluatesTo("bottom", "//ui:table/ui:pagination/@controls", component); }
getRowsPerPage()); Assert.assertEquals("Incorrect page number after handleRequest with change of rows", 2, table.getCurrentPage()); getRowsPerPage()); Assert.assertEquals("Incorrect page number after handleRequest and no change", 2, table. getCurrentPage()); getRowsPerPage()); Assert.assertEquals("Incorrect page number after handleRequest with change", 4, table. getCurrentPage()); Assert.assertEquals( "Incorrect rows per page after handleRequest and change to zero rows option", 0, table.getRowsPerPage()); Assert.assertEquals( "Incorrect page number after handleRequest with change to zero rows option", 0, getCurrentPage()); Assert.assertEquals("Incorrect default rows per page after handleRequest", 20, table. getRowsPerPage());