Populate select2 based on selection from another control

Populate select2 based on selection from another control

weskeyweskey Posts: 12Questions: 5Answers: 0

Populate the list of projects so that they’re relative to the customer when adding a contact but only if the customer is changed. Otherwise the values returned with the API are removed.

When creating a new contact in my web application I’m using a select2 control to allow selection of one or more projects to which a contact should be associated.

When creating a new contact, the customer is selected from a select control and I would like the projects select2 control to be populated based on this.

When I try using the following code to do this, the values originally retrieved for the customer are cleared.```
$(editor.field('customer').node()).on('change', function () {
var custId = editor.field('customer').val();
var optionsProjects = [];
$.getJSON('http://localhost:3001/v1/' + custId + '/custProjects', {
term: "-1"
},
function(data) {
var option = {};
$.each(data, function(i, e) {
option.label = e.projName;
option.value = e.projID;
optionsProjects.push(option);
option = {};
});
}
).done(function() {
editor.field('projects[].projId').update(optionsProjects);
})

                } );

```

Is there a better way to do this so that the contact project values retrieved initially are retained for editing / viewing and the projects in the select2 control are updated dynamically based on the selected value of customer?

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    duplicate of this thread.

  • weskeyweskey Posts: 12Questions: 5Answers: 0

    Was on the train with a poor Internet connection and managed to submit this 3 times. Unable to delete.

This discussion has been closed.