DIV CSS 佈局教程網

封裝自動建立表格的 JavaScript 程式碼
編輯:關於JavaScript     

本文分享一個以 JavaScript 封裝的自動建立表格元件,支援新增、刪除、編輯與上下移動等常見操作,供參考。若你有更好的封裝寫法,也歡迎交流。

關鍵程式碼

<script type="text/javascript">
var currentActiveRow; // 目前選取列的顏色標記
var customTable = function () { };
customTable.prototype = {
  init: {
    ajaxUrl: "",
    tId: "tbody",
    delMsg: "確認要刪除嗎?"
  },
  ajax: function (params, callback) {
    var that = this;
    $.ajax({
      type: "get",
      cache: false,
      dataType: "json",
      url: that.init.ajaxUrl,
      data: params,
      success: arguments[1] || function () { },
      error: arguments[2] || function () { }
    });
  },
  addRow: function () {
    var tbody = document.getElementById(this.init.tId);
    var rowNo = tbody.rows.length;
    tbody.insertRow(rowNo);
    tbody.rows[rowNo].insertCell(0);
    tbody.rows[rowNo].cells[0].appendChild(document.createTextNode(rowNo + 1));
    tbody.rows[rowNo].insertCell(1);
    tbody.rows[rowNo].cells[1].innerHTML = "<input name='radioRMS' type='radio' value='1'></input>";
    tbody.rows[rowNo].insertCell(2);
    tbody.rows[rowNo].cells[2].innerHTML = "<input name='checkboxRMS' type='checkbox' value='1'></input>";
    tbody.rows[rowNo].insertCell(3);
    tbody.rows[rowNo].cells[3].innerHTML = "<input name='descript' type='text' value='des" + (rowNo + 1) + "'></input>";
    tbody.rows[rowNo].insertCell(4);
    tbody.rows[rowNo].cells[4].innerHTML = "<input type='button' value='刪除' onclick='ttable.deleteRow(event)'/><input type='button' value='編輯' onclick='ttable.editRow(event)'/><a href='javascript:void(0)' onclick='ttable.moveUp(this)'>↑</a> <a href='javascript:void(0)' onclick='ttable.moveDown(this)'>↓</a>";
    tbody.rows[rowNo].onclick = ttable.changeActiveRow;
  },
  deleteRow: function (eve) {
    if (confirm(this.init.delMsg)) {
      element = window.event ? window.event.srcElement : eve.target;
      var rowNo = element.parentNode.parentNode.rowIndex;
      var tbody = document.getElementById(this.init.tId);
      tbody.deleteRow(rowNo - 1);
    }
  },
  changeActiveRow: function () {
    eve = arguments[0];
    element = window.event ? window.event.srcElement : eve.target;
    obj = element.parentNode;
    while (obj && obj.tagName != "TR") {
      obj = obj.parentNode;
      if (currentActiveRow) currentActiveRow.style.backgroundColor = "";
      currentActiveRow = obj;
      currentActiveRow.style.backgroundColor = "Red";
    }
  },
  cleanWhitespace: function (element) {
    for (var i = 0; i < element.childNodes.length; i++) {
      var node = element.childNodes[i];
      if (node.nodeType == 3 && !/\s/.test(node.nodeValue)) node.parentNode.removeChild(node);
    }
  },
  moveUp: function (_a) {
    var _table = document.getElementById(this.init.tId);
    ttable.cleanWhitespace(_table);
    var _row = currentActiveRow;
    if (_row.previousSibling) ttable.swapNode(_row, _row.previousSibling);
  },
  moveDown: function (_a) {
    var _table = document.getElementById(this.init.tId);
    ttable.cleanWhitespace(_table);
    var _row = currentActiveRow;
    if (_row.nextSibling) ttable.swapNode(_row, _row.nextSibling);
  },
  swapNode: function (node1, node2) {
    var _parent = node1.parentNode;
    var _t1 = node1.nextSibling;
    var _t2 = node2.nextSibling;
    if (_t1) _parent.insertBefore(node2, _t1); else _parent.appendChild(node2);
    if (_t2) _parent.insertBefore(node1, _t2); else _parent.appendChild(node1);
  }
};
var ttable = new customTable();
</script>

HTML 結構

<table border="1" id="tableSpan">
  <thead id="thead">
    <tr onclick="ttable.changeActiveRow(this);">
      <td>序號</td>
      <td>缺省</td>
      <td>啟用</td>
      <td>選項內容</td>
      <td>操作</td>
    </tr>
  </thead>
  <tbody id="tbody"></tbody>
</table>
<button onclick="ttable.addRow()">添加</button>
<button onclick="ttable.moveUp()">↑</button>
<button onclick="ttable.moveDown()">↓</button>

這個封裝把常用操作都集中到 customTable 的原型方法中:addRow 動態插入一列並附上操作按鈕,deleteRow 刪除指定列,moveUp/moveDown 搭配 swapNode 交換相鄰列以實現排序,changeActiveRow 則以背景色標示目前選取的列。透過 currentActiveRow 記錄焦點列,讓上下移動能準確作用於目標列,是一個實用又好懂的表格元件範例。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved