A simple CRUD interface for Lift/MongoDB object reference fields

November 03, 2012 – tagged Lift, Howto, Scala, Programming

While Lift provides a nice CRUD interface for simple data types such as Strings and Booleans, there is no out-of-the-box interface to work with reference fields, e.g., the ObjectIdRefListField in Record's MongoDB package. A nice way to integrate such an interface into the existing CRUD framework is to override the toForm method of ObjectIdRefListField. Thus, I created a trait One2ManyCRUD (which should actually be called Many2ManyCRUD, but I guess since I store the “many” references with the “one” object I'm editing, this is ok...) that can be mixed into the ObjectIdRefListField:

where the options function should return all allowed values for this field, such that you can define your reference field as follows:

The form generated by the One2ManyCRUD trait is a classical HTML multiselect list. Using the amazing select2 library, this can easily be turned into a very well usable interface by adding

<script type="text/javascript">

to the template, see http://ivaynberg.github.com/select2/#multi for an example.