jQuery tablesorter插件在Django模板中不起作用

时间:2018-12-25 14:54:42

标签: javascript jquery html django tablesorter

尝试在Django模板中使用表排序JQuery插件。尝试了jquery和tablesorter文件的不同来源。似乎没有与任何人一起工作。模板页面上没有任何更改。我要排序的真实表是tab2。创建了一个简单的tab1仅用于测试。也不适用于此表。尝试遵循以下说明https://mottie.github.io/tablesorter/docs/。从此页面下载的.js文件。以前没有使用JS和JQuery的经验。

{% extends 'base.html' %}
{% load static %}

{% block content %}
    <h4>Players</h4>
    <head>
      <script type="text/javascript" src="{% static 'players/jquery-latest.min.js' %}"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.1/css/dragtable.mod.min.css"></script>

      <script type="text/javascript">
        $(function() {
        $("#tab1").tablesorter();
        });
      </script>

    </head>
    <body>

      <table id="tab1" class="table table-hover table-bordered tablesorter">
 <thead>
   <tr>
     <th>Month</th>
     <th>Savings</th>
   </tr>
 </thead>
 <tbody>
   <tr>
     <td>January</td>
     <td>$100</td>
   </tr>
   <tr>
     <td>February</td>
     <td>$80</td>
   </tr>
 </tbody>
   <tr>
     <td>Sum</td>
     <td>$180</td>
   </tr>
</table>

  <div class="container">
  <table id="tab2" class="table table-hover table-bordered tablesorter">
    <thead>
      <tr>
        <th>Name</th>
        <th>Position</th>
        <th>Height</th>
        <th>Weight</th>
        <th>Birth Date</th>
        <th>Draft Pick No</th>
        <th>Games</th>
        <th>Goals</th>
        <th>Assists</th>
        <th>Shots</th>
        <th>Hits</th>
        <th>Faceoffs Won</th>
        <th>Blocks</th>
      </tr>
    </thead>
    {% for player in players %}

    <tbody>
      <tr>
      <td><a href="{% url 'player-detail' player.playerName|slugify player.playerId %}">{{ player.playerName }}</td>
      <td>{{ player.playerPositionCode }}</td>
      <td>{{ player.playerHeight }}</td>
      <td>{{ player.playerWeight }}</td>
      <td>{{ player.playerBirthDate }}</td>
      <td>{{ player.playerDraftOverallPickNo }}</td>
      <td>{{ player.gamesPlayed }}</td>
      <td>{{ player.goals }}</td>
      <td>{{ player.assists }}</td>
      <td>{{ player.shots }}</td>
      <td>{{ player.hits }}</td>
      <td>{{ player.blockedShots }}</td>
      <td>{{ player.faceoffsWon }}</td>
    {% endfor %}

      </tr>
    </tbody>
  </table>
</div>
    </body>

{% endblock content %}

2 个答案:

答案 0 :(得分:1)

根据documentation中的报道,您忘记了包含库。

$("#tab1").tablesorter();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://mottie.github.io/tablesorter/css/theme.default.css">
<script src="https://mottie.github.io/tablesorter/js/jquery.tablesorter.js"></script>
<script src="https://mottie.github.io/tablesorter/js/jquery.tablesorter.widgets.js"></script>


<table id="tab1" class="table table-hover table-bordered tablesorter">
    <thead>
    <tr>
        <th>Month</th>
        <th>Savings</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>January</td>
        <td>$100</td>
    </tr>
    <tr>
        <td>February</td>
        <td>$80</td>
    </tr>
    </tbody>
    <tr>
        <td>Sum</td>
        <td>$180</td>
    </tr>
</table>

答案 1 :(得分:1)

以前已经能够找到问题的根源。

我在'base.html'模板中有Bootstrap JS脚本。特别是jQuery。因此,这与子模板中的jQuery导入冲突。

<!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

删除此行后,表排序器已启动并正在运行。

<!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

值得注意的另一件事是,您不应将headbody标记放在{% block content %}内,而希望对脚本,标题和内容使用不同的块页。

基本模板

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
   <title>
     {% block title %} {% endblock title %}
   </title>
     {% block scripts %} {% endblock scripts %}
  </head>
  <body>
   {% block content %} {% endblock content %}
  </body> 

子模板:

{% extends 'base.html' %}

{% block title %}
  Players - NHL stats tracker
{% endblock title %}

{% block scripts %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.1/js/jquery.tablesorter.js"></script>
{% endblock scripts %}

{% block content %}
<table id="tab1" class="tablesorter">
    <thead>
    <tr>
        <th>Month</th>
        <th>Savings</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>January</td>
        <td>$100</td>
    </tr>
    <tr>
        <td>February</td>
        <td>$80</td>
    </tr>
    </tbody>
    <tr>
        <td>Sum</td>
        <td>$180</td>
    </tr>
</table>
{% endblock content %}