用 sqlalchemy 查询敲除填充表
Posted
技术标签:
【中文标题】用 sqlalchemy 查询敲除填充表【英文标题】:Knockout populating table with sqlalchemy query 【发布时间】:2018-05-25 12:14:30 【问题描述】:我之前发布了一个问题,并且回复链接到一个没有帮助的问题并且问题已经演变,所以这里是一个新问题 这是我用来将查询结果发布到数据库的整个 html 文件。查询正常工作,它返回 json 信息,但我不确定如何从响应中获取 json 并在不重新加载页面的情况下填充表。目前发生的情况是我填写表格以查询数据库,然后将结果序列化为 json,然后页面显示 json 信息。(这全部由我的烧瓶路由声明处理。)我想要完成的是不重新加载相反,它只是用正确字段中的结果填充表格
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Reports</title>
</head>
<body>
% extends "layout.html" %
% block content %
<div class="index">
<h1>Report Generation</h1>
<p>Search for accounts to generate a report for.</p>
<div class="jumbotron">
<form class="form-signin" method="post" id="query">
<label for="searchFirst" class="sr-only">First Name</label>
<input type="name" value="request.form.searchFirst" name="searchFirst" id="searchFirst" class="form-control" placeholder="First Name" required autofocus>
<label for="searchLast" class="sr-only">Last Name</label>
<input type="name" value="request.form.searchLast" name="searchLast" id="searchLast" class="form-control" placeholder="Last Name">
<label for="searchPhone" class="sr-only">Phone Number</label>
<input type="name" value="request.form.searchPhone" name="searchPhone" id="searchPhone" class="form-control" placeholder="Phone Number">
<button id="btnSignUp" class="btn btn-lg btn-primary btn-block" type="submit">Search</button>
</form>
</div>
<br>
<div id="results">
<table class="blueTable" id="queries">
<thead>
<tr>
<th>Policy(ies) ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Street</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Phone Number</th>
</tr>
</thead>
<tbody>
<tr data-bind="foreach: viewModel.queries">
<td data-bind="text: id"></td>
<td data-bind="text: first"></td>
<td data-bind="text: last"></td>
<td data-bind="text: street"></td>
<td data-bind="text: city"></td>
<td data-bind="text: state"></td>
<td data-bind="text: zipcode"></td>
<td data-bind="text: phonenumber"></td>
</tr>
</tbody>
</table>
</div>
</div>
<script type="text/javascript">
$(function ()
ko.applyBindings(viewModel);
viewModel.loadQueries()
);
function result(result)
this.id = ko.observable(result.id);
this.first = ko.observable(result.first);
this.last = ko.observable(result.last);
this.street = ko.observable(result.street);
this.city = ko.observable(result.city);
this.state = ko.observable(result.state);
this.zipcode = ko.observable(result.zipcode);
this.phonenumber = ko.observable(result.phonenumber);
var viewModel =
queries: ko.observableArray([]),
loadQueries: function ()
var self = this;
$.getJSON("/process",function (queries)
self.queries.removeAll();
self.queries(queries)
);
;
$('#query').submit(function (e)
$.postJSON('/reports', query, result());
e.preventDefault();
);
jQuery.extend(
postJSON: function(url, data, callback)
return jQuery.ajax(
type: "POST",
url: '/reports',
data: typeof data == 'string'?data:JSON.stringify(data),
success: callback,
dataType: url,
contentType: "application/json",
processData: false
);
);
</script>
% endblock %
</body>
</html>
这就是我的 json 的返回方式
"contacts": [
"city": "your city",
"first": "No",
"id": 6,
"last": "Policy",
"phonenumber": "0000000000",
"state": "your state",
"street": "123 Main St",
"zipcode": "00000"
]
【问题讨论】:
您需要将foreach
绑定更改为foreach: queries
。重新加载部分不清楚。您想在$.postJSON('/reports')
之后刷新数据?
【参考方案1】:
在您的 javascript 块中,您需要定义您的回调函数需要是什么。您将它用作 post 函数的参数,但我的猜测是它当前未定义。这是在 ajax 通信成功时调用的函数。
例如
function callback(data)
// do something here
$('#query').submit(function (e)
$.postJSON('/reports', query, result());
e.preventDefault();
);
jQuery.extend(
postJSON: function(url, data, callback)
return jQuery.ajax(
type: "POST",
url: '/reports',
data: typeof data == 'string'?data:JSON.stringify(data),
success: callback,
dataType: url,
contentType: "application/json",
processData: false
);
【讨论】:
以上是关于用 sqlalchemy 查询敲除填充表的主要内容,如果未能解决你的问题,请参考以下文章
在 Flask-SQLAlchemy 模型上使用函数查询会给出 BaseQuery object is not callable 错误