Skip to content

Displaying fields rendered in a custom way

ikostenko edited this page Apr 12, 2013 · 6 revisions

Sometimes it is useful to display extended information about an entity item, such as the total amount of nights per booking, or a composite product code (f.ex., SKU)

To create such a field in e.g. ListView, follow the steps below:

  • define the calculation logic in a class implementing org.lightadmin.core.config.domain.renderer.FieldValueRenderer interface;
  • pass the class instance to renderable() method of ListView definition in the administration class;
@Administration( Booking.class )
public class BookingAdministration {

	public static FieldSetConfigurationUnit listView( final FieldSetConfigurationUnitBuilder fragmentBuilder ) {
		return fragmentBuilder
			.field( "user" ).caption( "Customer" )
			.field( "hotel" ).caption( "Hotel" )
			.field( "smoking" ).caption( "Smoking" )
			.field( "beds" ).caption( "Beds" )
			.renderable( nightsValueRenderer() ).caption( "Nights" )
			.renderable( totalValueRenderer() ).caption( "Total" )
			.build();
	}

	public static FieldSetConfigurationUnit quickView( final FieldSetConfigurationUnitBuilder fragmentBuilder ) {
		return fragmentBuilder
				.field( "checkinDate" ).caption( "Check-In Date" )
				.field( "checkoutDate" ).caption( "Check-Out Date" )
				.renderable( hotelCodeRenderer() ).caption( "Hotel Code" ).build();
	}

	public static FieldValueRenderer<Booking> nightsValueRenderer() {
		return new FieldValueRenderer<Booking>() {
			@Override
			public String apply( final Booking booking ) {
				return String.valueOf( booking.getNights() );
			}
		};
	}

	public static FieldValueRenderer<Booking> totalValueRenderer() {
		return new FieldValueRenderer<Booking>() {
			@Override
			public String apply( final Booking booking ) {
				return String.valueOf( booking.getTotal() );
			}
		};
	}

	private static FieldValueRenderer<Booking> hotelCodeRenderer() {
		return new FieldValueRenderer<Booking>() {
			@Override
			public String apply( Booking booking ) {
				return String.format("%s-%s-%s", booking.getHotel().getId(), booking.getHotel().getZip(), booking.getHotel().getName());
			}
		};
	}
}

The above example will render the following result: RenderableFields