Updating Select Field from database query

Updating Select Field from database query

daniegloydaniegloy Posts: 35Questions: 12Answers: 5

I am a bit lost on what to do.

              $.ajax({
              url: 'checkStockNr.php',
              data:'checkType=getMMYear&MMval='+MMVal+' '
           })
              .done(function(Ydata) {

                 var YdataArray = $.parseJSON(Ydata);
                 var x,text,setText;
                for (x in YdataArray) {
                    text = YdataArray[x]['RegYear'];
                    editorNewForm.field('YEAR').update([{"label":text}]);

                }

                  console.log(text);


                   // editorNewForm.field( 'YEAR' ).set('Ydata');
              })

i only manage to get the last value out of the array to be added to the select list.

this is the php output from Ydata [{"RegYear":"1998"},{"RegYear":"1999"}]

but i seem to only get "1999" in the dropdown list.

Im New at datatables and im sure im making a obvious mistake.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,572Questions: 1Answers: 10,482 Site admin
    Answer ✓

    The issue is that you are calling field().update() once for every option. It expects the full list of options to be passed in, which explains why you are only ever seeing the final item in the array.

    What you need to do is create an array of objects with label and value and then pass that resulting array into field().update() just once (i.e. the array has all the options in it).

    Allan

  • daniegloydaniegloy Posts: 35Questions: 12Answers: 5

    Am i at least close?

                 var YdataArray = $.parseJSON(Ydata);
                    var x,text,select;
                    var setText = new Array();
                          for (x in YdataArray) {
                                text = YdataArray[x]['RegYear'];
                                console.log(text);
    
                                 setText[x] = text;
                           }
                           select = '';
    
                          for (x in setText) {
                            select += '{'+setText[x]+'},';
                          }
                          editorNewForm.field('YEAR').update([select]);
                  })
                })
    

    now its only creating one select option with both values, what am i missing?

  • daniegloydaniegloy Posts: 35Questions: 12Answers: 5
    Answer ✓

    Fixed Thank You. Found the solution here --- http://datatables.net/forums/discussion/9899

  • allanallan Posts: 63,572Questions: 1Answers: 10,482 Site admin
    Answer ✓

    Great stuff - good to hear you've got it working now :-)

    Allan

This discussion has been closed.