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?
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?
$(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?
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.
$(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?
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
}
?>
}
});
mrlux wrote:
$('#select2-x_branch_id-container').empty();
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.)
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...
CurrentValue
of the field as $_SESSION['branch_id']
(if it exists) by Row_Rendered server event.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.