Page 1 of 1
How to inizialize select2 control on add page client side
Posted: Sat Feb 11, 2023 7:01 am
by gmandelli
Hello,
when adding a record a could inizialize a text box using client side script with
document.getElementById("x_my_table_field").setAttribute("value", my_Value);
I tried to do something similar for a select2 control associated to a lookup filed but without success.
I first tried
document.getElementById("x_id_zip_code").setAttribute("value", myZip);
document.getElementById("x_id_zip_code").trigger('change');
but i couldn't end with the select box inizialized with myZip
What's wrong?
How should I do it?
Regards,
John
Re: How to inizialize select2 control on add page client side
Posted: Sat Feb 11, 2023 7:19 am
by darkdragon
Use some jQuery
myZip = ...;
$('select[id="x_id_zip_code"]').val(myZip).change();
Re: How to inizialize select2 control on add page client side
Posted: Mon Feb 13, 2023 1:03 am
by gmandelli
Hi,
this doesn't end to having myZip as search text in the combo list.
Other suggestions?
Regards,
John
Re: How to inizialize select2 control on add page client side
Posted: Mon Feb 13, 2023 7:43 am
by darkdragon
Hello,
It's unclear what your requirements are.
Re: How to inizialize select2 control on add page client side
Posted: Mon Feb 13, 2023 9:05 am
by MichaelG
The select2 options are loaded by Ajax, and they may not not available yet when your script is running.
Try enabling Lookup Cache for the field in the add page (e.g. in the Page_Load server event) so that the lookup options are available when your script is running. For example:
$this->Field->UseLookupCache = true;
Re: How to inizialize select2 control on add page client side
Posted: Mon Feb 13, 2023 4:13 pm
by darkdragon
On which case. the above code should be put inside updatedone event:
$(document).on("updatedone", function(e, args) {
myZip = ...;
$('select[id="x_id_zip_code"]').val(myZip).change();
});
Re: How to inizialize select2 control on add page client side
Posted: Tue Feb 14, 2023 5:32 am
by gmandelli
In server event page load I put
CurrentPage.id_zip_code.UseLookupCache = true;
On client script I put
$('select[id="x_id_zip_code"]').val(myZip).change();
and
$(document).on("updatedone", function(e, args) {
console.log(args); // Uncomment to view the arguments in browser console
});
When I open the add page passing myZip in query string (http://localhost:5000/TblUcDealersAdd?CallingNumber=503&Zip=20100
)
the alert show up twice:
x_id_zip_code has been updated.
and
undefined has been updated.
Anyway the passed value (20100) is not shown in the listbox.
The idea is to help the operator pre-compiling the fileds in the add page with known parameters.
Regards,
John
Re: How to inizialize select2 control on add page client side
Posted: Tue Feb 14, 2023 3:34 pm
by darkdragon
You should put in Startup Script like this:
var myZip = ....;
$(document).on("updatedone", function(e, args) {
console.log(args); // Uncomment to view the arguments in browser console
$('select[id="x_id_zip_code"]').val(myZip).change();
});
Re: How to inizialize select2 control on add page client side
Posted: Tue Feb 14, 2023 11:45 pm
by gmandelli
Hi, this is not working too.
The combobox remains unfilled\not inizialized.
Maybe if the way aspnetmaker fills the combobox
<option value="" data-select2-id="2"> </option>
<option value="1">90001, Los Angeles, California, Area 2</option><option value="2">37011, Nashville, Tennessee</option>
I need to show the record that contains, for example, 90001 when the page is loaded.
Mant thanks.
Regards,
John