Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

Wicket: How can I hide a column in the DataTable when each cell is empty or contains a hidden component?

I have a column in my table where I populate item with the following code:

    @Override
    public void populateItem(Item<ICellPopulator<T>> item, String componentId, IModel<T> model) {
        if (deleteEnabled(model)) {
            item.add(new ConfirmationLink<>(componentId).onClick(target -> deleteRow(...)));
        } else {
            item.add(new Label(componentId));
        }
    }

So when user has enough grants to delete an entity, he can see the icon in the row, if doesn't he only sees an empty cell.

Where can I check for each model in the table that the user does not have permission to delete any entity and prevent the column from being rendered in the table?

Update

I would like to add some code to explain my case.

This is a very common example of using a table in my application.

public class CenterPanel extends SWPanel<CourierSearchQuery> {

    public CenterPanel(String id) {
        super(id, new Model<>(new CourierSearchQuery()));
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();
 
        List<IColumn<Courier, String>> columns = List.of(

                new StyledColumn<Courier>("Ф.И.О.", "name.FIO", "name.FIO")
                        .setCreateComponentFunction((id, model) ->
                                new LinkPanel(id, model.map(n -> n.getName().getFIO()), model)),

                new StyledColumn<Courier>("Статус", "state.title")
                        .setCssClass(CSS.width(100)),

                new StyledColumn<Courier>("Телефон", "phone")
                        .setCssClass(CSS.width(100)),

                new DeleteColumn<>() // <- this column should be hideable
        );

        add(new EntityDataTable<>("table", columns, getModel()));
    }
}

There is an JPA entity (Courier), POJO parameter object (CourierSearchQuery) where I can set criteria for filtering data and EntityDataTable. The last one contains a dataprovider that can find a DAO for the entity, call a search method with filter parameters and create an iterator over it.

Also there is a DeleteColumn class. Under the hood it checks user's permissions for current entity and shows an ajax link.

I would like to mark it with the following interface and hide it inside a DataTable class (if needed):

public interface HideableColumn<T> {

    boolean isCellVisible(T object);
}

To implement hiding I've write following:


    private List<? extends IColumn<T, String>> allColumns;

    @Override
    protected void onConfigure() {
        super.onConfigure();

        if (allColumns == null) allColumns = new ArrayList<>(getColumns());

        if (allColumns.stream().anyMatch(e -> e instanceof HideableColumn)) {
            getColumns().clear();

            Set<T> objects = new HashSet<>();
            getDataProvider().iterator(getCurrentPage() * getItemsPerPage(), getItemsPerPage()).forEachRemaining(objects::add); // <-1

            allColumns.stream()
                    .filter(e -> !(e instanceof HideableColumn) || objects.stream().anyMatch(((HideableColumn<T>) e)::isCellVisible))
                    .forEach(e -> getColumns().add(e)); // <-2
        }
    }

But this code has two problems: 1 - it makes the table calls dataProvider#iterator() twice 2 - it doesn't compile because of dataTable#getColumns() has wildcard in declaration


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So if all the rows in a column are empty, you do not what do display the column at all?

I propose two options in this case:

  1. In Wicket: Check the permissions first and do not add the column in the first place
  2. In JavaScript: Check if the column is empty and hide it, there are probably solutions for that on the web.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...