我有Flask
项目,BoostrapTables
使用x-editable
插件。我的表为用户提供了select
框。
我有一个单独的表单,用户可以添加新联系人(通过Fiddles中的按钮模拟),因此我需要将新用户添加到select
选项。我有这个部分工作:
http://jsfiddle.net/42kr1pk1/
问题出在我的实际应用程序中,所有列数据(我有很多列)都是从Python
中的数据库查询生成的,并通过render_template
传递给页面。
这是我遇到麻烦的地方,因为我似乎无法通过JavaScript
传递render_template
函数。例如,这将通过render_template
传递:< / p>
var columns = [{field: 'Contact',
title: 'Contact',
editable: {
type: 'select',
source: function() { return names; }
}
},{field: 'Notes',
title: 'Notes',
}]
在小提琴中显示:http://jsfiddle.net/010pcbvg/
这里是Flask
的样子:
@app.route("/")
def test():
columns = """[{field: 'Contact',
title: 'Contact',
editable: {
type: 'select',
source: function() { return names; }
}
},{field: 'Notes',
title: 'Notes',
}]"""
names = [{'value': 1, 'text': 'New'},
{'value': 4, 'text': 'Andrew'},
{'value': 2, 'text': 'John'},
{'value': 3, 'text': 'Liz'}]
return render_template('test.html', names=names, columns=columns)
的test.html
<html>
<head><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var names = {{names|safe}};
var columns = $.parseJSON({{columns|safe}});
</script>
</head>
<body>
</body>
</html>
我收到错误:
Uncaught SyntaxError: Unexpected token o in JSON at position 1
也许我的整个方法都是错误的,但似乎这样的事情应该适用于一些调整。