Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 排序表自定义td属性_Javascript_Sorttable.js - Fatal编程技术网

Javascript 排序表自定义td属性

Javascript 排序表自定义td属性,javascript,sorttable.js,Javascript,Sorttable.js,下面是sorttable.js中的代码,我使用它对表进行排序(CTRL+F“row.cells.length”来查找我认为导致我的问题的函数) /* 表分拣机v2.5 向表列添加双向排序。 版权所有2005数字线路,苏格兰 版权所有2007尼尔·弗雷泽,加利福尼亚州 版权所有2011谷歌公司。 https://neil.fraser.name/software/tablesort/ 根据Apache许可证2.0版(以下简称“许可证”)获得许可; 除非遵守许可证,否则不得使用此文件。 您可以通过以

下面是sorttable.js中的代码,我使用它对表进行排序(CTRL+F“row.cells.length”来查找我认为导致我的问题的函数)

/*
表分拣机v2.5
向表列添加双向排序。
版权所有2005数字线路,苏格兰
版权所有2007尼尔·弗雷泽,加利福尼亚州
版权所有2011谷歌公司。
https://neil.fraser.name/software/tablesort/
根据Apache许可证2.0版(以下简称“许可证”)获得许可;
除非遵守许可证,否则不得使用此文件。
您可以通过以下方式获得许可证副本:
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,软件
根据许可证进行的分发是按“原样”进行分发的,
无任何明示或暗示的保证或条件。
请参阅许可证以了解管理权限和权限的特定语言
许可证下的限制。
在您的页面上包括:
*/
//命名空间对象。
var TableSort={};
/**
*切换以启用或禁用表端口。
*/
TableSort.enabled=true;
/**
*向上指示器的HTML(从小到大排序)。
*/
TableSort.arrowUp='▲;';
/**
*向下指示器的HTML(从大到小排序)。
*/
TableSort.arrowDown='▼;';
/**
*未排序指示器的HTML。
*/
TableSort.arrowne='';
/**
*鼠标悬停在排序链接上时显示的工具提示。
*/
TableSort.titleText='按此列排序';
/**
*所有表格的列表。
*@私人
*/
TableSort.tables=[];
/**
*上次表是根据哪一列排序的-=向上,+=向下
*@私人
*/
TableSort.lastSort=[];
/**
*把所有的桌子整理好。
*/
TableSort.initAll=函数(){
如果(!TableSort.enabled){
返回;
}
var tableNodeList=document.getElementsByTagName('TABLE');
for(var x=0,table;table=tableNodeList[x];x++){
TableSort.initTable(表);
}
};
/**
*使一个或多个表可排序。
*使用创建的任何表的ID调用此函数
*在页面加载后使用DTHML。
*@param{…string}var_args表的ID。
*/
TableSort.init=函数(var_args){
如果(!TableSort.enabled){
返回;
}
对于(var x=0;x0;
TableSort.lastSort_uT]*=-1;
}否则{
TableSort.lastSort_uT]=列+1;
}
//在每个页眉/页脚单元格上显示正确的箭头。
var spanMatchAll=new RegExp('\\b表格排序'+t+'\\d+\\b');
var spanMatchExact=new RegExp('\\b表格排序'+t+'.''.+column+'\\b');
var SPAN=table.getElementsByTagName('SPAN');
对于(var s=0,span;span=span[s];s++){
if(span.className&&spanMatchAll.test(span.className)){
if(spanMatchExact.test(span.className)){
如果(反向){
span.innerHTML=TableSort.arrowDown;
}否则{
span.innerHTML=TableSort.arrowUp;
}
}否则{
span.innerHTML=TableSort.arrowne;
}
}
}
//获取表的数据并将其存储在字典(assoc数组)中。
如果(!table.tBodies.length){return;}//
/*
Table Sorter v2.5
Adds bi-directional sorting to table columns.
Copyright 2005 Digital Routes, Scotland
Copyright 2007 Neil Fraser, California
Copyright 2011 Google Inc.
https://neil.fraser.name/software/tablesort/

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Include on your page:
  <SCRIPT LANGUAGE='JavaScript1.2' SRC='tablesort.js'></SCRIPT>
*/

// Namespace object.
var TableSort = {};

/**
 * Switch to enable or disable the TableSort.
 */
TableSort.enabled = true;

/**
 * HTML for up indicator (sorted small to big).
 */
TableSort.arrowUp = ' &#x25b2;';

/**
 * HTML for down indicator (sorted big to small).
 */
TableSort.arrowDown = ' &#x25bc;';

/**
 * HTML for unsorted indicator.
 */
TableSort.arrowNone = ' &nbsp;';

/**
 * Tooltip to display when mousing over a sorting link.
 */
TableSort.titleText = 'Sort by this column';

/**
 * List of all the tables.
 * @private
 */
TableSort.tables_ = [];

/**
 * Upon which column was the table sorted last time.  -=up, +=down
 * @private
 */
TableSort.lastSort_ = [];


/**
 * Make all tables sortable.
 */
TableSort.initAll = function() {
  if (!TableSort.enabled) {
    return;
  }
  var tableNodeList = document.getElementsByTagName('TABLE');
  for (var x = 0, table; table = tableNodeList[x]; x++) {
    TableSort.initTable_(table);
  }
};


/**
 * Make one or more tables sortable.
 * Call this function with the ID(s) of any tables which are created
 * with DTHML after the page has loaded.
 * @param {...string} var_args ID(s) of tables.
 */
TableSort.init = function(var_args) {
  if (!TableSort.enabled) {
    return;
  }
  for (var x = 0; x < arguments.length; x++) {
    var table = document.getElementById(arguments[x]);
    if (table) {
      TableSort.initTable_(table);
    }
  }
};


/**
 * Turn all the header/footer cells of one table into sorting links.
 * @param {Element} table The table to be converted.
 * @private
 */
TableSort.initTable_ = function(table) {
  TableSort.tables_.push(table);
  var t = TableSort.tables_.length - 1;
  if (table.tHead) {
    for (var y = 0, row; row = table.tHead.rows[y]; y++) {
      for (var x = 0, cell; cell = row.cells[x]; x++) {
        TableSort.linkCell_(cell, t, x);
      }
    }
  }
  if (table.tFoot) {
    for (var y = 0, row; row = table.tFoot.rows[y]; y++) {
      for (var x = 0, cell; cell = row.cells[x]; x++) {
        TableSort.linkCell_(cell, t, x);
      }
    }
  }
  TableSort.lastSort_[t] = 0;
};


/**
 * Turn one header/footer cell into a sorting link.
 * @param {!Element} cell The TH or TD to be made a link.
 * @param {number} t Index of table in TableSort array.
 * @param {number} x Column index.
 * @private
 */
TableSort.linkCell_ = function(cell, t, x) {
  if (TableSort.getClass_(cell)) {
    var link = document.createElement('A');
    link.href = 'javascript:TableSort.click(' + t + ', ' + x + ', "' +
        escape(TableSort.getClass_(cell)) + '");';
    if (TableSort.titleText) {
      link.title = TableSort.titleText;
    }
    while (cell.hasChildNodes()) {
      link.appendChild(cell.firstChild);
    }
    cell.appendChild(link);
    // Add an element where the sorting arrows will go.
    var arrow = document.createElement('SPAN');
    arrow.innerHTML = TableSort.arrowNone;
    arrow.className = 'TableSort_' + t + '_' + x;
    cell.appendChild(arrow);
  }
};


/**
 * Return the class name for a cell.  The name must match a sorting function.
 * @param {!Element} cell The cell element.
 * @return {string} Class name matching a sorting function.
 * @private
 */
TableSort.getClass_ = function(cell) {
  var className = (cell.className || '').toLowerCase();
  var classList = className.split(/\s+/g);
  for (var x = 0; x < classList.length; x++) {
    if (('compare_' + classList[x]) in TableSort) {
      return classList[x];
    }
  }
  return '';
};


/**
 * Sort the rows in this table by the specified column.
 * @param {number} t Index of table in TableSort array.
 * @param {number} column Index of the column to sort by.
 * @param {string} mode Sorting mode (e.g. 'nocase').
 */
TableSort.click = function(t, column, mode) {
  var table = TableSort.tables_[t];
  if (!mode.match(/^[_a-z0-9]+$/)) {
    alert('Illegal sorting mode type.');
    return;
  }
  var compareFunction = TableSort['compare_' + mode];
  if (typeof compareFunction != 'function') {
    alert('Unknown sorting mode: ' + mode);
    return;
  }
  // Determine and record the direction.
  var reverse = false;
  if (Math.abs(TableSort.lastSort_[t]) == column + 1) {
    reverse = TableSort.lastSort_[t] > 0;
    TableSort.lastSort_[t] *= -1;
  } else {
    TableSort.lastSort_[t] = column + 1;
  }
  // Display the correct arrows on every header/footer cell.
  var spanMatchAll = new RegExp('\\bTableSort_' + t + '_\\d+\\b');
  var spanMatchExact = new RegExp('\\bTableSort_' + t + '_' + column + '\\b');
  var spans = table.getElementsByTagName('SPAN');
  for (var s = 0, span; span = spans[s]; s++) {
    if (span.className && spanMatchAll.test(span.className)) {
      if (spanMatchExact.test(span.className)) {
        if (reverse) {
          span.innerHTML = TableSort.arrowDown;
        } else {
          span.innerHTML = TableSort.arrowUp;
        }
      } else {
        span.innerHTML = TableSort.arrowNone;
      }
    }
  }
    // Fetch the table's data and store it in a dictionary (assoc array).
    if (!table.tBodies.length) { return; }// No data in table.
    var tablebody = table.tBodies[0];
    var cellDictionary = [];

    for (var y = 0, row; row = tablebody.rows[y]; y++)
    {
        var cell;

        if (row.cells.length)
        {
            cell = row.cells[column];

//          None of these lines work:
//          cell = row.cells[column].attr("actual_value");
//          cell = row.cells.attr("actual_value");
//          cell = row.cells[column].attributes[0].value;
        }
        else
        { // Dodge Safari 1.0.3 bug
            cell = row.childNodes[column];
        }

        cellDictionary[y] = [TableSort.dom2txt_(cell), row];
    }

    // Sort the dictionary.
    cellDictionary.sort(compareFunction);

    // Rebuild the table with the new order.
    for (y = 0; y < cellDictionary.length; y++)
    {
        var i = reverse ? (cellDictionary.length - 1 - y) : y;
        tablebody.appendChild(cellDictionary[i][1]);
    }
};


/**
 * Recursively build a plain-text version of a DOM structure.
 * Bug: whitespace isn't always correct, but shouldn't matter for tablesort.
 * @param {Element} obj Element to flatten into text.
 * @return {string} Plain-text contents of element.
 * @private
 */
TableSort.dom2txt_ = function(obj) {
  if (!obj) {
    return '';
  }
  if (obj.nodeType == 3) {
    return obj.data;
  }
  var textList = [];
  for (var x = 0, child; child = obj.childNodes[x]; x++) {
    textList[x] = TableSort.dom2txt_(child);
  }
  return textList.join('');
};


/**
 * Case-sensitive sorting.
 * Compare two dictionary structures and indicate which is larger.
 * @param {!Array} a First tuple.
 * @param {!Array} b Second tuple.
 * @return {number} Number indicating which param is larger (-1/0/1).
 */
TableSort['compare_sort_string_case'] = function(a, b) {
  if (a[0] == b[0]) {
    return 0;
  }
  return (a[0] > b[0]) ? 1 : -1;
};

/**
 * Case-insensitive sorting.
 * Compare two dictionary structures and indicate which is larger.
 * @param {Array} a First tuple.
 * @param {Array} b Second tuple.
 * @return {number} Number indicating which param is larger (-1/0/1).
 */
TableSort['compare_sort_string'] = function(a, b) {
  var aLower = a[0].toLowerCase();
  var bLower = b[0].toLowerCase();
  if (aLower == bLower) {
    return 0;
  }
  return (aLower > bLower) ? 1 : -1;
};

/**
 * Numeric sorting.
 * Compare two dictionary structures and indicate which is larger.
 * @param {Array} a First tuple.
 * @param {Array} b Second tuple.
 * @return {number} Number indicating which param is larger (-1/0/1).
 */
TableSort['compare_sort_int'] = function(a, b) {
  var aNum = parseFloat(a[0]);
  if (isNaN(aNum)) {
    aNum = -Number.MAX_VALUE;
  }
  var bNum = parseFloat(b[0]);
  if (isNaN(bNum)) {
    bNum = -Number.MAX_VALUE;
  }
  if (aNum == bNum) {
    return 0;
  }
  return (aNum > bNum) ? 1 : -1;
};


if (window.addEventListener) {
  window.addEventListener('load', TableSort.initAll, false);
} else if (window.attachEvent) {
  window.attachEvent('onload', TableSort.initAll);
}

if (navigator.appName == 'Microsoft Internet Explorer' &&
    navigator.platform.indexOf('Mac') == 0) {
  // The Mac version of MSIE is way too buggy to deal with.
  TableSort.enabled = false;
}

// Export symbols in case of agressive compilation.
window['TableSort'] = TableSort;
TableSort['click'] = TableSort.click;
<td actual_value="0">Jan 1 1970</td>
TableSort.dom2txt_ = function(obj) {
  if (!obj) {
    return '';
  }

  if (obj.nodeType == 3) {
    return obj.data;
  }

  if (obj.hasAttribute('actual_value')) {
    return obj.getAttribute('actual_value')
  }


  var textList = [];
  for (var x = 0, child; child = obj.childNodes[x]; x++) {
    textList[x] = TableSort.dom2txt_(child);
  }
  return textList.join('');
};