Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
By aura9k1
#815382
Good day,

I'm trying to use a Dropdown in a Grid. The dropdown populates from a DB using a SQL command (this works fine). I'm trying to get the value of what the user has selected from the dropdown. I can't seem to select the dropdown on its own and use getText() as it always returns null (the field is editable).
Code: Select all
$("#myDropdown").getText(1,1);

Instead I have to check when the grid is changed, and then get the value from the dropdown, which in this case is a first column field.
Code: Select all
$("#form\\[myGrid\\]\\[1\\]\\[myDropdown\\]").change( func );

function func (){
var myVar = $("#myGrid").getText (1,1);
alert(myVar);
}
The problem with this code is that it returns the value of what was previously displayed in the Dropdown, not what the user has currently selected. Even getValue(1,1) returns the previous key, not the current one.

What would be the best way to get the current value of a (dropdown) field in a Grid?
User avatar
By amosbatto
#815413
You need to use form.setOnchange() to get the value of the changed grid field. See:


Getting the text label of the selected option in a dropdown in a grid, is tricky. You can use this javascript:
Code: Select all
var formId = $("form").prop("id");
$("#"+formId).setOnchange(function(fieldId, newVal, oldVal) {
  var matches = fieldId.match(/\[(\d)\]\[buyer\]/);
  if (matches) {
    var rowNo = matches[1];
    var aOpts = $("[id='form[receiptList]["+rowNo+"][buyer]']").find("option");
    var label = '';
    for (idx in aOpts) {
      if (aOpts[idx].value == newVal) {
        label = aOpts[idx].text; 
        break;
      }
    }
    alert(label);
  }
});
Where the ID of the grid is "receiptList" and the ID of the grid field is "buyer".
Here is a Dynaform to test this code:
(1.58 KiB) Downloaded 235 times

🚀 Tauchen Sie mit Immediate Alora AI in die Welt d[…]

Do you want a quick way to delete passwords from P[…]

Try the CloudMigration PST Converter to convert […]

In the rapidly evolving world of online sports be[…]