javascript - 为什么表格排序在我的 table 上不起作用? ( Ajax 表)

标签 javascript jquery html

我正在使用 Ajax 从 MySQL 加载表。 我已经尝试过 tablesorter.com 和其他一些网站一样,但没有一个对我有用。

我找到了这个jsfiddle http://jsfiddle.net/gFzCk/4694/在 stackoverflow 上,它不适用于我的表。

Js代码:-

var table = $('#gods');
$('#tname, #bdmg')
  .wrapInner('<span title="sort this column"/>')
  .each(function() {
  var th = $(this),
      thIndex = th.index(),
      inverse = false;
  th.click(function() {
    table.find('td').filter(function() {
      return $(this).index() === thIndex;
    }).sortElements(function(a, b) {
      return $.text([a]) > $.text([b]) ?
        inverse ? -1 : 1 :
      inverse ? 1 : -1;
    }, function() {
      // parentNode is the element we want to move
      return this.parentNode;
    });
    inverse = !inverse;
  });
});

。 HTML:-

<table id="gods">
   <tr>
      <th>img</th>
      <th id="tname">Name</th>
      <th id="bdmg">Bdmg</th>
      <th>Attspeed</th>
      <th>Physdef</th>
      <th>Magdef</th>
      <th>Health</th>
      <th>Mana</th>
      <th>HP5</th>
      <th>Mspeed</th>
      <th>MP5</th>
      <th>Lifesteal</th>
      <th>Power</th>
   </tr>
</table>

我在这里做错了什么?

最佳答案

您必须在代码中包含库 jquery.sortElements.js 才能实现此功能。

https://rawgithub.com/padolsey/jQuery-Plugins/master/sortElements/jquery.sortElements.js

var table = $('#gods');
    $('#tname, #bdmg')
        .wrapInner('<span title="sort this column"/>')
        .each(function(){

            var th = $(this),
                thIndex = th.index(),
                inverse = false;

            th.click(function(){

                table.find('td').filter(function(){

                    return $(this).index() === thIndex;

                }).sortElements(function(a, b){

                    return $.text([a]) > $.text([b]) ?
                        inverse ? -1 : 1
                        : inverse ? 1 : -1;

                }, function(){

                    // parentNode is the element we want to move
                    return this.parentNode; 

                });

                inverse = !inverse;

            });

        });
td, th { border: 1px solid #111; padding: 6px; }
th { font-weight: 700; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgithub.com/padolsey/jQuery-Plugins/master/sortElements/jquery.sortElements.js"></script>
<table id="gods">
       <tr>
    	<th>img</th>
    	<th id="tname">Name</th>
    	<th id="bdmg">Bdmg</th>
      </tr>
      <tr>
    	<td>Attspeed</td>
    	<td>Physdef</td>
    	<td>Magdef</td>
      </tr>
      <tr>
    	<td>Health</td>
    	<td>Mana</td>
    	<td>HP5</td>
      </tr><tr>
    	<td>Mspeed</td>
    	<td>MP5</td>
    	<td>Lifesteal</td>
      
    </tr>
   </table>

关于javascript - 为什么表格排序在我的 table 上不起作用? ( Ajax 表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49571164/

相关文章:

javascript - 返回变量并再次使用它的正确语法(javascript)

javascript - 如何创建“显示更多”按钮并指定最初可以显示多少行文本

jQuery Slider IE 问题(不工作)

javascript - 悬停时更改 Div 标签属性

javascript - 未捕获的 DOMException( promise 中) - 劫持 serviceworker 中的特定 GraphQL 请求

javascript - jQuery:Div 和变量使用 .after() 函数呈现为 [Object object]

javascript - 在 jQuery 中将选中复选框的值作为 CSV 返回

javascript - 使用 jQuery 显示警告

html - div标签没有移动任何东西

JavaScript : copying data from one array of objects to another which has existing data gives incorrect result