(1)鏈接的 onclick 事件被先執行,其次是 href 屬性下的動作(頁面跳轉,或 javascript 僞鏈接)
(2)假設鏈接中同時存在 href 與 onclick,如果想讓 href 屬性下的動作不執行,onclick 必須得到一個 false 的返回值。
(3)如果頁面過長有滾動條,且希望通過鏈接的 onclick 事件執行操作。應將它的 href 屬性設爲 javascript:void(0);,而不要是 #,這可以防止不必要的頁面跳動;
(4)如果在鏈接的 href 屬性中調用一個有返回值的函數,當前頁面的內容將被此函數的返回值代替;
<a href="javascript:void(0);" onclick="download()" style="color: blue" download>Download</a>
function download() {
let url = window.location.protocol + '//' + window.location.host + '/download';
url = url + '?userAge=' + $("#userAge").val();
url = url + '&userId='+$("#userId").val();
url = url + '&userName='+encodeURIComponent(encodeURIComponent($("#userName").val()));
window.location.href = url;
return false;
}
@xxx.route('/download', methods=['GET'])
@login_required
def download():
user_id = request.args.get('userId')
user_age = request.args.get('userAge')
user_name = urlparse.unquote(request.args.get('userName'))
logger.info('userId: {0}, userAge: {1}, userName: {2}'.format(str(user_id), str(user_age), str(user_name)))
connection = engine.raw_connection()
df = pd.DataFrame()
try:
cursor = connection.cursor()
cursor.callproc("xxx_xxxx", [user_name, user_age])
sql = "select * from xxx_xxx where userid=:user_id "
df = pd.read_sql_query(sql, engine, params=[user_id])
cursor.close()
connection.commit()
except Exception as e:
logger.error('Get data error:' + str(e.message))
logger.error(traceback.format_exc())
finally:
connection.close()
output = BytesIO()
writer = pd.ExcelWriter(output)
df.to_excel(writer, index=False, sheet_name='Sheet')
workbook = writer.book
header_fmt = workbook.add_format({
'font_size': 14,
'bold': True,
'fg_color': '#D7E4BC',
'border': 1})
sheet_table = writer.sheets['Sheet']
for col_num, value in enumerate(df.columns.values):
sheet_table.write(0, col_num, value, header_fmt)
sheet_table.set_column('A:A', 30)
sheet_table.set_column('B:B', 10)
sheet_table.set_column('C:C', 30)
sheet_table.set_column('D:I', 15)
sheet_table.set_column('J:K', 20)
workbook.close()
writer.close()
output.seek(0)
return send_file(output, attachment_filename="Excel_Name.xlsx", as_attachment=True)