xdev-software / vaadin-grid-exporter

Makes it possible to export Vaadin Grids to different formats
Apache License 2.0
25 stars 2 forks source link

is it possible to load the columns header using a headerrow? #268

Closed juanmacintas closed 1 month ago

juanmacintas commented 1 month ago

Checklist

What is/are your question(s)?

I have created a grid and i add the columns not setting the header like this: peticionColumn = gridPeticion.addColumn(PeticionVDto::cdpetici).setFlexGrow(1).setSortable(true) Cause i have a HeaderRow to include filters. HeaderRow headerRow = gridPeticion.appendHeaderRow(); headerRow.getCell(peticionColumn).setComponent( createFilterHeader("Petición", peticionFilter::setCdpetici));

I create the exporter like this: btExportar = new Button("Exportar", VaadinIcon.PRINT.create() ,e -> GridExporter .newWithDefaults(this.gridPeticion) .withLocalizationConfig(guiaLocalizationConfig()) .open()); Whith this configuratión, when i click on btExportar and tab General is showed, the header is not labeled. Is it possible to preload them with an header row?

Thanks in advance

Additional information

I'm using vaadin 24.4.6 and 3.2.3-SNAPSHOT of the library vaadin-grid-exporter

AB-xdev commented 1 month ago

Hi,

I think the most simple solution to this is using a different ColumnHeaderResolvingStrategy.

For example you could use ManualColumnHeaderResolvingStrategy like this:

GridExporter.newWithDefaults(this.grExamples)
    .withColumnConfigurationBuilder(new ColumnConfigurationBuilder()
        .withColumnConfigHeaderResolvingStrategyBuilder(b -> b.withManualColumnHeaderStrategy(...)))
    // ...

or write your own custom HeaderResolver.

Hope this helps :)

juanmacintas commented 1 month ago

Hi @AB-xdev , Thanks for your reply,

I tried with this: e -> GridExporter .newWithDefaults(this.gridPeticion) .withLocalizationConfig(guiaLocalizationConfig()) .withColumnConfigurationBuilder(new ColumnConfigurationBuilder().withColumnConfigHeaderResolvingStrategyBuilder(b -> b.withStrategy(new VaadinColumnHeaderResolvingStrategy()))) .open());

with the same problem. I don't know how to user the solution of manualColumnheaderStrategy. If i code like the example `e -> GridExporter .newWithDefaults(this.gridPeticion) .withLocalizationConfig(guiaLocalizationConfig()) .withColumnConfigurationBuilder(new ColumnConfigurationBuilder().withColumnConfigHeaderResolvingStrategyBuilder(b -> b.withManualColumnHeaderStrategy(col -> col.getKey(),headerTextResolverMap()))) .open());

and

private Map headerTextResolverMap() {

    return  Map.of( "col1", "Column1", "col2", "Column2",.....  

} I get an error at: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.function.Function (java.lang.String and java.util.function.Function are in module java.base of loader 'bootstrap') at software.xdev.vaadin.grid_exporter.column.headerresolving.ManualColumnHeaderResolvingStrategy.resolve(ManualColumnHeaderResolvingStrategy.java:56) ~[classes/:na]`

I'm sure it's my fault, headerTextResolverMap returns a Map and not a Function. Im' just trying to follow the sample wrote in ManualColumnHeaderResolvingStrategy class' header

Thanks for you help

AB-xdev commented 1 month ago

@juanmacintas

I tried with this ... with the same problem.

Yes because this is the default option.

I get an error at: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.function.Function ... ... the sample wrote in ManualColumnHeaderResolvingStrategy class

Unfortunately the example is outdated.

You have to supply a function because support for e.g. translations was added a few releases ago.

Should work like this now:

new ManualColumnHeaderResolvingStrategy(
  col -> col.getKey(), 
  Map.of(
    "name", k -> "Username", 
    "pw", k -> "Password"))

Also please try to format your code/stacktrace properly in Markdown it's very hard to read. Thank you.

juanmacintas commented 1 month ago

Thanks for your help! with that Map the headers are well loaded.