Using "updatedone" client side event

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
mrlux
User
Posts: 115

Using "updatedone" client side event

Post by mrlux »

I add code in table add -> Startup Script

$(document).on('updatedone', function(e, args) {
console.log(7777);	
});	

and get '7777 ' 4 time in (F12). Why?

I need it once.

How to make sure that control is only for those select that are necessary?


arbei
User
Posts: 9288

Post by arbei »

You should check the field name in your code, you may read the example in docs -> Example 3.


mrlux
User
Posts: 115

Post by mrlux »

$(document).on('updatedone', function(e, args) {
$("#x_branch_id_from [value="+<?php echo $_SESSION['branch_id']; ?>+"], #x_branch_id_to [value="+<?php echo $_SESSION['branch_id']; ?>+"]").attr("selected", "selected");

$("#x_user_id [value="+<?php echo $_SESSION['user_id']; ?>+"]").attr("selected", "selected");

// Start
	$.ajax({ 
		method: "post",
		url: "all_cashboxes.php",
		data: {user_id: <?php echo $_SESSION['user_id']; ?>, branch: <?php echo $_SESSION['branch_id']; ?>},	 
		dataType: "JSON",
	  	success: function(data) {
		for (var i=0; i<data.num; i++) {
			if (data.data[i]['id'] == <?php echo $_SESSION['cashbox_id']; ?>) {
		$('#x_cashbox_id_from').append('<option selected="selected" value='+data.data[i]['id']+' >'+data.data[i]['name']+'</option>');
			} else {
		$('#x_cashbox_id_from, #x_cashbox_id_to').append('<option value='+data.data[i]['id']+' >'+data.data[i]['name']+'</option>');
			}
		}	
	  }
	});
// End

});

How to make this part (Start - End) select automatically?

Or do I need to upgrade to version 2023 to do this?


arbei
User
Posts: 9288

Post by arbei »

mrlux wrote:

How to make this part (Start - End) select automatically?

Your "success" function adds options, you should select what you need after adding options. (Version is irrelevant.) To select options, it depends on your Edit Tag, if it is a Select2 element, read Selecting options.


mrlux
User
Posts: 115

Post by mrlux »

$(document).on('updatedone', function(e, args) {

<?php
$_SESSION['rows'] = ExecuteRows("SELECT * from cashbox where branch_id = '".$_SESSION['branch_id']."' order by name");
$_SESSION['num'] = ExecuteScalar("SELECT count(id) from cashbox where branch_id = '".$_SESSION['branch_id']."' ");
?>

// if we pass field 'x_cashbox_id_from', to do
var $target = $(args.target);
if ($target.data("field") == 'x_cashbox_id_from') {

// get all cashboxes from branch
<?php
	for ($i=0; $i<$_SESSION['num']; $i++) {
		$id = $_SESSION['rows'][$i]['id'];
		$cashbox_id = $_SESSION['cashbox_id'];
		if ($id == $cashbox_id) {
?>			
		$('#x_cashbox_id_from').append('<option selected="selected" value='+'<?php echo $_SESSION['rows'][$i]['id']; ?>'+' >'+'<?php echo $_SESSION['rows'][$i]['name']; ?>'+'</option>');
<?php			} else
			{ ?>
		$('#x_cashbox_id_from, #x_cashbox_id_to').append('<option value='+'<?php echo $_SESSION['rows'][$i]['id']; ?>'+' >'+'<?php echo $_SESSION['rows'][$i]['name']; ?>'+'</option>');
<?php		} 
	}	?>
}
});

This work good, but if I click 'cancel' in page 'add' and again click 'add', then get double records in select.

How to solve this problem?


mobhar
User
Posts: 11660

Post by mobhar »

You should remove the items first before add the items using .append() method.


mrlux
User
Posts: 115

Post by mrlux »

My code good work in v2020, but not work in v2023

Add/Copy Page -> Startup Script

