我有一个最初由烧瓶提供的页面。使用for循环的列表填充下拉列表。当用户单击下拉列表中的某个项目时,会向Flask发送ajax帖子。 Flask然后返回json和jquery然后插入一些html。
所有这一切都很好。
问题:在更新DOM之后,由于某种原因,向Flask发出GET请求,然后从头开始重新呈现模板。我无法弄明白如果解雇GET ......
问题:如何阻止这种情况?
这是我的代码:
app.py
@app.route('/orders', methods=['GET','POST'])
@login_required
def orders():
customers = get_active_cust()
if request.method == 'GET':
return render_template('orders.html', customers=customers)
elif request.method=='POST':
offers = get_sent_offers(int(request.values.get('party_id')))
return offers.to_json(orient='records', date_format='iso')
'offers'是一个返回pandas数据帧的独立模块。这将转换为json格式并发送给客户端。
HTML:
<div class="container" style="padding-top: 40px">
<div class="row">
<!--left-->
<div class="col-md-2" id="leftCol">
<ul class="nav nav-stacked" id="sidebar">
<li class="dropdown">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select a Customer <span class="caret"></span></button>
<ul ID="customer" class="dropdown-menu">
{% for index,row in customers.iterrows() %}
<li><a href="" id="customer_select" value={{ row.party_id }}>{{ row.party_name }}</a></li>
{% endfor %}
</ul>
</li>
</ul>
</div>
<!--/left-->
<!--right-->
<div class="col-md-9">
<div id="offer_column"></div> <!--title insert point-->
<div class="row">
<div class="col-md-9">
<div id="each_offer"></div> <!-- panel insert point -->
<hr class="">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
</div>
<hr class="">
</div>
<!--/right-->
</div>
<!--/row-->
</div>
<!--/container-->
<div class="row"></div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#customer_select').click(function(){
var party_id = $(this).attr('value');
var past_offers;
$.post('/orders',{
party_id: party_id})
.done(function(response){
past_offers = JSON.parse(response);
$('<h3 id="sec0" class="">Sent Offers</h3>').insertBefore($('#offer_column'));
//get unique offer dates
var unique = {};
var distinct = [];
for( var i in past_offers ){
if( typeof(unique[past_offers[i].sent_date]) == "undefined"){
distinct.push(past_offers[i].sent_date);
}
unique[past_offers[i].sent_date] = 0;
};
//loop through dates and print out past offers into panels>tables
for (var d in distinct) {
var panel = '<div class="panel panel-default">' + '<div class="panel-heading">' +
'<h4 class="">' + new Date(distinct[d]) + '</h4>' +
'</div><div class="panel-body" div="">' +
'<table class="table table-bordered" style=""><tbody>' +
'<tr><th class="" contenteditable="false">Location</th>' +
'<th class="" contenteditable="false">Product</th>' +
'<th class="" contenteditable="false">Price</th>' +
'<th class="" contenteditable="false">Volume</th>' +
'</tr>';
//loop to add each product per distinct date
for (var i in past_offers) {
if (past_offers[i].sent_date == distinct[d]) {
panel = panel +
'<tr>' +
'<td class="" contenteditable="false">' + past_offers[i].station_name + '</td>' +
'<td class="" contenteditable="false">' + past_offers[i].product_name + '</td>' +
'<td class="" contenteditable="true">' + past_offers[i].target_offer_tax + '</td>' +
'<td class="" contenteditable="true">0</td>' +
'</tr>';
}
}
// add finishing touches
panel = panel +
'</tbody></table>' + '<div class="">' +
'<button class="btn btn-default">Submit</button>' +
'</div></div></div>';
$(panel).insertAfter($('#each_offer'));
}
});
});
});
</script>
答案 0 :(得分:1)
添加:
return false;
.done
功能解决了问题。显然,这会阻止浏览器循环回到显然正在触发“GET”的链接。
以下完整代码更新:
<div class="container" style="padding-top: 40px">
<div class="row">
<!--left-->
<div class="col-md-2" id="leftCol">
<ul class="nav nav-stacked" id="sidebar">
<li class="dropdown">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select a Customer <span class="caret"></span></button>
<ul ID="customer" class="dropdown-menu">
{% for index,row in customers.iterrows() %}
<li><a href="" id="customer_select" value={{ row.party_id }}>{{ row.party_name }}</a></li>
{% endfor %}
</ul>
</li>
</ul>
</div>
<!--/left-->
<!--right-->
<div class="col-md-9">
<div id="offer_column"></div> <!--title insert point-->
<div class="row">
<div class="col-md-9">
<div id="each_offer"></div> <!-- panel insert point -->
<hr class="">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
</div>
<hr class="">
</div>
<!--/right-->
</div>
<!--/row-->
</div>
<!--/container-->
<div class="row"></div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#customer_select').click(function(){
var party_id = $(this).attr('value');
var past_offers;
$.post('/orders',{
party_id: party_id})
.done(function(response){
past_offers = JSON.parse(response);
$('<h3 id="sec0" class="">Sent Offers</h3>').insertBefore($('#offer_column'));
//get unique offer dates
var unique = {};
var distinct = [];
for( var i in past_offers ){
if( typeof(unique[past_offers[i].sent_date]) == "undefined"){
distinct.push(past_offers[i].sent_date);
}
unique[past_offers[i].sent_date] = 0;
};
//loop through dates and print out past offers into panels>tables
for (var d in distinct) {
var panel = '<div class="panel panel-default">' + '<div class="panel-heading">' +
'<h4 class="">' + new Date(distinct[d]) + '</h4>' +
'</div><div class="panel-body" div="">' +
'<table class="table table-bordered" style=""><tbody>' +
'<tr><th class="" contenteditable="false">Location</th>' +
'<th class="" contenteditable="false">Product</th>' +
'<th class="" contenteditable="false">Price</th>' +
'<th class="" contenteditable="false">Volume</th>' +
'</tr>';
//loop to add each product per distinct date
for (var i in past_offers) {
if (past_offers[i].sent_date == distinct[d]) {
panel = panel +
'<tr>' +
'<td class="" contenteditable="false">' + past_offers[i].station_name + '</td>' +
'<td class="" contenteditable="false">' + past_offers[i].product_name + '</td>' +
'<td class="" contenteditable="true">' + past_offers[i].target_offer_tax + '</td>' +
'<td class="" contenteditable="true">0</td>' +
'</tr>';
}
}
// add finishing touches
panel = panel +
'</tbody></table>' + '<div class="">' +
'<button class="btn btn-default">Submit</button>' +
'</div></div></div>';
$(panel).insertAfter($('#each_offer'));
}
});
return false; // <------ This solved the issue
});
});
</script>