2

I have a scenario where a user selects a row of data to edit and I want an EntityRef Select2 box to be populated with a piece of information from that row.

The EntityRef select2 widget works for selecting and submitting data, but I can't seem to select a value programmatically.

I have tried a number of solutions with varying levels of non-success. Sometimes, the element doesn't change at all. Sometimes it says "undefined".

The below selects the first element in the list, but then enters some sort of infinite loop, where according to the console a requests are constantly being sent and I'm unable to open the Select2 dropdown because of it.

$('#s2id_dc_group_id').select2('val', dcName); //infinite loop.

UPDATE:

Here is the HTML for the input field that we will be using:

<input id="dc_group_id" class="form-control" name="dc_group_field" placeholder="{ts}- select DC -{/ts}" />

And here is where we setup the EntityRef (we call this function inside a document ready block):

function SetupEntityRef() {
    CRM.api3('Group', 'get', {
        "sequential": 1,
        "title": "Discipleship Community",
        "return": ["children"],
    }).done(function (result) {
        console.log(result);
        var dc_groups = result['values'][0]['children'].split(',');
        console.log('DC Groups: ' , dc_groups);
        $('[name=dc_group_field]').crmEntityRef({
            entity: 'group',
            api: {
                params: {
                    id: {"IN":dc_groups},  // dc ids
                }},
            create: false,
            select: {minimumInputLength: 0} // show results without having to search
        });

    });
}
  • Are you using CiviCRM? – Pradeep Nayak Oct 8 '18 at 19:42
  • @PradeepNayak Yes, CiviCRM and Drupal. – Alec Macleod Oct 8 '18 at 19:45
  • So can you please mention the field name you trying to select? – Pradeep Nayak Oct 8 '18 at 19:47
  • The name of the field (before select2 is applied to it) is "dc_group_field". – Alec Macleod Oct 8 '18 at 19:52
  • So did you try $('#dc_group_field').val(dcName); – Pradeep Nayak Oct 8 '18 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.