EffeBi
EffeBi

Reputation: 307

Pass Array of Arrays in javascript to field

this page is a view in a RoR project and i need to build this array of arrays using jQuery and JavaScript :

{ {id, mode, val_a, val_b}, {id, mode, val_a, val_b}, {id, mode, val_a, val_b} }

i think my build process is good

var arraytutti = [];
$('.selettoremodo').each(function() {
  arraysingolo = [];
  work = this.id;
  modo = this.value;
  orecontabilizzate = $("." + work + "[name='durataore_contab']").val();
  minuticontabilizzati = $("." + work + "[name='durataminuti_contab']").val();
  arraysingolo.push(work, modo, orecontabilizzate, minuticontabilizzati)

  arraytutti.push(arraysingolo)
});

printing in console log the main array with "console.log(arraytutti)" i have this (see picture)

chrome js console

Now i need to pass ALL THIS ARRAY WITH STRUCTURE to a field, but using $("#appoggio").html(arraytutti.toString());

or

$("#appoggio").html(arraytutti);

i have something like

389,2,3,0,391,2,4,0,393,2,0,24,395,2,8,41

i need the full array's structure so i can pass the field by post and analize the data from the controller,

how can i solve?

Upvotes: 0

Views: 54

Answers (3)

Bartłomiej Gładys
Bartłomiej Gładys

Reputation: 4615

you can do something like this in ruby controller on this params:

#=> [389, 2, 3, 0, 391, 2, 4, 0, 393, 2, 0, 24, 395, 2, 8, 41]

params[:something].each_slice(4).map{|l| l}
#=> [[389, 2, 3, 0], [391, 2, 4, 0], [393, 2, 0, 24], [395, 2, 8, 41]]

Upvotes: 0

gotbahn
gotbahn

Reputation: 516

$("#appoggio").html(JSON.stringify(arraytutti)); 

Upvotes: 0

Oden
Oden

Reputation: 628

You simply need to call JSON.stringify(arraytutti), and decode the JSON on the server side.

Upvotes: 1

Related Questions