Page 1 of 1

select2 selection

Posted: Tue Nov 29, 2022 4:54 pm
by konfuzion

<select id="x_myfield" ...>
<option value="1">one</option>
<option value="2">two</option>

$('#x_myfield').val('1'); // Select the option with a value of '1'
$('#x_myfield').trigger('change'); // Notify any JS components that the value changed

I added this code to make custom selection from select2 on initial load but nothing is being selected.

in jsfiddle it works but through phpmaker it doesn't work

What did I miss?


Re: select2 selection

Posted: Tue Nov 29, 2022 5:26 pm
by arbei

You may search "updatedone" in this forum or in the docs.


Re: select2 selection

Posted: Tue Nov 29, 2022 6:16 pm
by mobhar

Alternatively, if you don't want to use jQuery/Javascript, then you may simply use Row_Rendered server event to display the selected value in Select2 control when the page has been just loaded.

Let's say you want to make customer Around the Horn (AROUT) as the default selected value in CustomerID field in Add Page of orders table, then simply put this following code in Row_Rendered server event that belongs to the orders table:

if (CurrentPageID() == "add") 
    $this->CustomerID->CurrentValue = "AROUT";

Re: select2 selection

Posted: Tue Nov 29, 2022 6:18 pm
by konfuzion

I'm trying to get it working in the Modal Multi-Update Window but there's something wrong with this code. I want to trigger the auto select after the Modal window is shown.

$(window).on('shown.bs.modal', function (e) {
	// do something...
	$(document).on('updatedone', function(e, args) {			
		$('#x_myfield').val('2'); // Select the option with a value of '2'
	});
	}

Re: select2 selection

Posted: Tue Nov 29, 2022 6:33 pm
by mobhar

I suggest you to simply use Row_Rendered server event instead of Startup Script, since the server event will also handle the page that displayed via Modal dialog window.

For Multi-Update and using the example above, you may only change that code to:

if (CurrentPageID() == "update") 
    $this->CustomerID->CurrentValue = "AROUT";

and make sure you have already enabled Multi-Update and Modal dialog option under Table setup -> Multi-Update Page of current table.


Re: select2 selection

Posted: Tue Nov 29, 2022 7:13 pm
by konfuzion

Tks for the suggestion but actually, I want to make the Modal Multi-Update more flexible and dynamic.

For example, I have 4 different buttons. and 20 unique fields

4 groups
1 group = 1 button + 5 unique fields

In List page are 4 buttons, clicking on the button results in different Multi-Update Modal Windows.

Group 1 = If click button#1 then Modal pops up with 5a fields displayed with the correct option selected
Group 2 = If click button#2 then Modal pops up with 5b fields displayed with the correct option selected
Group 3 = If click button#3 then Modal pops up with 5c fields displayed with the correct option selected
Group 4 = If click button#4 then Modal pops up with 5d fields displayed with the correct option selected

So it's best if I can do everything in Jquery so it's fully dynamic.

Currently, hiding the 15 other fields and checking the checkboxes works for each group, only the auto-select of select options is not working (yet)


Re: select2 selection

Posted: Tue Nov 29, 2022 8:07 pm
by konfuzion

I see, I think Row_Rendered server event for default selecting may be useable in my case also, will give it a try.