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

Jquery tablesorter plugin does not work in Django template

尝试在 Django 模板中使用 table 排序 JQuery 插件。为 jquery 和 table 排序文件尝试了不同的来源。似乎没有与他们中的任何一个一起工作。模板页面上没有任何变化。我要排序的真正 table 是 tab2。创建了一个简单的 tab1 仅用于测试。也不适用于此 table。尝试遵循这些说明 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>0</td>
   </tr>
   <tr>
     <td>February</td>
     <td></td>
   </tr>
 </tbody>
   <tr>
     <td>Sum</td>
     <td>0</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 %}

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>0</td>
    </tr>
    <tr>
        <td>February</td>
        <td></td>
    </tr>
    </tbody>
    <tr>
        <td>Sum</td>
        <td>0</td>
    </tr>
</table>

前段时间找到了问题的根源。

我在 'base.html' 模板中有 Bootstrap 个 JS 脚本。尤其是 jQuery。所以它与 child 模板中的 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>

删除此行后,tablesorter 启动并且 运行。

<!-- 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> 

Child 模板:

{% 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>0</td>
    </tr>
    <tr>
        <td>February</td>
        <td></td>
    </tr>
    </tbody>
    <tr>
        <td>Sum</td>
        <td>0</td>
    </tr>
</table>
{% endblock content %}