0

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!

9
  • "But I'm not satisfied with this" Why? The mistake at the end is that you are assigning a function to columns, instead of calling the function and assign its return value. I don't see how the last solution would be any improvement over the original code though. Commented Mar 18, 2015 at 23:16
  • 1
    You can try this: pastebin.com/raw.php?i=10UX0xpP but columns: StatusCols() is fine. Commented Mar 18, 2015 at 23:19
  • Hi Felix, my previous code works, but need an extra function. I'm trying to remove the additional function, so my idea was create a "local" function that could return an array. Commented Mar 18, 2015 at 23:25
  • I'm not sure if I've found your exact issue just by looking, but one helpful tip; if you want to make another array that's based on the contents of a first array, it's handy to use .map(. It works similarly to each, but...well, look it up here: api.jquery.com/jQuery.map Commented Mar 18, 2015 at 23:25
  • 1
    @Arvy Let's say you have a function called myFunc. myFunc is equal to function(){...}. You call it by typing its name + a set of parenthesis: myFunc(). Well, it's equivalent to calling it directly (replace the name by the function declaration itself): function(){...}() Commented Mar 18, 2015 at 23:28

1 Answer 1

1

Thank you @blex. Solved. Solution:

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.