2017-07-25 77 views
0

假設這構成了一個表:如何在Javascript中使用數組創建表格對象原型?

rows: 
      [ 
       //TABLE 1 
       { //TABLE 1 TITLE HEADER 
        cells: [ 
        {value: ""}, 
        {value: "Table 1", textAlign: "center", bold: "true"} 
        ] 
       }, 
       { // Header A 
        cells: [ 
        {value: ""}, 
        {value: "Month", textAlign: "center", verticalAlign: "center", background: "rgb(198,217,241)", bold: "true"}, 
        {value: "Metric", textAlign: "center", bold: "true"}, 
        {value: ""}, 
        {value: "Achievement (%)", textAlign: "center", verticalAlign: "center", bold: "true"}, 
        {value: "Weight (%)", textAlign: "center", bold: "true"}, 
        ] 
       }, 
       { // Header B 
        cells: [ 
        {value: ""}, 
        {value: ""}, 
        {value: "Plan", textAlign: "center", background: "rgb(192,0,0)", bold: "true", color:"white"}, 
        {value: "Actual", textAlign: "center", background: "rgb(0,176,80)", bold: "true", color:"white"}, 
        {value: ""}, 
        {value: "50", textAlign: "center", background: "rgb(198,217,241)"}] 
       }, 
       { // Table1 row1 
        cells: [ 
        {value: ""}, 
        {value: "1", textAlign: "center"}, 
        {value: "", textAlign: "center", background: "rgb(242,220,219)"}, 
        {value: "", textAlign: "center", background: "rgb(235,241,222)"}, 
        {value: "", textAlign: "center", background: "rgb(198,217,241)", bold:"true"}, 
        { value: "", textAlign: "center", background: "rgb(198,217,241)", bold:"true"}] 
       }, 
       { // Table1 row2 
        cells: [ 
        {value: ""}, 
        {value: "2", textAlign: "center"}, 
        {value: "", textAlign: "center", background: "rgb(242,220,219)" }, 
        {value: "", textAlign: "center", background: "rgb(235,241,222)"}, 
        {value: "", textAlign: "center", background: "rgb(198,217,241)", bold:"true"}, 
        {value: "", textAlign: "center", background: "rgb(198,217,241)", bold:"true"}] 
       } 
       { // FOOTER 
        cells: [ 
        {value: ""}, 
        {value: "Average per month", textAlign: "center", background: "rgb(198,217,241)", bold:"true"}, 
        {value: ""}, 
        {value: "", textAlign: "center", background: "rgb(198,217,241)", bold:"true"}, 
        {value: "", textAlign: "center", background: "rgb(198,217,241)", bold:"true"}, 
        {value: "", textAlign: "center", background: "rgb(255,192,0)", bold:"true"}] 
       } 
      ] 

如何使該表對象的原型,行,所以我可以重複使用多行多臺的情況下,考慮到表包括

  • 1標題報頭
  • 1頭A和頭1乙
  • 至少1行,但是可以無限期地進行迭代
  • 1頁腳

我想要做的是爲「表」和行對象創建一個構造函數/原型函數,這樣我就可以通過增加索引號來循環「行」,而無需手動重寫/添加具有相同模式的行/表。

更新:要添加更多的上下文,this fiddle顯示我想迭代的「表」和「行」。

+0

什麼你試過嗎? –

+0

什麼問題? –

+0

抱歉不清楚!基本上我想爲「表」創建一個構造函數/原型,這樣我就可以存儲「行」對象以及循環行,而無需手動重寫/添加具有相同模式的行。 –

回答

0

像這樣的事情類樣實現?:

