2017-04-14 160 views
3

我從來沒有很好的編碼,我做的主要是PHP不是JavaScript。這是運行在Windows服務器上,我無法安裝任何新的東西。從javascript變量創建表格

我有一個網頁,別人建立,我想重新格式化。目前的是一個OpenLayers地圖。

我想把地圖上的信息放在桌子上。

PowerShell腳本將數據輸出到txt文件。

網頁導入的數據做了一些不同的事情。

我結束了三列我想放在桌子上的東西,那裏只有20行。

這是我到目前爲止嘗試過的。

<table id="myTable"> 
    <tr> 
    <td>Office</td> 
    <td>Server Ping</td> 
    <td>Circuit Ping</td> 
</table> 


function myCreateFunction() { 
    var table = document.getElementById("myTable"); 
    var row = table.insertRow(0); 
    var cell1 = row.insertCell(0); 
    var cell2 = row.insertCell(1); 
    var cell3 = row.insertCell(2); 
    cell1.innerHTML = fullname; 
    cell2.innerHTML = pingResultHTML; 
    cell3.innerHTML = circuitPrimaryResultHTML; 
    }; 

如果需要,我可以發佈更多或全部的網頁代碼。

我正在假設,如果變量可以用來填充地圖上的彈出信息,我應該能夠將它們重定向到表中。

編輯: 這是附加信息。 這是從txt文件調用數據的內容。

if (window.XMLHttpRequest) 
    {xmlhttp=new XMLHttpRequest(); 
} 
else 
{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
}; 

xmlhttp.open("GET","latest_results_list.txt",false); 
xmlhttp.send(); 
xmlDoc=xmlhttp.responseText; 

var mySplitResult; 
mySplitResult = xmlDoc.split("\r\n"); 
for(i = 0; i < mySplitResult.length; i++) 
    { 
    var locationStringCleaned = mySplitResult[i].replace(/(\r\n|\n|\r)/gm,""); 


    officeParameters = locationStringCleaned.split(","); 
    officelocation = officeParameters[0]; 
    pingStatus = officeParameters[1]; 
    pingTime = officeParameters[2]; 
    primaryCircuit = officeParameters[3]; 
    primaryCircuitTime = officeParameters[4]; 


    addNewLocation(officelocation, pingStatus, pingTime, primaryCircuit, primaryCircuitTime); 

這增加了網站名稱。

 if (officename == "SHO"){ 
fullname = "Stevens Point, WI"; 
    } else if (officename == "RIC"){ 
fullname = "Richmond, VA"; 
    } else if (officename == "DAV"){ 
fullname = "Davenport, IA"; 
    } else if (officename == "GOL"){ 
fullname = "Goldsboro, NC"; 
    } else if (officename == "IRV"){ 
fullname = "Irvine, CA"; 
    } else if (officename == "MON"){ 
fullname = "Montgomery, AL"; 
    } else if (officename == "MAD"){ 
fullname = "Madison, WI"; 
    } else if (officename == "SAL"){ 
fullname = "Salem, OR"; 
    } else if (officename == "SCO"){ 
fullname = "Scottsdale, AZ"; 
    } else if (officename == "WES"){ 
fullname = "Westford, MA"; 
    } else if (officename == "FRE"){ 
fullname = "Freeport, IL"; 
    } else if (officename == "MIL"){ 
fullname = "Milwaukee, WI"; 
    } else if (officename == "AVI"){ 
fullname = "Stevens Point, WI"; 
    } else if (officename == "PLO"){ 
fullname = "Plover, WI"; 
    } else if (officename == "MADG"){ 
fullname = "Madison, WI"; 
    } else if (officename == "MADC"){ 
fullname = "Madison, WI"; 
    } else if (officename == "MADR"){ 
fullname = "Madison, WI"; 
    } else if (officename == "EDW"){ 
fullname = "Edwardsville, IL"; 
    } else if (officename == "CHA"){ 
fullname = "Charlotte, NC"; 

這些格式的文本顏色基於通過其他一些if語句運行後的ping。

pingResultHTML = "<span style='color:" + pingColor + "'>" + pingResult + " (" + pingTime + " ms)</span>"; 
circuitPrimaryResultHTML = "<span style='color:" + linkStatusPrimaryColor + "'>" + linkStatusPrimary + " (" + linkStatusPrimaryTime+ " ms)</span>"; 

這是txt文件中的數據示例。

MAD,GOOD,7,GOOD,7 
DAV,GOOD,30,GOOD,30 
+0

如果你有20〜行,你在哪裏存儲這些信息? (*一個對象數組?需要反序列化的JSON?*)或者每次需要添加一行時調用myCreateFunction()(即調用函數〜20次)? – SpencerD

+0

你有沒有考慮過angularjs?這很容易使用角... – rbtLong

+0

我不知道它是如何存儲的。我注意到每次點擊彈出窗口時是否從txt文檔取回它?但文檔中的數據也決定了地圖上每個點的顏色。 – mouseskowitz

回答

0

首先,你需要適應在JavaScript理解的形式就像對象數據,地圖&無論.....

,那麼你只是需要迭代,像但─ 讓假設數據包含你映射。

var rowCount=1, 
table = document.getElementById("myTable"), 
tbdy = document.createElement('tbody'); 
    for (var k in data) { 
    var tr = document.createElement('tr'); 
     for (var j = 0; j < 3; j++){ 
      var td = document.createElement('td'); 
      td.appendChild(document.createTextNode((!j)?rowCount++:(j==1)?k:device[k])); 
      tr.appendChild(td); 
     } 
       tbdy.appendChild(tr); 
    } 
table.appendChild(tbdy); 
0

我建議使用類似OOP:

function MyTable(data) { 
     this.data = data; 
     this.rows = []; 
     this.container = document.createElement('table'); 
     this.body = document.createElement('tbody'); 
     this.container.appendChild(this.body); 
     this.render(); 
    } 

    MyTable.prototype.render = function(){ 
     this.body.innerHTML = ''; 
     for(var i = 0; i < this.data.length; i++){ 
      this.rows.push(new MyRow(this.data[i])); 
     } 
     for(var j = 0; j < this.rows.length; j++) { 
      this.body.appendChild(this.rows[j].container); 
     } 
    } 

    MyTable.prototype.setData= function(data){ 
     this.data = data; 
     this.rows = []; 
     this.render(); 
    } 

    function MyRow(data) { 
     this.container = document.createElement('tr'); 
     var name = document.createElement('td'); 
     name.innerHTML = data.name; 
     this.container.appendChild(name); 
    } 

    var myTable = new MyTable([{name: 'Joe'}, {name: 'Pit'}]); 
    document.body.appendChild(myTable.container); 
    //data update: 
    myTable.setData([{name: 'Alex'}, {name: 'Stephan'}]); 
    myTable.render(); 
+0

你能幫我理解這是如何工作的一點點。我可以看到這使用至少一個變量構建了一個表。我如何使用這個每行三個變量,每列一個?此外,這是否會在原始數據被修改之前或之後? – mouseskowitz

+0

我更新了答案。另外請記住,這只是一個從頭開始。當然,你可以通過一些回調,自定義單元格渲染器等來擴展它 – degr