APP置顶

/*
* Table 表格操作
* tableOPI
*
*/
(function (window){
  /*
  * _table Table对象,
  * _ctrl ctrl键按下标志,
  * _tbody 表格正文,
  * _selectRow 选中行
  * _trclassUNS 行未选中状态样式
  * _trclassSED 行选中状态样式
  * _trindexColNum 显示行号列 从1开始 0为不设置
  *
  */
  var _table,_ctrl,_tbody,_selectRow;
  var _trclassUNS = "";
  var _trclassSED = "";
  var _trindexColNum = 0;
//Array 排序
  function sortRowIndex(a,b) {
    return a.rowIndex - b.rowIndex
  }
  function sortRowIndexDesc(a,b) {
    return b.rowIndex - a.rowIndex
  }
  /**
   * ctrl键事件
   */
  document.onkeydown = function (e) {
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which;
    if (code == 17) {
      _ctrl = true;
    }
  };
  document.onkeyup = function (e) {
    _ctrl = false;
  };
  /*
  * find  child
  * 找寻特定的子元素
  */
  var findChild = function(obj,name) {
    if(obj.childNodes) {
      for(i=0;i<obj.childNodes.length;i++) {
        if(obj.childNodes[i].nodeName == name) {
          return obj.childNodes[i];
        }
      }
    }
    return null;
  }
  /*
  * 函数 nextSibling,previousSibling 获取下一个或上一个的兄弟节点
  * 在IE下会忽略回车和换行。FF下不会忽略。可以用递归来解决浏览器的差异
  * nodeType = 3是文本节点 nodeType = 1是元素节点 nodeType = 2是属性节点
  */
  var getPreviousSibling = function(obj) {
    do {
      obj = obj.previousSibling;
    } while (obj != null && obj.nodeType != 1)
    return obj;
  }
  var getNextSibling = function(obj) {
    do {
      obj = obj.nextSibling;
    } while (obj != null && obj.nodeType != 1)
    return obj;
  }
  /*
  * 置顶
  */
  var moveTop = function(node){
    //获取父结点
    var _parent=node.parentNode;
    _parent.insertAdjacentElement("afterBegin",node);
  }
  /*
  * 通用的函数交换两个结点的位置
  */
  var swapNode = function(node1,node2){
    //获取父结点
    var _parent=node1.parentNode;
    //获取两个结点的相对位置
    var _t1=getNextSibling(node1);
    var _t2=getNextSibling(node2);
    //将node2插入到原来node1的位置
    if(_t1)_parent.insertBefore(node2,_t1);
    else _parent.appendChild(node2);
    //将node1插入到原来node2的位置
    if(_t2)_parent.insertBefore(node1,_t2);
    else _parent.appendChild(node1);
  }

//重写insertAdjacentElement()方法,因为firefox中没有该方法
  if (window.HTMLElement) {
    HTMLElement.prototype.insertAdjacentElement=function(where,parsedNode){
      switch(where){
        case "beforeBegin":
          this.parentNode.insertBefore(parsedNode,this);
          break;
        case "afterBegin":
          this.insertBefore(parsedNode,this.firstChild);
          break;
        case "beforeEnd":
          this.appendChild(parsedNode);
          break;
        case "afterEnd":
          if(this.nextSibling)
            this.parentNode.insertBefore(parsedNode,this.nextSibling);
          else
            this.parentNode.appendChild(parsedNode);
          break;
      }
    }
  }
  /**
   * 初始化
   * var mypoi = tableOPI(document.getElementById("table"));
   */
  var tableOPI = function(tableObj) {
    return new tableOPI.fn.init(tableObj);
  };
  tableOPI.fn = tableOPI.prototype;
  tableOPI.fn.init = function(tableObj){
    this.buildRange(tableObj);
    return this;
  };
  tableOPI.fn.init.prototype = tableOPI.fn;
  tableOPI.prototype.buildRange = function(tableObj){
    _table = tableObj;
    _tbody = findChild(_table,"TBODY");
    _selectRow = new Array();
    //var _this=this;

  }
  /**
   * 选择并改变选中行的样式
   * trObj 选中行对象
   * classNameUNS 非选样式
   * classNameSED 选中样式
   */
//选择并改变选中行的样式 (需指定非选样式与选中样式)
  tableOPI.prototype.changeBackColorClass = function(trObj,classNameUNS,classNameSED) {
    if(_ctrl) {
      trObj.className = classNameSED;
      _selectRow.push(trObj);
      //排序 按实际行 顺序
      _selectRow.sort(sortRowIndex);
    }
    else {
      var len = _tbody.childNodes.length;
      for(i=0;i<len;i++) {
        if(_tbody.childNodes[i].nodeName == "TR") {
          _tbody.childNodes[i].className = classNameUNS;
        }
      }
      trObj.className = classNameSED;
      _selectRow = new Array();
      _selectRow.push(trObj);
    }
  }
  /*
  * 重置实际行号
  */
  tableOPI.prototype.setRowIndexColNum = function(num) {
    _trindexColNum = num;
  }
  tableOPI.prototype.resetRowIndex = function() {
    if(_trindexColNum==0) {
      return;
    }
    var count = 0;
    var len = _tbody.childNodes.length;
    for(i=0;i<len;i++) {
      if(_tbody.childNodes[i].nodeName == "TR") {
        count = 0;
        for(j=0;j<_tbody.childNodes[i].childNodes.length;j++) {
          if(_tbody.childNodes[i].childNodes[j].nodeName == "TD") {
            count++;
            if(count==_trindexColNum) {
              _tbody.childNodes[i].childNodes[j].firstChild.nodeValue = _tbody.childNodes[i].rowIndex;
              break;
            }
          }
        }
      }
    }
  }

  /*
  *  置顶
  */
  tableOPI.prototype.moveRowTop = function(tdObj) {
    trObj = tdObj.parentNode;
    for(i=0;i<len;i++) {
      if(_tbody.childNodes[i].nodeName == "TR") {
        _tbody.childNodes[i].className = _trclassUNS;
      }
    }
    _selectRow = new Array();
    _selectRow.push(trObj);

    var len = _selectRow.length;
    //没有行选中 提示
    if(len==0) {
      this.unselectedCallBack();
    }
    //循环置顶选中行 从最下一个选中行开始
    for(i=len-1;i>=0;i--) {
      moveTop(_selectRow[i]);
    }
    //重置实际行号
    this.resetRowIndex();
  }

  tableOPI.prototype.unselectedCallBack = function() {
    //alert("please selected one row!");
  }

  window.tableOPI=tableOPI;

})(window);
<td class="numeric-cell" onclick=poi.moveRowTop(this);><img src="template/m_bizhixing/kitchen-sink/core/template/bitssion/img/index/top.png" width="14" height="14"/></td>
/*置顶*/
window.poi;
window.onload=function(){
   poi = tableOPI(document.getElementById("usdtTable"));
   poi.resetRowIndex();
};

猜你喜欢

转载自blog.csdn.net/qq_32705087/article/details/82788023
今日推荐