save data ajax in array

0

I have the following code where when pressing an Item of the table I make a query in Ajax this returns a result a list of item. I am declaring a Array() to be able to save this list in the.

     $('#table tbody tr> td').click(function() {
                      var pieza = $(this).closest('tr').find('td:eq(4)').text();
                      var piezas = [];
                      $.ajax({
                        data:  {'pieza':pieza},
                        url:   'JSON/likeJSON.php',
                        type:  'POST',
                        cache: false,
                        success:  function (data)
                        {
            //Con la Ayuda de @Sr1871 y @Fran Islas logre poder desplegar el listado.
            //Pero lo que ocurre ahora es que si selecciono otra pieza en la tabla,
            //el listado no cambia, ya que mi JSON realizo un Where item like '%%'                       
                        data = JSON.parse(data);
                        for (var count = 0; count < data.length; count++) {

                            piezas.push({
                                value: count,
                                text: '' + data[count].pieza + ''
                            })

                        }
                    },
                        async: false
                      });

                      $('#table').editable({
                          container: 'body',
                          selector: 'td.Pieza',
                          title: 'Pieza Nueva',
                          type: "POST",
                          source:piezas,
                          showbuttons: false,
                          select2: {
                              width: 200,
                              placeholder: 'Nueva Pieza',
                              allowClear: true
                          },
                          success: function() {

                          }
                      });

                    })

How can I save the data in my array in Javascritps to then use this Array() as source in my editable method.

With the help of @ Sr1871 and @Fran Islands I will be able to display the list. But what happens now is that if I select another piece this list does not change and my likeJSON see the new data.

I hope you have explained me well

    
asked by MoteCL 12.10.2018 в 15:51
source

2 answers

1

Through the JSON.parse function we can transform a character string with JSON format into a JavaScript object. On the other hand, the function JSON.stringify allows to obtain the inverse result: it transforms a JavaScript object into a text string with JSON format.

ajaxGet("http://localhost:3000/imagenes", function(respuesta) {
  // Transformación de formato JSON a JavaScript
  var imagenes = []; // nueva instancia
  imagenes = JSON.parse(respuesta);
  console.log(imagenes);
});

I hope it serves you. Greetings!

Edited: Test by adding var imagenes = []; // nueva instancia

    
answered by 12.10.2018 / 17:04
source
1

The documentation asks that you have the fields value and text if it is array

documentation: link

so you can simply do

for (var count = 0; count < data.length; count++) {

   piezas.push({value : count, text: data[count].pieza})

}

It could also be done with map

success:  function (data) {
    piezas = data.map(function(simpleData){
        return {value :simpleData.valor', text : simpleData.pieza}
    })
}

This can be a better use if your value comes from the ajax data, but you can use the for

    
answered by 12.10.2018 в 16:53