首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在pivottable.js函数中引用上下文数据

如何在pivottable.js函数中引用上下文数据
EN

Stack Overflow用户
提问于 2016-08-09 15:20:02
回答 1查看 1.1K关注 0票数 0

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

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

在我的views.py中:

代码语言:javascript
复制
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中:

..。

代码语言:javascript
复制
<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}}。谢谢你的帮助

EN

回答 1

Stack Overflow用户

发布于 2016-08-09 17:17:32

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38844457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档