javascript - 想要使用提供的 js 仅使用第 11 列和第 12 列进行排序

标签 javascript sorting

请检查jsFiddle Link在这里,使用第三列进行排序。我已将其集成到我的代码中,该代码中有 13 列表。但我无法找出所提供的 javascript 中要更改代码的哪一部分,因此只有单击特定标题才能对表格进行排序。比如说,本例中的 header 3(但我希望在我的 header 中 header 11)请查看 js 并帮助我找出要更改/编辑的 LOC

我在MVC中使用它。 请帮助我使用下面的js代码,找到要更改的行。

 var t = new SortableTable(document.getElementById('myTable1'), 100);

function SortableTable(tableEl) {

 this.tbody = tableEl.getElementsByTagName('tbody');
 this.thead = tableEl.getElementsByTagName('thead');
 this.tfoot = tableEl.getElementsByTagName('tfoot');

 this.getInnerText = function (el) {
     if (typeof (el.textContent) != 'undefined') return el.textContent;
     if (typeof (el.innerText) != 'undefined') return el.innerText;
     if (typeof (el.innerHTML) == 'string') return el.innerHTML.replace(/<[^<>]+>/g, '');
 }

 this.getParent = function (el, pTagName) {
     if (el == null) return null;
     else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) return el;
     else return this.getParent(el.parentNode, pTagName);
 }

 this.sort = function (cell) {

     var column = cell.cellIndex;
     var itm = this.getInnerText(this.tbody[0].rows[1].cells[column]);
     var sortfn = this.sortCaseInsensitive;

     if (itm.match(/\d\d[-]+\d\d[-]+\d\d\d\d/)) sortfn = this.sortDate; // date format mm-dd-yyyy
     if (itm.replace(/^\s+|\s+$/g, "").match(/^[\d\.]+$/)) sortfn = this.sortNumeric;

     this.sortColumnIndex = column;

     var newRows = new Array();
     for (j = 0; j < this.tbody[0].rows.length; j++) {
         newRows[j] = this.tbody[0].rows[j];
     }

     newRows.sort(sortfn);

     if (cell.getAttribute("sortdir") == 'down') {
         newRows.reverse();
         cell.setAttribute('sortdir', 'up');
     } else {
         cell.setAttribute('sortdir', 'down');
     }

     for (i = 0; i < newRows.length; i++) {
         this.tbody[0].appendChild(newRows[i]);
     }

 }

 this.sortCaseInsensitive = function (a, b) {
     aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]).toLowerCase();
     bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]).toLowerCase();
     if (aa == bb) return 0;
     if (aa < bb) return -1;
     return 1;
 }

 this.sortDate = function (a, b) {
     aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]);
     bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]);
     date1 = aa.substr(6, 4) + aa.substr(3, 2) + aa.substr(0, 2);
     date2 = bb.substr(6, 4) + bb.substr(3, 2) + bb.substr(0, 2);
     if (date1 == date2) return 0;
     if (date1 < date2) return -1;
     return 1;
 }

 this.sortNumeric = function (a, b) {
     aa = parseFloat(thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]));
     if (isNaN(aa)) aa = 0;
     bb = parseFloat(thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]));
     if (isNaN(bb)) bb = 0;
     return aa - bb;
 }

 // define variables
 var thisObject = this;
 var sortSection = this.thead;

 // constructor actions
 if (!(this.tbody && this.tbody[0].rows && this.tbody[0].rows.length > 0)) return;

 if (sortSection && sortSection[0].rows && sortSection[0].rows.length > 0) {
     var sortRow = sortSection[0].rows[0];
 } else {
     return;
 }

 for (var i = 0; i < sortRow.cells.length; i++) {
     sortRow.cells[i].sTable = this;
     sortRow.cells[i].onclick = function () {
         this.sTable.sort(this);
         return false;
     }
 }

} 我还需要对带有小数位的列进行排序。

最佳答案

您的函数仅对数字或日期值字段进行排序,因此如果您的列号 11 将包含数字数据,那么它将能够进行排序,否则不能进行排序。

下面的代码是您的函数中的原因,它将从整个表中查找数值,然后对其进行排序。

if (itm.match(/\d\d[-]+\d\d[-]+\d\d\d\d/)) sortfn = this.sortDate; // date format mm-dd-yyyy
     if (itm.replace(/^\s+|\s+$/g, "").match(/^[\d\.]+$/)) sortfn = this.sortNumeric;

我也用十进制数尝试你的代码。

Fiddle with decimal number

编辑: 更好的排序方式 Sorting array of float point numbers

关于javascript - 想要使用提供的 js 仅使用第 11 列和第 12 列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24547348/

相关文章:

python - 如何按字母顺序对字典的键进行排序

c - 按特定顺序对 argv 进行排序

python - 如何根据特定行中的值对 numpy 数组进行排序?

javascript - HTML5 Canvas 不显示文本

javascript - 在 Nuxt.Js 中为 document.title 添加一个观察者

javascript - 为什么我在我的函数中得到了错误的父节点?

python - 在python中按优先级对矩阵进行排序

arrays - 按下按钮时将生成的单词保存到单词数组/列表中

javascript - 获取每个表单部分的 id 和值

javascript - 使用 Ionic 3 从 JSON 数据筛选器的搜索栏