2014-10-07 146 views
0

我在學習燒瓶並試圖建立一個顯示財富500強企業數據的頁面。帶有Javascript的燒瓶HTML模板

我已經得到了正確顯示,現在我試圖能夠通過任何列排序表。它看起來像我需要一些JavaScript我已經保存在我的靜態目錄,但我不上怎麼拉在JavaScript很清楚

問題:

  • 我是否有正確的框架?
  • 如何正確使用JavaScript?
  • 我讀過,我應該把任何腳本標記放在base.html中的頭部,有沒有一段時間我不會那樣做?
  • 我不是問我應該是什麼問題?

下面是我的文件結構

-app 
--static 
    *sorttable.js 
--templates 
    *base.html 
    *companies.html 
--run.py 
--views.py 
--companies.csv 

下面是base.html文件

<html> 
    <head> 
    <script> src="{{ url_for('static', filename='sorttable.js', type='text/javascript') }}"</script> 
    {% if title %} 
    <title>{{ title }} - microblog</title> 
    {% else %} 
    <title>Welcome to microblog</title> 
    {% endif %} 
    </head> 
    <body> 
    <div>Microblog: <a href="/index">Home</a></div> 
    <div>Login: <a href="/login">Here</a></div> 
    <hr /> 
    {% block content %}{% endblock %} 
    </body> 
</html> 

下面是companies.html

{% extends "base.html" %} 
{% block content %} 
<table class="sortable"> 

<table> 
     <thead> 
     <tr> 
      <th> Revenues </th> 
      <th> Profits </th> 
      <th> Rank </th> 
      <th> Company </th> 
     </tr> 
     </thead> 
    {% for keys in companies %} 

     <tr> 
      <td> {{ keys.Revenues }} </td> 
      <td> {{ keys.Profits }} </td> 
      <td> {{ keys.Rank }} </td> 
      <td> {{ keys.Standard }} </td> 
     </tr> 

    {% endfor %} 
    <tfoot> 
    </tfoot> 

</table> 
{% endblock %} 

而且run.py

# encoding: utf-8 

from flask import render_template 
from app import app 
from .forms import LoginForm 

@app.route('/companies') 
def companies(): 
    import csv 

    with open('companies.csv','rU') as f: 
     companies = csv.DictReader(f) 


     return render_template("companies.html", 
          title='Home', 
          companies=companies) 

回答

0

我找到了答案,如何讓列表進行排序

我改了行的base.html文件來自:

<script> src="{{ url_for('static', filename='sorttable.js', type='text/javascript') }}"</script> 

<script src="{{ config.STATIC_URL }}/static/sorttable.js"></script> 

,似乎做招。

,我肯定會喜歡從別人那裏得到的反饋如何這可以普遍提高

0

script標籤不正確。您不需要在script<script>)之後立即關閉標籤,而是需要首先包含其屬性。

<script src="{{ url_for('static', filename='sorttable.js') }}"></script> 

type="text/javascript"是可選的,如果你使用的是HTML5(<!doctype html>),但應包括其他。