How to inizialize select2 control on add page client side

This public forum is for user-to-user discussions of ASP.NET Maker. Note that this is not support forum.
Post Reply
gmandelli
User
Posts: 32

How to inizialize select2 control on add page client side

Post 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


darkdragon
User
Posts: 148

Post by darkdragon »

Use some jQuery

myZip = ...;
$('select[id="x_id_zip_code"]').val(myZip).change();

gmandelli
User
Posts: 32

Post by gmandelli »

Hi,
this doesn't end to having myZip as search text in the combo list.

Other suggestions?

Regards,
John


darkdragon
User
Posts: 148

Post by darkdragon »

Hello,

It's unclear what your requirements are.


MichaelG
User
Posts: 1095

Post 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;


darkdragon
User
Posts: 148

Post 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();

});

gmandelli
User
Posts: 32

Post 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


darkdragon
User
Posts: 148

Post 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();
});

gmandelli
User
Posts: 32

Post 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">&nbsp;</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


Post Reply