Dependent editor field not updated with $.ajax call in function

Dependent editor field not updated with $.ajax call in function

yetyet Posts: 43Questions: 17Answers: 1

I have this method as a dependent call for fetching some data from the back and assiging the return value to "bezeichnung". The AJAX call is executed correctly, however there is no update of the field. The approach works without the AJAX call with some dummy data. Anything missing here?

   function cas_nr_to_str(cas_nr) {
      $.ajax({
        url: `/gefahrenstoffe/${cas_nr}/json`,
        type: 'GET',
        dataType: 'json',
        async: false,
        success: function(data) {
          var cas_str = data.cas_str;
          return {
            values: {
              bezeichnung: cas_nr + "x"
            }
          };
        },
        error: function(error) {
          console.error('Error:', error);
        }
      });
    }


    editor_create.dependent('cas_nr', cas_nr_to_str);
    editor_edit.dependent('cas_nr', cas_nr_to_str);

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    The return is returning inside the success function - which goes nowhere. Editor doesn't see that. You need to use a callback function that is passed in as the third parameter to your handling function by dependent():

    function cas_nr_to_str(cas_nr, row, callback) {
       $.ajax({
         url: `/gefahrenstoffe/${cas_nr}/json`,
         type: 'GET',
         dataType: 'json',
         async: false,
         success: function(data) {
           var cas_str = data.cas_str;
           callback({
             values: {
               bezeichnung: cas_nr + "x"
             }
           });
         },
         error: function(error) {
           console.error('Error:', error);
         }
       });
     }
     
     
     editor_create.dependent('cas_nr', cas_nr_to_str);
     editor_edit.dependent('cas_nr', cas_nr_to_str);
    

    Allan

  • yetyet Posts: 43Questions: 17Answers: 1

    Thanks!

Sign In or Register to comment.