如何在透视表.js函数中引用上下文数据



我有一个django项目设置。我正在使用pivottable.js (https://github.com/nicolaskruchten/pivottable)添加一个带有数据透视表的新页面

我的问题是我无法从views.py上下文{{data}}加载csv数据,从透视表.js jQuery函数内。

In my views.py:
def sysdev_pivottable(request):
    context = {}
    csvString = ""
    with open("W:\data.csv", 'rb') as csvfile:
        reader= csv.reader(csvfile, delimiter=',', quotechar='|')
        for row in reader:
            csvString = csvString + ','.join(row)
    context['data'] = csvString
    template = loader.get_template('pivottable.html')
    return HttpResponse(template.render(context, request))
在pivottable.html:

<h1> Test load: {{ data }}</h1> #this displays data properly
<script type="text/javascript">
$(function()
{
    var input1 = "1,2,3,4" #test data declared within the function()
    $("#output").pivotUI(input1, {}); #this loads properly
    var input2 = {{data}} ##this doesn't work, would break the rest of the <script>
});
    </script>
        <div id="output" style="margin: 10px;">
        </div>

我不知道如何从$(function(){})中正确加载{{data}}。从$(function(){})外部,我可以正确加载{{data}}。谢谢你的帮助

正如@AnnaVracheva所提到的,我的主要问题是忘记了{{data}}周围的引号。此外,我使用jquery.csv (https://github.com/evanplaice/jquery-csv)对输入数据进行格式化,一切正常。

$(function() { var input = $.csv.toArrays("{{data}}") $("#output").pivotUI(input, { rows: ["RowValue"], cols: ["ColValue"] }); });
</script>
<div id="output" style="margin: 10px;">
</div>

最新更新