Skip to content

Displaying fields rendered in a custom way

ikostenko edited this page Apr 18, 2013 · 6 revisions

Sometimes it is useful to display extended information about an entity item, such as composite product code (f.ex., SKU), or booking total together with currency sign.

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

  • define the field value calculation logic in a class implementing org.lightadmin.core.config.domain.renderer.FieldValueRenderer interface;
  • pass the class instance to renderable() method of FieldSetConfigurationUnit section 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( 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> totalValueRenderer() {
		return new FieldValueRenderer<Booking>() {
			@Override
			public String apply( final Booking booking ) {
				return String.format( "\u20AC %s", 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