var Tabel = (function() { 
 
    function Tabel(params) { 
 
     if (params === void 0) { params = {}; } 
 
     this.table = (params.table == void 0 ? document.createElement("table") : params.table); 
 
     if (params.attributes != void 0) { 
 
      for (var key in params.attributes) { 
 
       if (params.attributes.hasOwnProperty(key)) { 
 
        var attribute = params.attributes[key]; 
 
        this.table.setAttribute(key, attribute); 
 
       } 
 
      } 
 
     } 
 
     this.thead = this.table.appendChild(document.createElement("thead")); 
 
     this.tbody = this.table.appendChild(document.createElement("tbody")); 
 
     this.tfoot = this.table.appendChild(document.createElement("tfoot")); 
 
     this.rows = (params.rows == void 0 ? [] : params.rows) 
 
      .sort(function (a, b) { 
 
      var A = (a.headOrFoot == void 0 ? 0 : (a.headOrFoot ? 1 : -1)); 
 
      var B = (b.headOrFoot == void 0 ? 0 : (b.headOrFoot ? 1 : -1)); 
 
      return B - A; 
 
     }); 
 
     this.render(); 
 
    } 
 
    Tabel.prototype.render = function() { 
 
     cancelAnimationFrame(this.renderHandle); 
 
     this.renderHandle = requestAnimationFrame(this._render.bind(this)); 
 
    }; 
 
    Tabel.prototype._render = function() { 
 
     this.thead.innerHTML = this.tbody.innerHTML = this.tfoot.innerHTML = ""; 
 
     for (var rowIndex = 0; rowIndex < this.rows.length; rowIndex++) { 
 
      var row = this.rows[rowIndex]; 
 
      var inHead = (row.headOrFoot == void 0 ? 0 : (row.headOrFoot ? 1 : -1)); 
 
      var tr = void 0; 
 
      if (inHead == 1) { 
 
       tr = this.thead.appendChild(document.createElement("tr")); 
 
      } 
 
      else if (inHead == 0) { 
 
       tr = this.tbody.appendChild(document.createElement("tr")); 
 
      } 
 
      else { 
 
       tr = this.tfoot.appendChild(document.createElement("tr")); 
 
      } 
 
      for (var cellIndex = 0; cellIndex < row.cells.length; cellIndex++) { 
 
       var cell = row.cells[cellIndex]; 
 
       var td = tr.appendChild(document.createElement(inHead != 0 ? 'th' : 'td')); 
 
       if (cell.attributes != void 0) { 
 
        for (var key in cell.attributes) { 
 
         if (cell.attributes.hasOwnProperty(key)) { 
 
          var attribute = cell.attributes[key]; 
 
          td.setAttribute(key, attribute); 
 
         } 
 
        } 
 
       } 
 
       td.innerHTML = cell.data.toString(); 
 
      } 
 
     } 
 
    }; 
 
    return Tabel; 
 
}()); 
 
//Test 
 
var t1 = new Tabel({ 
 
    attributes: { 
 
     id: "table-1", 
 
     "class": "table table-striped" 
 
    }, 
 
    rows: [ 
 
     { 
 
      cells: [ 
 
       { data: "ID", attributes: { style: "text-align: center", colspan: 1 } }, 
 
       { data: "Name" }, 
 
       { data: "Month" }, 
 
       { data: "Header" } 
 
      ], 
 
      headOrFoot: true 
 
     }, { 
 
      cells: [ 
 
       { data: "ID", attributes: { style: "text-align: center", colspan: 1 } }, 
 
       { data: "Name" }, 
 
       { data: "Month" }, 
 
       { data: "Footer" } 
 
      ], 
 
      headOrFoot: false 
 
     }, { 
 
      cells: [ 
 
       { data: 1 }, 
 
       { data: "Bob" }, 
 
       { data: "January", attributes: { colspan: 2 } } 
 
      ] 
 
     }, { 
 
      cells: [ 
 
       { data: 2 }, 
 
       { data: "Bill" }, 
 
       { data: "March", attributes: { colspan: 2 } } 
 
      ] 
 
     }, { 
 
      cells: [ 
 
       { data: 3 }, 
 
       { data: "Mona" }, 
 
       { data: "March", attributes: { colspan: 2 } } 
 
      ] 
 
     } 
 
    ] 
 
}); 
 
document.body.appendChild(t1.table);