<?php    
$branch_name = ExecuteScalar("SELECT name from branch where id = {$_SESSION['branch_id']} ");
$_SESSION['rows'] = ExecuteRows("SELECT * from branch order by name ");
$_SESSION['num'] = ExecuteScalar("SELECT count(id) from branch "); 
?>

$(document).on('updatedone', function(e, args) {
var $target = $(args.target);
if ($target.data("field") == 'select2-x_branch_id-container') {
$('#select2-x_branch_id-container').empty();
 
<?php
for ($i=0; $i<$_SESSION['num']; $i++) {
?>    
if (<?php echo $_SESSION['rows'][$i]['id']; ?> == <?php echo $_SESSION['branch_id']; ?> ) {

$('#select2-x_branch_id-container').append('<option selected value='+'<?php echo $_SESSION['branch_id']; ?>'+' >'+'<?php echo $branch_name; ?>'+'</option>');
} else {
    $('#select2-x_branch_id-container').append('<option value='+'<?php echo $_SESSION['branch_id']; ?>'+' >'+'<?php echo $branch_name; ?>'+'</option>');
}
<?php
}
?>
} 
});

arbei
User
Posts: 9288

Post by arbei »

mrlux wrote:

$('#select2-x_branch_id-container').empty();

  1. You should not remove HTML generated by Select2, if you want to change options you may read Add, select, or clear items,
  2. The 'updatedone' event is not for changing options, if you want to use server side PHP code to set options, you better use Lookup_Selecting server event instead.

mrlux
User
Posts: 115

Post by mrlux »

I do not quite understand how I can change the above code for v2023. I need to display the full list of branches, but at the same time show selected only the one that is currently selected in the session.


arbei
User
Posts: 9288

Post by arbei »

Then you should use "updatedone" event (which fires when all options are updated) to select the option. And as said, you should check the field name in your code, you may read the example in docs -> Example 3. (You may also search "updatedone" in this forum for examples.)

To select options, you may read Add, select, or clear items, especially Selecting options.

(There is no need to use PHP to populate the options yourself unless the options are not from the lookup table of the field. In that case, you should use Lookup_Selecting server event as suggested above.)


mrlux
User
Posts: 115

Post by mrlux »

In the dropdown I get all the branch values. I need to select by default only the one that is in the current session.

Field in my table is name "branch_id".

My code is in Add/Copy Page -> Startup Script:

$(document).on('updatedone', function(e, args) {
var $target = $(args.target);
if ($target.data("field") == "x_branch_id") { 
$('#select2-x_branch_id-container').val(<?php echo $_SESSION['branch_id']; ?>);
$('#select2-x_branch_id-container').trigger('change');
    }
});

But it not work...


arbei
User
Posts: 9288

Post by arbei »

You need to select the field (i.e. "x_branch_id"), not its container.


mrlux
User
Posts: 115

Post by mrlux »

$(document).on('updatedone', function(e, args) {
var $target = $(args.target);
if ($target.data("field") == "x_branch_id") {
$('#x_branch_id').val(<?php echo $_SESSION['branch_id']; ?>);
$('#x_branch_id').trigger('change');
}
});

Also not work


arbei
User
Posts: 9288

Post by arbei »

  1. If lookup cache not enabled for the page (see Lookup cache enabled pages), there is no options to select yet when the page is first loaded. You may try to enable lookup cache.
  2. Alternatively, to set the field on page load by PHP, you may just set the CurrentValue of the field as $_SESSION['branch_id'] (if it exists) by Row_Rendered server event.

mobhar
User
Posts: 11660

Post by mobhar »

As arbei mentioned above, you may simply use Row_Rendered server event by setting the CurrentValue of the field as $_SESSION['branch_id'], for example:

if (CurrentPageID() == "add") {
    $this->branch_id->CurrentValue = $_SESSION['branch_id'];
}

If it still does not work, then make sure the $_SESSION['branch_id'] contains the valid value, or your may just change the session variable to your desired static value just to test.


Post Reply