on_field_select_value

on_field_select_value(field, lookup_item)

domain: client

language: javascript

class Item class

Description

When user clicks on the button to the right of the field input or uses typeahead, the application creates a copy of the lookup item of the field and triggers on_field_select_value event. Use on_field_select_value to specify fields that will be displayed, set up filters for the lookup item, before it will be opened.

In short, on_field_select_value is used for locating or identifying the record on the lookup table when we click on the lookup icon.

The field parameter is the field whose data will be selected.

The lookup_item parameter is a copy of the lookup item of the field

Example

function on_field_select_value(field, lookup_item) {
    if (field.field_name === 'customer') {
        lookup_item.set_where({lastname__startwith: 'B'});
        lookup_item.view_options.fields = ['firstname', 'lastname', 'address', 'phone'];
    }
}

Or, generic code with different ID for each table:

function on_field_select_value(field, lookup_item) {
    const field_map = {
        album: 'id',
        genre: 'genreid',
        artist: 'artist_id'
    };

    const target_field = field_map[field.field_name];
    if (target_field && field.value) {
        const where = {};
        where[target_field + '__eq'] = field.value;
        lookup_item.set_where(where);
    }
}

See also

Fields

Lookup fields