所以我想用一些客户信息建立一个管理页面,我有这个问题在那里我无法从客户选择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