Thanks for the suggestion
I looked at the function in ew.js
And it seems that it takes one row at a time and returns a True or False on whether to add it.
However it doesn't seem that it runs for the rows in the Child Lookup field, only "Parent" Lookup fields during page load.
So i tried to hook changes to the parent lookup field through a jquery on change selector.
But it seems that while I try add the values, when I click the select2 there is nothing in it. I assume it is doing some check onclick or something and ensure there is nothing in there?
$(document).on('change', 'input[name^="x_ParentField"]', function() {
// Check which parent values are selected
var checkedParentFieldValues = $('input[name^="x_ParentField"]:checked').map(function() {
return this.value;
}).get().join(',');
// Fetch values for ChildField via custom API
// Construct the API URL
var apiUrl = ew.PATH_BASE + "api/get_ChildLookupFieldValues?ParentField=" + checkedParentFieldValues;
// Fetch and update options for LookupField
$.get(apiUrl, function(response) {
var rows = response.data; // Make sure 'data' is the correct key that contains your options array
// response is like { "data": [{ "lf": 1001, "df": "A1", "df2": "", "df3": "", "df4": "" },{ "lf": 1002, "df": "A2", "df2": "", "df3": "", "df4": "" }]}
// try to ensure can clear select2 values if any
$('#x_ChildField\\[\\]').select2({ allowClear: true });
// Try to clear existing options (didn't clear)
var $childField = $('#x_ChildField\\[\\]').empty();
// Dynamically add new options from the response
$.each(rows, function(index, row) {
var newOption = new Option(row.df, row.lf, false, false);
$childField.append(newOption);
// $("<option>", { value: row.lf, text: row.df }).appendTo("#x_ChildField\\[\\]"); // also tried #x_ChildField
});
// Notify Select2 (if applicable) of the update
$childField.trigger('change');
}).fail(function() {
console.error("Failed to fetch data for LookupField.");
});
But I could seem to get it to work in this fashion either.
So I tried turning off the lookup options and just use a select with multiple option checked, then tried populating it similar to the last example (with all the "use lookup table" unchecked for both "ParentField" and "ChildField"
And then just attempted to insert data via the chrome console...
$("<option>", { value: 1101, text: "My Value 1101" }).appendTo("#x_ChildField\\[\\]");
This minimal test worked but it was no longer a select2 dialog.
So ideally I could use the lookupTables but populate ChildField with all values on page load (and if all ParentField's are deselected). But I am still unclear how this can be achieved