Displaying Fields Rendered in a Custom Way

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 List View, Quick View, or Show View, 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;
public class BookingAdministration extends AdministrationConfiguration {

	public 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 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 totalValueRenderer() {
		return new FieldValueRenderer() {
			@Override
			public String apply( final Booking booking ) {
				return String.format( "\u20AC %s", booking.getTotal() );
			}
		};
	}

	private static FieldValueRenderer hotelCodeRenderer() {
		return new FieldValueRenderer() {
			@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:

Fields with custom content: Total, Hotel Code