简体   繁体   English

Java变量内部的循环迭代(Django)

[英]For Loop Iteration Inside of Javascript Variable (Django)

I am trying to implement a dropdown menu with selectors inside of django. 我正在尝试使用Django内部的选择器来实现下拉菜单。 I have made good progress in finding a piece of software that looks great! 我在寻找一款看起来很棒的软件方面取得了良好的进展!

http://nelrohd.github.io/bootstrap-dropdown-checkbox/ http://nelrohd.github.io/bootstrap-dropdown-checkbox/

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: 该软件的工作方式是通过jQuery选择器接收单个变量:

    var myData = [{id: 1, label: "Test" }];
    $(".myDropdownCheckbox").dropdownCheckbox({
      data: myData,
      title: "Dropdown Checkbox"
    });

Right now i can only use hardcoded properties for the vars 现在我只能对var使用硬编码属性

    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:) 我想找到一种方法来使这些属性可迭代,并能够从Django数据库中提取列表:(类似:)

    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. 我试图使用django模板标签,但似乎也无法正常工作。 My question is: is it possible to iterate over a for loop to create properties for a variable? 我的问题是:是否可以遍历for循环来为变量创建属性?

Have you tried: 你有没有尝试过:

var data = [];

for (var i = 0; i <= 10; i++) {
  data[data.length] = { id: (i + 1), label: 'Option ' + (i + 1), isChecked: true };
}

If you generate the list in Django, you can do like: 如果您在Django中生成列表,则可以执行以下操作:

  1. I will assume you have a list of arbitrary objects you want to serialize in Javascript. 我假设您有要在Javascript中序列化的任意对象的列表。 When you are ready to pass it to the response context, convert it with stuff like this: mark_safe(json.dumps([{'id': element.id, 'label': element.label, 'isChecked': element.is_checked} for element in mylist])) . 当您准备将其传递到响应上下文时,请使用以下内容进行转换: mark_safe(json.dumps([{'id': element.id, 'label': element.label, 'isChecked': element.is_checked} for element in mylist])) This is just an example since I don't know which objects you want to render. 这只是一个示例,因为我不知道要渲染哪些对象。
  2. Assuming such safe and dumped json is sent to the foo context variable: 假设将这样安全且转储的json发送到foo上下文变量:

     var data = {{ foo }}; 

if you want to generate the options in pure javascript, there's nothing djangoish on it. 如果您想使用纯JavaScript生成选项,则无需赘述。 A simple js for loop like @AlexandreThebaldi answer will work. 像@AlexandreThebaldi答案这样的简单js for循环将起作用。

I would double check to make sure you had the Django tags set up correctly. 我会仔细检查以确保您正确设置了Django标签。 It should look like this by passing in the set of options as your context: 通过将选项集作为上下文传递,它看起来应该像这样:

{% for opt in options %}
     {id: {{ forloop.counter }}, label: {{ opt }}, isChecked: true}
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM