[英]json to html table in php/javascript
我有一個像這樣的json文件:
{
publicationDate: "28-02-2014",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
signingDate: "28-02-2014"
},
我需要在這樣的表中顯示它:
<table>
<tr>
<td><strong>Data de publicação</strong></td>
<td><strong>Empresa Contratada</strong></td>
<td><strong>Empresa que Contratou</strong></td>
<td><strong>ID</strong></td>
<td><strong>Objecto adquirido</strong></td>
<td><strong>Preço Contratual</strong></td>
<td><strong>Data do Contrato</strong></td>
</tr>
</table>
我是如何用PHP或javascript做的?
非常感謝你們;)
這是一個JSFiddle,它顯示了如何在對象中打印數據:
和代碼:
HTML
<table id="table">
<tr>
</tr>
</table>
JAVASCRIPT
var object = {
publicationDate: "28-02-2014",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
signingDate: "28-02-2014"
};
for (var prop in object) {
// important check that this is objects own property
// not from prototype prop inherited
if(object.hasOwnProperty(prop)){
var td = document.createElement("td");
var strong = document.createElement("strong");
var text = document.createTextNode(prop + " - " + object[prop]);
strong.appendChild(text);
td.appendChild(strong);
document.getElementById("table").appendChild(td);
}
}
編輯更新到angus_thermopylae:
我更新了JSFiddle來展示這個概念: http : //jsfiddle.net/4PVr5/12/
然后,您可以在所需的對象上包含任意數量的屬性,但只打印您定義的屬性以及您定義的順序。 您只需添加一個文本字符串,然后再打印另一個。
編輯更新:我更新了代碼以遵循表頭。 現在它直接添加它們並且還處理具有太少屬性的對象。
HTML
<table id="table">
<thead>
<th id="publicationDate"></th>
<th id="contracted"></th>
<th id="contracting"></th>
<th id="id"></th>
<th id="objectBriefDescription"></th>
<th id="initialContractualPrice"></th>
<th id="signingDate"></th>
</thead>
<tbody>
</tbody>
</table>
JAVASCRIPT
var orderedObject = {
publicationDate: "28-02-2014",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
signingDate: "28-02-2014"
};
var unorderedObject = {
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
signingDate: "28-02-2014",
publicationDate: "28-02-2014",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
};
var toManyPropertiesObject = {
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
signingDate: "28-02-2014",
publicationDate: "28-02-2014",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
newProp: "ignored",
newProp1: "ignored",
newProp2: "ignored",
};
var toFewPropertiesObject = {
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
};
printObjectInTable(orderedObject, "table");
printObjectInTable(unorderedObject, "table");
printObjectInTable(toManyPropertiesObject, "table");
printObjectInTable(toFewPropertiesObject, "table");
function printObjectInTable(objectToIterate, tableID) {
var thChildren = document.getElementById(tableID).getElementsByTagName("th"),
childrenLength = thChildren.length,
tr = document.createElement("tr");
for (var i = 0; i < thChildren.length; i++) {
var th = thChildren[i];
// important check that this is objects own property
// not from prototype prop inherited
var td = document.createElement("td");
if (objectToIterate.hasOwnProperty(th.id)) {
td.appendChild(document.createTextNode(objectToIterate[th.id]));
}
tr.appendChild(td);
}
document.getElementById(tableID).getElementsByTagName("tbody")[0].appendChild(tr);
}
以下是如何在PHP中執行此操作:
$json=file_get_contents("http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)");
$data = json_decode($json);
//var_dump($data);
echo "<table>
<tr>
<td><strong>Data de publicação</strong></td>
<td><strong>Empresa Contratada</strong></td>
<td><strong>Empresa que Contratou</strong></td>
<td><strong>ID</strong></td>
<td><strong>Objecto adquirido</strong></td>
<td><strong>Preço Contratual</strong></td>
<td><strong>Data do Contrato</strong></td>
</tr>";
foreach($data as $object):?>
<tr>
<td><strong><?php echo $object->{'publicationDate'}?></strong></td>
<td><strong><?php echo $object->{'contracted'}?></strong></td>
<td><strong><?php echo $object->{'contracting'}?></strong></td>
<td><strong><?php echo $object->{'id'}?></strong></td>
<td><strong><?php echo $object->{'objectBriefDescription'}?></strong></td>
<td><strong><?php echo $object->{'initialContractualPrice'}?></strong></td>
<td><strong><?php echo $object->{'signingDate'}?></strong></td>
</tr>
<?php endforeach;
echo "</table>";
?>
每行轉換將起到這樣的作用(低級別而不做任何花哨的事情):
//with jdata as the object below
{
publicationDate: "28-02-2014",
contracted: "Servicash - Equipamentos Electrónicos, Lda.",
contracting: "Banco de Portugal",
id: 994738,
objectBriefDescription: "Consumíveis de papel para tratamento de dinheiro",
initialContractualPrice: "12.945,50 €",
signingDate: "28-02-2014"
}
function tablize(jdata) {
var trow = "<tr>";
trow += "<td>"+jdata.publicationData+"</td>";
trow += "<td>"+jdata.contracted+"</td>";
trow += "<td>"+jdata.contracting+"</td>";
trow += "<td>"+jdata.id+"</td>";
trow += "<td>"+jdata.objectBriefDescription+"</td>";
trow += "<td>"+jdata.initialContractualPrice+"</td>";
trow += "<td>"+jdata.signingDate+"</td>";
trow += "</tr>"
return trow;
}
現在你的問題是在表格中得到它。 假設你有能力(略微)調整表結構,我建議你這樣設置你的表:
<table>
<thead>
<tr>
<th><strong>Data de publicação</strong></th>
<th><strong>Empresa Contratada</strong></th>
<th><strong>Empresa que Contratou</strong></th>
<th><strong>ID</strong></th>
<th><strong>Objecto adquirido</strong></th>
<th><strong>Preço Contratual</strong></th>
<th><strong>Data do Contrato</strong></th>
</tr>
</thead>
<tbody id="cdata"></tbody>
</table>
然后,您只需要一個函數來迭代所有數據並添加累積的行,或者將新創建的行追加到元素:
假設您將此數據作為數組獲取:
function fillTable(contractarray) {
var tblrows = "";
if (contractarray) {
for (var i=0;i<contractarray.length;i++) {
tblrows += tablize(contractarray[i]);
}
}
//appropriate method to set the table body--using jQuery for brevity
$("#cdata").html(tblrows);
}
如果你沒有能力調整表格html,那么你必須以另一種方式“找到”DOM結構中的表格並重新創建整個事物(包括標題)或通過適當地清除/添加行來調整它。
無論哪種方式,tablize(jdata)函數都可以工作,第二個函數需要相應調整。
完全取消它(使用請求者提供的url:
var dataurl = "http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)";
$.getJSON(dataurl, function(data) { tablize(data)});
這將啟動請求,將數據傳遞給tablize()函數,並填充行。 一個很小(但很好)的副作用是,如果沒有返回數據,表格會清空,表明我們什么都沒有回來。
根據您的評論回答,您正在使用
$('table#tbl TBODY').append(
用於將數據附加到表中,
但你應該使用
$('table#tbl').append(
其他代碼很好
您還必須將代碼運行到Web服務器中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.