Selected a value of a second field automatically

Selected a value of a second field automatically

Mairie du PecqMairie du Pecq Posts: 9Questions: 3Answers: 0

Hi,

I have two fields with type "Select2".

I would like that when I select a member from the first field, that a selection of the second one be made automatically.
It works well manualy.

ps : I use Select2 for the autocomplete function (in the first field) and grouping (for the second).

Here i choose "Dylan Bob" and i want that the field "Retour" selected the id of "arret_id" automatically

editor = new $.fn.dataTable.Editor( {
ajax: "includes/join.php?date="+date_sql+"&chauffeur="+chauffeur,
table: "#tab_navigation",
  fields: [ 
   {
   label: "Adhérent :",
   name: "navigation_tournee.nav_adh_id",
   type: "select2",                 
   opts: {  
     multiple: false,
     minimumInputLength: 1,                          
     language: "fr",
     ajax: {
        url: "includes/adherents_json.php",
        dataType: 'json',
        delay: 150,
        data: function (params) {
           var query = {
              q: params.term,
              type: 'GET',
            }
          return query;
          }                             
       }
   }, {
   label: "Arrêt :",
   name: "navigation_tournee.nav_aller_id",
   placeholder: "",
   type: "select2",                            
   opts: {
      ajax: {
         url: "includes/arrets_json.php",
         dataType: 'json',
         cache: true,                        
      }
  }
}


I try with

editor.field('navigation_tournee.nav_adh_id').input().on('change', function (e, d) {
}

But i'm lost

Thx for your help

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Select2 with Ajax is always difficult! (imho).

    I was going to suggest basically what you have done - use a change event from the top field, to set the value for the second field. What happens when you try that?

    Allan

  • Mairie du PecqMairie du Pecq Posts: 9Questions: 3Answers: 0

    Thx Allan,

    When i select a value in field 1 :

    editor.field('navigation_tournee.nav_adh_id').input().on('change', function (e, d) {
       console.log( d );
    });
    

    The result of d is "undefined"

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    That is expected. To should only be set when Editor triggers a change (e.g. when you select a row and start editing it). The second parameter to the event handler is used to let you know if the change event happens because of an end user interaction (undefined) or Editor (an object).

    Allan

  • Mairie du PecqMairie du Pecq Posts: 9Questions: 3Answers: 0

    Thank you very much Allan !

    Work well

    editor.field('navigation_tournee.nav_adh_id').input().on('change', function (e, d) {
       if (d === undefined) {
          // The change was triggered by the end user on update rather than an auto set
          var adh_id = editor.get('navigation_tournee.nav_adh_id');                        
          if (adh_id != 0) {
             $.ajax({
                url: 'includes/adherents_adr_json.php?adh_id=' + adh_id,
                dataType: 'json',
                success: function (results) {
                adh_arr_id = results[0].id;
                editor.set('navigation_tournee.nav_aller_id', adh_arr_id);
             }
          })
       }
    }
    });
    
Sign In or Register to comment.