Html排序表优先级不起作用

问题描述 投票:1回答:1

我正在为SharePoint 2010编写此代码。

现在它可以按字母顺序对表进行排序,但我需要它与众不同。而且遗憾的是它没有改变变量a和b来让它改变。我想获取优先级并将其移动到列表中的下一个优先级。它不是因为一些奇怪的原因无法弄清楚我在做什么。我试图提醒我A和B的变化及其未定义的变化......

function sortStatusTable(n) {
  var table, rows, switching, i, x, y, a, b, shouldSwitch, dir, switchcount =  0;
  table = document.getElementById("claimsTable");
  switching = true;
  //Set the sorting direction to ascending:
       dir = "asc"; 
  /*Make a loop that will continue until
  no switching has been done:*/
  while (switching) {
  //start by saying: no switching is done:
  switching = false;
  rows = table.getElementsByTagName("TR");
  /*Loop through all table rows (except the
  first, which contains table headers):*/
  for (i = 1; i < (rows.length - 1); i++) {
    //start by saying there should be no switching:
    shouldSwitch = false;
    /*Get the two elements you want to compare,
    one from current row and one from the next:*/
    x = rows[i].getElementsByTagName("TD")[n];
    y = rows[i + 1].getElementsByTagName("TD")[n];


    if (x.innerHTML.toLowerCase() == "Critical"){
    a = "a";
   }

    if (x.innerHTML.toLowerCase() =="High"){
    a = "b";
   }

    if (x.innerHTML.toLowerCase() =="Medium"){
    a = "c";
   }

    if (x.innerHTML.toLowerCase() =="Low"){
    a = "d";
   }

   if (y.innerHTML.toLowerCase() =="Critical"){
    b = "a";
   }

    if (y.innerHTML.toLowerCase() =="High"){
    b = "b";
   }

    if (y.innerHTML.toLowerCase() =="Medium"){
    b = "c";
   }

    if (y.innerHTML.toLowerCase() =="Low"){
    b = "d";
   }

   console(a);
   console(b)
    /*check if the two rows should switch place,
    based on the direction, asc or desc:*/
   if (dir == "asc") {
      if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
        //if so, mark as a switch and break the loop:
        shouldSwitch= true;
        break;
      }
    } else if (dir == "desc") {
      if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
        //if so, mark as a switch and break the loop:
        shouldSwitch= true;
        break;
      }
    }
  }
  if (shouldSwitch) {
    /*If a switch has been marked, make the switch
    and mark that a switch has been done:*/
    rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
    switching = true;
    //Each time a switch is done, increase this count by 1:
    switchcount ++;      
  } else {
    /*If no switching has been done AND the direction is "asc",
    set the direction to "desc" and run the while loop again.*/
    if (switchcount == 0 && dir == "asc") {
      dir = "desc";
      switching = true;
    }
  }
   }
}
javascript html sorting html-table
1个回答
0
投票

我犯的错误就是当我做出if语句时,我从来没有考虑过名字都是小写的事实。 “中等”而不是“中等”。

if (y.innerHTML.toLowerCase() =="medium"){
    var b = "c";
 }

然后我做了这个部分

if (dir == "asc") {
    if (a > b) {
      //if so, mark as a switch and break the loop:
      shouldSwitch= true;
      break;
    }
  } else if (dir == "desc") {
    if (a < b) {
      //if so, mark as a switch and break the loop:
      shouldSwitch= true;
      break;
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.