所以我想用一些客戶信息建立一個管理頁面,我有這個問題在那裏我無法從客戶選擇ID
顯示他的所有信息一個單獨的頁面。無法從JSON字符串中選擇特定ID
基本上,在第一頁site.com/client
我有一個小桌子,所有的客戶,當我點擊按鈕來形象化,我會去頁面site.com/det_client&cliId=3
,我有所有的信息。
這個信息是在JSON
文件和每個客戶都有一個唯一的ID
。這個ID
正在從第一頁發送到第二個URL,我得到這個數據與jquery
。直到這部分沒有問題。我可以做,也可以從客戶端獲得正確的ID
。
問題開始時我需要編寫新表與特定ID
的所有信息。我試圖在這裏看(在stackoverflow),但我找到的解決方案是給我錯誤。這是我使用的代碼:
$(document).ready(function() {
var url = "data/c_fisico.json";
var cli; //getting the client id from the url. rest of the code is in other page
$.getJSON(url, function() {
function findId(url, idToLookFor) {
var categoryArray = url.aaData;
for (var i = 0; i < categoryArray.length; i++) {
if (categoryArray[i].cd == idToLookFor) {
return(categoryArray[i].id);
}
}
}
});
var item = findId(url, cli),
table = JSON.parse(item);
//there is more data, but i made it smaller
$.getJSON(table, function (response){
var write;
$.each (response, function (index, table) {
write += '<tr><td>Cod</td><td>' + table.id + '</td></tr><tr><td>Name</td><td>' + table.nm_cliente + '</td></tr><tr><td>cpf</td><td>' + table.cpf + '</td></tr>';
}); //end each
$('#TableTest').html(write);
}); //end getJSON
});
我有錯誤是:Uncaught ReferenceError: findId is not defined
這是我的JSON的例子:
{
"aaData": [
{
"id":0,
"cd":"C-0001",
"nm_cliente":"John",
"cpf":"000.111.222-33",
"nm_pai":"Nome Completo",
"nm_mae":"Nome Completo",
"tel":"00-9966-6699",
"cidade":"city",
"estado":"estate"
},
//rest of my code here...
]
}
所以,基本上,我需要的是從客戶端獲取所有信息,這些信息由URL提供相同的ID,然後將其寫入頁面以顯示該客戶端的所有詳細信息。
錯誤告訴你這一切,功能'findId'不在範圍內 – 2015-03-31 18:32:55
我回答你得到實際的錯誤,但另一個要考慮的輸出數據像這樣的'Array.prototype。地圖「或」Array.prototype.forEach「。你的'aaData'是一個數組,映射是一種非常乾淨的方式,將數據映射爲一堆數組映射到表中。 – 2015-03-31 18:37:01
@Rob Scott如何解決這個問題?你能指導我嗎?我忘了維度,但我是jquery的新手。 – celsomtrindade 2015-03-31 18:42:09