Skip to content

Commit

Permalink
fix #932 Wrong UI interaction between datatable SummaryPlugin and Emp…
Browse files Browse the repository at this point in the history
…tyStatePlugin

Adding the ability to remove the summary rows in case of empty data set
  • Loading branch information
vegegoku committed Jul 28, 2024
1 parent 45af1fc commit af02811
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@

package org.dominokit.domino.ui.datatable.plugins.summary;

import static java.util.Objects.isNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.dominokit.domino.ui.datatable.DataTable;
import org.dominokit.domino.ui.datatable.events.TableDataUpdatedEvent;
import org.dominokit.domino.ui.datatable.events.TableEvent;
import org.dominokit.domino.ui.datatable.plugins.DataTablePlugin;
import org.dominokit.domino.ui.datatable.plugins.HasPluginConfig;
import org.dominokit.domino.ui.elements.TFootElement;
import org.dominokit.domino.ui.utils.BaseDominoElement;

Expand All @@ -40,11 +45,13 @@
* @param <T> The type of data in the DataTable.
* @param <S> The type of data in the summary row.
*/
public class SummaryPlugin<T, S> implements DataTablePlugin<T> {
public class SummaryPlugin<T, S>
implements DataTablePlugin<T>, HasPluginConfig<T, SummaryPlugin<T, S>, SummaryPluginConfig> {

private List<SummaryRow<T, S>> summaryRows = new ArrayList<>();
private DataTable<T> dataTable;
private TFootElement footer;
private SummaryPluginConfig config = new SummaryPluginConfig();

/**
* Initializes the SummaryPlugin with the DataTable.
Expand Down Expand Up @@ -74,8 +81,10 @@ public void onFooterAdded(DataTable<T> datatable) {
* @return This SummaryPlugin instance.
*/
public SummaryPlugin<T, S> setSummaryRecords(Collection<S> records) {
summaryRows.forEach(BaseDominoElement::remove);
summaryRows.clear();
removeSummaryRecords();
if (this.config.isRemoveOnEmptyData() && this.dataTable.getRecords().isEmpty()) {
return this;
}
List<S> recordsList = new ArrayList<>(records);
for (int i = 0; i < recordsList.size(); i++) {
SummaryRow<T, S> summaryRow = new SummaryRow<>(recordsList.get(i), i, this.dataTable);
Expand All @@ -86,6 +95,46 @@ public SummaryPlugin<T, S> setSummaryRecords(Collection<S> records) {
return this;
}

public void removeSummaryRecords() {
summaryRows.forEach(BaseDominoElement::remove);
summaryRows.clear();
}

@Override
public void handleEvent(TableEvent event) {
if (TableDataUpdatedEvent.DATA_UPDATED.equals(event.getType())) {
if (config.isRemoveOnEmptyData() && ((TableDataUpdatedEvent<T>) event).getData().isEmpty()) {
removeSummaryRecords();
}
}
}

/**
* Sets the configuration for the SummaryPlugin.
*
* @param config The SummaryPlugin to set.
* @return The SummaryPlugin instance.
*/
@Override
public SummaryPlugin<T, S> setConfig(SummaryPluginConfig config) {
if (isNull(config)) {
this.config = new SummaryPluginConfig();
} else {
this.config = config;
}
return this;
}

/**
* Gets the current configuration of the SummaryPlugin.
*
* @return The current SummaryPluginConfig.
*/
@Override
public SummaryPluginConfig getConfig() {
return config;
}

/**
* Specifies the order in which this plugin should be applied relative to other plugins. Plugins
* with lower order values are applied first.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright © 2019 Dominokit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.domino.ui.datatable.plugins.summary;

import org.dominokit.domino.ui.datatable.plugins.PluginConfig;

public class SummaryPluginConfig implements PluginConfig {

private boolean removeOnEmptyData = false;

/**
* @return boolean, true will cause the plugin to remove the summary records for empty data
* tables, false will keep them, default to false
*/
public boolean isRemoveOnEmptyData() {
return removeOnEmptyData;
}

/**
* Use this to configure the plugin to remove/keep the summary records when the datatable has no
* records.
*
* @param removeOnEmptyData boolean, true to remove the records, false to keep them.
* @return same config instance.
*/
public SummaryPluginConfig setRemoveOnEmptyData(boolean removeOnEmptyData) {
this.removeOnEmptyData = removeOnEmptyData;
return this;
}
}

0 comments on commit af02811

Please sign in to comment.