在javascript中循环遍历django列表

时间:2022-09-04 20:19:02

I need to loop through my django list that I have passed to the template.

我需要遍历我传递给模板的django列表。

I have this code in my django view:

我在django视图中有这个代码:

if plan:
    investments = Investment.objects.all().filter(plan = plan).order_by('maturity_date').filter(maturity_date__gte = now)
    for i in investments:
        financial_institution = i.financial_institution
        amount = i.get_current_value(date)
        fi_list.append({
            'fi': financial_institution,
            'amt':amount
        })
    context['list'] = fi_list

Which outputs:

[<financial_institution: Example> <amount: 5000>]

Now what I want to do is loop through this list, and if my javascript variable matches the item in the list, do further code. However I am stuck on how to do this.

现在我要做的是遍历此列表,如果我的javascript变量与列表中的项匹配,请执行进一步的代码。但是我仍然坚持如何做到这一点。

Here is my javascript so far, using jQuery:

这是我到目前为止的javascript,使用jQuery:

function cdic_limit(amount) {
        var limit = 100000.00;
        var list ="{{ list }}";
        var fi = $("#id_financial_institution option:selected").text();

    }

Down the road, what I ultimately want, is if the selected institution is in the list, check and make sure their total amount isn't exceeding $100k

在路上,我最终想要的是,如果选定的机构在列表中,检查并确保它们的总金额不超过10万美元

Any suggestions?

1 个解决方案

#1


1  

I don't know what you intend to do with the fi_list variable that you add to the context. If you plan to list the institutions and their limits in a systematic way, such as a table, then it should be simple enough to retrieve the amount data in much the same way as you retrieved the selected financial institution's name.

我不知道你打算用你添加到上下文的fi_list变量做什么。如果您计划以系统的方式列出机构及其限制(例如表格),那么它应该足够简单,以检索金额数据的方式与检索所选金融机构名称的方式大致相同。

If you intend to reveal to the user the amounts for all the institutions (which I don't think you do), and by reveal I mean it exists anywhere in the HTML code, whether or not the browser renders it, then one thing you can do is encode fi_list into a JSON string, make your response have (in a script tag) code like:

如果您打算向用户透露所有机构的金额(我认为您不这样做),并且透露我的意思是它存在于HTML代码中的任何位置,无论浏览器是否呈现它,那么有一件事你可以做的是将fi_list编码为JSON字符串,使您的响应具有(在脚本标记中)代码,如:

var finInst = jQuery.parseJSON( "{{ jsonString }}" );

function checkLimit(amount) {
    // I don't know what amount is supposed to do.
    if (finInst[jQuery(this).text()] > 100000)
        // do amount > 100000 code
    else
        // amount within limit
}

With django/python code like:

使用django / python代码:

import json
if plan:
    investments = Investment.objects.all().filter(plan = plan).order_by('maturity_date').filter(maturity_date__gte = now)
    fi_list = {}
    for i in investments:
        financial_institution = i.financial_institution
        amount = i.get_current_value(date)
        fi_list[financial_institution] = amount
    context['jsonString'] = json.dumps(fi_list)

And finally, whenever an institution option is selected from the web page, trigger the checkLimit function.

最后,每当从网页中选择一个机构选项时,触发checkLimit功能。

Honestly, this is really bad code because I think you don't want to expose all these amount values for each institution (critically confidential info maybe?). So the only reliable way to produce on the fly results would be to use AJAX to call a django view whenever an institution is selected. You might want to look at dajaxproject to simplify these requests.

老实说,这是非常糟糕的代码,因为我认为您不希望为每个机构公开所有这些金额值(严格保密的信息可能?)。因此,即时生成结果的唯一可靠方法是使用AJAX在选择机构时调用django视图。您可能希望查看dajaxproject以简化这些请求。

#1


1  

I don't know what you intend to do with the fi_list variable that you add to the context. If you plan to list the institutions and their limits in a systematic way, such as a table, then it should be simple enough to retrieve the amount data in much the same way as you retrieved the selected financial institution's name.

我不知道你打算用你添加到上下文的fi_list变量做什么。如果您计划以系统的方式列出机构及其限制(例如表格),那么它应该足够简单,以检索金额数据的方式与检索所选金融机构名称的方式大致相同。

If you intend to reveal to the user the amounts for all the institutions (which I don't think you do), and by reveal I mean it exists anywhere in the HTML code, whether or not the browser renders it, then one thing you can do is encode fi_list into a JSON string, make your response have (in a script tag) code like:

如果您打算向用户透露所有机构的金额(我认为您不这样做),并且透露我的意思是它存在于HTML代码中的任何位置,无论浏览器是否呈现它,那么有一件事你可以做的是将fi_list编码为JSON字符串,使您的响应具有(在脚本标记中)代码,如:

var finInst = jQuery.parseJSON( "{{ jsonString }}" );

function checkLimit(amount) {
    // I don't know what amount is supposed to do.
    if (finInst[jQuery(this).text()] > 100000)
        // do amount > 100000 code
    else
        // amount within limit
}

With django/python code like:

使用django / python代码:

import json
if plan:
    investments = Investment.objects.all().filter(plan = plan).order_by('maturity_date').filter(maturity_date__gte = now)
    fi_list = {}
    for i in investments:
        financial_institution = i.financial_institution
        amount = i.get_current_value(date)
        fi_list[financial_institution] = amount
    context['jsonString'] = json.dumps(fi_list)

And finally, whenever an institution option is selected from the web page, trigger the checkLimit function.

最后,每当从网页中选择一个机构选项时,触发checkLimit功能。

Honestly, this is really bad code because I think you don't want to expose all these amount values for each institution (critically confidential info maybe?). So the only reliable way to produce on the fly results would be to use AJAX to call a django view whenever an institution is selected. You might want to look at dajaxproject to simplify these requests.

老实说,这是非常糟糕的代码,因为我认为您不希望为每个机构公开所有这些金额值(严格保密的信息可能?)。因此,即时生成结果的唯一可靠方法是使用AJAX在选择机构时调用django视图。您可能希望查看dajaxproject以简化这些请求。