Thursday, November 26, 2009

XPages Type Ahead - use var property to get field value

I've seen several examples on type-ahead using getComponent( 'componentId' ).getValue(). Using hard-coded field id's can be a bad idea in the long run. If someone changes the id of the type-ahead field, the type-ahead will fail. There's a more maintainable alternative, the var property of the typeAhead-component.

It's not directly available on the field. If you select the <typeAhead>-node in the source, you will get an "All properties" view for the Type Ahead component. Under data, you can set the var property.



If you, like me, like modifying source-code better, add a var attribute to the typeAhead node.



In the suggestions-code, you can now do:
@DbLookup( [ @DbName()[0], 'database.nsf' ], '(lupView)', lupKey, 1, '[PARTIALMATCH]' )


Another tip: Although you can compute Type Ahead on and off in the field, after the field loses focus, it looks like Type Ahead is always enabled. If you select the <typeAhead>-node, you can modify the rendered property (enables/disables type-ahead).

7 comments:

Hilmar Örn said...

Do you know how to get multiple values with this typeahead. Like if i want to choose 4 persons in sendto field.

Tommy Valand said...

I'm not aware of any way to do this out of the box.

The closest you'll get with out of the box functionality is putting a button next to the type ahead field, that adds the selected person (the value of the type ahead field) to the SendTo field (remember to validate the name).

Rasmus said...

Great tip, Tommy!

Do you know if it's possible to do return a partial match not only at the beginning of the column value?

E.g, a search for "John" should return a match for the entry "John Anderson", but also for the entry "Andy Johnson".

Tommy Valand said...

As far as I know, XPages type-ahead suggestions are limited to value*, so *value* won't work.

Rasmus said...

True, Tommy!

Managed to created the "*value*" search by doing some hacking on my own! Will post the details after Easter, if anyone's interested!

Tommy Valand said...

Great! I look forward to it :)

Rasmus said...

As promised, better late than never:
http://devxpages.blogspot.com/2010/04/extending-xpages-type-ahead.html


Feel free to add a comment, if you like!