Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WICKET-6941 Add visible attribute to AbstractColumn to hide/show columns in DataTable/DataGridView #491

Open
wants to merge 12 commits into
base: wicket-9.x
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,23 @@ protected final void populateItem(final Item<T> item)
for (int i = 0; i < populatorsNumber; i++)
{
ICellPopulator<T> populator = populators.get(i);
IModel<ICellPopulator<T>> populatorModel = new Model<>(populator);
Item<ICellPopulator<T>> cellItem = newCellItem(cells.newChildId(), i, populatorModel);
cells.add(cellItem);

populator.populateItem(cellItem, CELL_ITEM_ID, item.getModel());

if (cellItem.get("cell") == null)
if (populator.isVisible())
{
throw new WicketRuntimeException(
populator.getClass().getName() +
".populateItem() failed to add a component with id [" +
CELL_ITEM_ID +
"] to the provided [cellItem] object. Make sure you call add() on cellItem and make sure you gave the added component passed in 'componentId' id. ( *cellItem*.add(new MyComponent(*componentId*, rowModel) )");
IModel<ICellPopulator<T>> populatorModel = new Model<>(populator);
Item<ICellPopulator<T>> cellItem = newCellItem(cells.newChildId(), i, populatorModel);
cells.add(cellItem);

populator.populateItem(cellItem, CELL_ITEM_ID, item.getModel());

if (cellItem.get("cell") == null)
{
throw new WicketRuntimeException(
populator.getClass().getName() +
".populateItem() failed to add a component with id [" +
CELL_ITEM_ID +
"] to the provided [cellItem] object. Make sure you call add() on cellItem and make sure you gave the added component passed in 'componentId' id. ( *cellItem*.add(new MyComponent(*componentId*, rowModel) )");
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ public interface ICellPopulator<T> extends IClusterable, IDetachable
*/
void populateItem(final Item<ICellPopulator<T>> cellItem, final String componentId,
final IModel<T> rowModel);

boolean isVisible();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this to be be

default boolean isVisible() {
    return true;
}

and no setter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also JavaDoc.


void setVisible(boolean visible);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As said I would prefer this to be only read-only

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class PropertyPopulator<T> implements ICellPopulator<T>
{
private static final long serialVersionUID = 1L;
private final String property;
private boolean visible = true;

/**
* Constructor
Expand Down Expand Up @@ -74,4 +75,16 @@ public void populateItem(final Item<ICellPopulator<T>> cellItem, final String co
{
cellItem.add(new Label(componentId, new PropertyModel<>(rowModel, property)));
}

@Override
public boolean isVisible()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment above.

{
return visible;
}

@Override
public void setVisible(boolean visible)
{
this.visible = visible;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public abstract class AbstractColumn<T, S> implements IStyledColumn<T, S>

private final S sortProperty;

private boolean visible = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment about read only


/**
* @param displayModel
* model used to generate header text
Expand Down Expand Up @@ -92,4 +94,16 @@ public String getCssClass()
{
return null;
}

@Override
public boolean isVisible()
{
return visible;
}

@Override
public void setVisible(boolean visible)
{
this.visible = visible;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ protected Iterator<IModel<IColumn<T, S>>> getItemModels()

for (IColumn<T, S> column : table.getColumns())
{
columnsModels.add(Model.of(column));
if (column.isVisible())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is the only involved toolbar that needs to be fixed... Did you run

mvn clean install

?

{
columnsModels.add(Model.of(column));
}
}

return columnsModels.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class Contact implements IClusterable

private String cellPhone;

private String age;

/**
* Constructor
*/
Expand All @@ -51,7 +53,7 @@ public Contact()
public String toString()
{
return "[Contact id=" + id + " firstName=" + firstName + " lastName=" + lastName +
" homePhone=" + homePhone + " cellPhone=" + cellPhone + "]";
" homePhone=" + homePhone + " cellPhone=" + cellPhone + " age=" + age + "]";
}

/**
Expand All @@ -74,7 +76,8 @@ public boolean equals(final Object obj)
return other.getFirstName().equals(getFirstName()) &&
other.getLastName().equals(getLastName()) &&
other.getHomePhone().equals(getHomePhone()) &&
other.getCellPhone().equals(getCellPhone());
other.getCellPhone().equals(getCellPhone()) &&
other.getAge().equals(getAge());

}
else
Expand Down Expand Up @@ -174,4 +177,20 @@ public void setLastName(final String lastName)
{
this.lastName = lastName;
}

/**
* @return age
*/
public String getAge()
{
return age;
}

/**
* @param age
*/
public void setAge(String age)
{
this.age = age;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public Contact generate()
contact.setId(generateId());
contact.setHomePhone(generatePhoneNumber());
contact.setCellPhone(generatePhoneNumber());
contact.setAge(generateAge());
return contact;
}

Expand Down Expand Up @@ -101,6 +102,11 @@ private String generatePhoneNumber()
.toString();
}

private String generateAge()
{
return new StringBuilder().append(rint(1, 999)).toString();
}

private int rint(final int min, final int max)
{
return (int)(Math.random() * (max - min) + min);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,22 @@ public String getCssClass()

columns.add(new PropertyColumn<Contact, String>(new Model<>("Home Phone"), "homePhone"));
columns.add(new PropertyColumn<Contact, String>(new Model<>("Cell Phone"), "cellPhone"));

IColumn<Contact, String> ageColumn = new PropertyColumn<Contact, String>(new Model<>("Age"), "age");
columns.add(ageColumn);

@SuppressWarnings({ "rawtypes", "unchecked" })
DefaultDataTable defaultDataTable = new DefaultDataTable("table", columns,
new SortableContactDataProvider(), 8)
{
private static final long serialVersionUID = 1L;

@Override
protected void onConfigure()
{
super.onConfigure();
ageColumn.setVisible(false);
}

@Override
protected IModel getCaptionModel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,16 @@ public IResourceStream getMarkupResourceStream(MarkupContainer container,
}

}
/**
* Tests that a {@link DataTable} with non-visible column will not be rendered.
*/
@Test
public void testWicket6941()
{
DataTablePage page = new DataTablePage();
tester.startPage(page);
tester.assertRenderedPage(DataTablePage.class);
assertTrue(tester.getLastResponseAsString().contains("<span wicket:id=\"label\">ID</span>"));
assertFalse(tester.getLastResponseAsString().contains("<span wicket:id=\"label\">Age</span>"));
}
}