Connor Meeks Connor Meeks - 1 year ago 207
HTML Question

For Loop Iteration Inside of Javascript Variable (Django)

I am trying to implement a dropdown menu with selectors inside of django. I have made good progress in finding a piece of software that looks great!

However, i am getting stuck on how to make it more dynamic.

The end goal is to have a filter option that consists of a list of applications that i can select and unselect in order to change what is outputted to the screen.

The way the software works is that it takes in a single variable through a jQuery selector:

var myData = [{id: 1, label: "Test" }];
data: myData,
title: "Dropdown Checkbox"

Right now i can only use hardcoded properties for the vars

var data =[
{ id: "1", label: "Option 1", isChecked: true },
{ id: "2", label: "Option 2", isChecked: true },
{ id: "3", label: "Option 3", isChecked: true},
{ id: "4", label: "Option 4", isChecked: true },
{ id: "5", label: "Option 5", isChecked: true },
{ id: "6", label: "Option 6", isChecked: true},
{ id: "7", label: "Option 7", isChecked: true },
{ id: "8", label: "Option 8", isChecked: true },
{ id: "9", label: "Option 9", isChecked: true },
{ id: "10", label: "Option 10", isChecked: true },

I would like to find a way to make these properties iterable and able to pull a list from the django database: (something like:)

var data=[
for( var i = 0; i<=10, i++)
{id: (i), label: (list(i)), isChecked: true},

I have tried to use django template tags, but cant seem to get that working either. My question is: is it possible to iterate over a for loop to create properties for a variable?

Answer Source

Have you tried:

var data = [];

for (var i = 0; i <= 10; i++) {
  data[data.length] = { id: (i + 1), label: 'Option ' + (i + 1), isChecked: true };
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download