Arvy
Arvy

Reputation: 1212

jQuery array parameter and each()

I'm new in jQuery world, still learning. I'm trying to understand how can I pass an array parameter reading data from an "each()" loop. Let my try to explain (my native language is Portuguese, sorry).

My original code is below:

$('#relatorio').DataTable({
   (...)
   columns: [
      {visible:$('#c1').is(':checked')},
      {visible:$('#c2').is(':checked')},
      {visible:$('#c3').is(':checked')}
   ]
});

Since I'll use this in several places (reports), I did a .JS file. But, in each report, I'll have different checkboxes. So I want to scan each checkbox and mount the object array dynamically. I've add a class "colsel" in each checkbox, so I can do:

var vCada=[];
$('.colsel').each(function(){ vCada.push({visible:$(this).is(':checked')}); });

Ok, but now I don't know how to replace this directly in there.

I had success creating a function that returns the object array:

function StatusCols() {
  var vCada=[];
  $('.colsel').each(function(){ vCada.push({visible:$(this).is(':checked')}); });
  return vCada;
}

and then:

columns: StatusCols()

But I'm not satisfied with this and my newbie knowledge is not helping me :(

I've tried:

columns: function() {
   var vCada=[];
   $('.colsel').each(function(){ vCada.push({visible:$(this).is(':checked')}); });
   return vCada;
 }

So, where is my mistake? Can anyone help me (and teach me)?

Thank you!

Upvotes: 0

Views: 74

Answers (1)

Arvy
Arvy

Reputation: 1212

Thank you @blex. Solved. Solution:

columns: function() {
   var vCada=[];
   $('.colsel').each(function(){ vCada.push({visible:$(this).is':checked')}); });
   return vCada;
 }()

Means: function(){...}()

Upvotes: 1

Related Questions