How can I put a json object of the items of the products in a textarea?

1

I am using Angular and the ng-cart plugin for the shopping cart; the problem is that everything works fine but I require that the items be put in a textarea to save in the bd.

The json that you send is:

{  
   "shipping":null,
   "tax":0,
   "taxRate":null,
   "subTotal":1318.52,
   "totalCost":1318.52,
   "items":[  
      {  
         "id":"4",
         "name":"Clasico Panini",
         "price":80,
         "quantity":13,
         "data":"2",
         "total":1040
      },
      {  
         "id":"2",
         "name":"Mixiote",
         "price":69.63,
         "quantity":4,
         "data":"2",
         "total":278.52
      }
   ]
}

I currently have a for loop that prints me the console without problems.

But when I want to print it in the textarea, it seems that nothing else puts the last item nothing else.

I already try several methods but I get the same result, and if I directly send the ngCart.getItems(); in the textarea I get [object][object] .

the url of the plugin as reference is: ngcart.snapjay.com/docs

    
asked by Rafael Robles 22.06.2016 в 18:53
source

3 answers

0

Remember that the content of a textarea is just a string and you have an array of items so you have to join everything using the join method.

var productos = ngCart.getItems();
var i;
var results = [];
for (i= 0; i< productos.lenght; i++) {
    results.push('(' + productos[i]._quantity + 'x) Ordenes de: ' + 
                 productos[i]._name + ' :-: Precio unit: $' + 
                 productos[i]._price);
}
$scope.productos = results.join('\r\n');

I leave you a demo

angular.module('app', [])
  .controller('MyCtrl', function($scope) {
    var productos = [{
      "id": "4",
      "name": "Clasico Panini",
      "price": 80,
      "quantity": 13,
      "data": "2",
      "total": 1040
    }, {
      "id": "2",
      "name": "Mixiote",
      "price": 69.63,
      "quantity": 4,
      "data": "2",
      "total": 278.52
    }];
    var i;
    var results = [];
    for (i = 0; i < productos.length; i++) {
      results.push('(' + productos[i].quantity + 'x) Ordenes de: ' + productos[i].name + ' :-: Precio unit: $' + productos[i].price);
    }
    // Unelos usando saltos de línea para que queden unos debajo de los otros 
    $scope.productos = results.join('\r\n');
  })
textarea {
  width: 100%
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
  <textarea ng-model="productos"></textarea>
</div>
    
answered by 22.06.2016 / 19:46
source
0

What if you put your $scope.productos = productos within if . That is, something like the following code:

for(...){
var producto = ...
$scope.productos = producto;
}

I'm waiting for you.

    
answered by 22.06.2016 в 19:20
0

The problem is that you declare producto within the loop, so in each iteration producto is saving the current element and at the end of the loop producto has the value of the last product .

What you must do is declare producto outside the loop and in each iteration concatenate your string representation of the current object. In this way:

let producto = '';
for(let key in productos) {
    let actual = productos[key];
    producto += /* aquí creas el string de 'actual' */;
}

// solo para ejemplo, asumiendo un ng-model="areaProductos"
$scope.areaProductos = producto;

Or also (as you have it):

$scope.areaProductos += '\n' + producto;
    
answered by 22.06.2016 в 19:22