2015-03-31 66 views
-1

所以我想用一些客户信息建立一个管理页面,我有这个问题在那里我无法从客户选择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,然后将其写入页面以显示该客户端的所有详细信息。

+2

错误告诉你这一切,功能'findId'不在范围内 – 2015-03-31 18:32:55

+0

我回答你得到实际的错误,但另一个要考虑的输出数据像这样的'Array.prototype。地图“或”Array.prototype.forEach“。你的'aaData'是一个数组,映射是一种非常干净的方式,将数据映射为一堆数组映射到表中。 – 2015-03-31 18:37:01

+0

@Rob Scott如何解决这个问题?你能指导我吗?我忘了维度,但我是jquery的新手。 – celsomtrindade 2015-03-31 18:42:09

回答

1

你需要把你的代码放在$.getJSON的回调中(并且不需要函数findId)。否则,您需要实现回调函数(即,请求获取数据,然后异步方法在完成时调用外部函数)。

如果你只需要加载一个ID,然后像...

$(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(json_data) { 
     var categoryArray = json_data.aaData; 
     for (var i = 0; i < categoryArray.length; i++) { 
      if (categoryArray[i].cd == cli) { 
       writeItem(categoryArray[i]); //Pass the matching object to writeItem. 
       return; 
      } 
     } 
    }); 
}); 

function writeItem(item) { 
    //"item" has already been parsed to an object, it's no longer a JSON string. 
    var write += '<tr><td>Cod</td><td>' + item.cd + '</td></tr><tr><td>Name</td><td>' + item.nm_cliente + '</td></tr><tr><td>cpf</td><td>' + item.cpf + '</td></tr>'; 
    $('#mytable').html(write); 
} 

如果你需要写多行(多个ID,可能通过点击按钮),你需要更新每一个电话,类似的数据集...

var my_data = null; 

$(document).ready(function() { 
    var url = "data/c_fisico.json"; 

    //You should add error handling in here, so you know if the JSON file wasn't available. 
    $.getJSON(url, function(json_data) { 
     my_data = json_data.aaData; 
     $("#some_button").attr("disabled", false); //JSON data has loaded, so enable our button. 
    }); 

    $("#some_button").attr("disabled", true); //Disable our button so the user can't click it until JSON is loaded. 
    $("#some_button").on("click", function() { 
     var id = //Figure out what ID you want to load here. 

     writeItem(id); //Call the write function. 
    }); 
}); 

function findId(id) { 
    if (my_data === null) { return null; } //JSON hasn't been retrieved yet, so we can't find anything. 

    for (var i = 0; i < my_data.length; i++) { 
     if (my_data[i].cd == id) { 
      return my_data[i]; 
     } 
    } 

    return null; //The requested ID wasn't found. 
} 



function writeItem(id) { 
    var item = findId(id); 
    if (item === null) { 
     //The ID wasn't found or JSON isn't available. 
     //Show error message or whatever you'd like. 
     return; 
    } 

    var write = '<tr><td>Cod</td><td>' + item.cd + '</td></tr><tr><td>Name</td><td>' + item.nm_cliente + '</td></tr><tr><td>cpf</td><td>' + item.cpf + '</td></tr>'; 
    $('#mytable').append(write); //Appends to existing table data. 
} 

最后,如果你需要加载多个ID,而且从数据源时间,什么李重负载ke ...

var busy = false; 


$(document).ready(function() { 
    var url = "data/c_fisico.json"; 

    //You should add error handling in here, so you know if the JSON file wasn't available. 
    $.getJSON(url, function(json_data) { 
     my_data = json_data.aaData; 
     $("#some_button").attr("disabled", false); //JSON data has loaded, so enable our button. 
    }); 

    $("#some_button").on("click", function() { 
     var id = //Figure out what ID you want to load here. 

     findId(id, writeItem); //Call the find function and tell it to call the write function when done. 
    }); 
}); 


function findId(id, callback) { 
    if (busy) { return; } //Don't call again if we're already in the process of another call. 
    busy = true; 

    $.getJSON(url, function(json_data) { 
     var categories = json_data.aaData; 
     for (var i = 0; i < categories.length; i++) { 
      if (categories[i].cd == id) { 
       //Item was found, so pass the object to our callback. 
       callback(categories[i]); 
       busy = false; //You should also set busy = false if the getJSON call fails. 
       return; 
      } 
     } 

     //ID wasn't found. Error message. 
     busy = false; //You should also set busy = false if the getJSON call fails. 
    }); 
} 


function writeItem(item) { 
    //Gets called by findId as a callback function. 

    var write = '<tr><td>Cod</td><td>' + item.cd + '</td></tr><tr><td>Name</td><td>' + item.nm_cliente + '</td></tr><tr><td>cpf</td><td>' + item.cpf + '</td></tr>'; 
    $('#mytable').append(write); //Appends to existing table data. 
} 

请注意,这些示例中存在未处理的错误条件。我会留给你和你的实施!

+0

谢谢!这解决了我的问题= D另外,如果你可以提供其他内容来改善这一点,这将是很棒的,因为我是新来的jquery,等等。但是,谢谢你! – celsomtrindade 2015-03-31 19:13:02

2

你的问题是与你的功能范围:findId()传递给jQuery.getJSON(回调中定义),而不是你的$(document)。就绪()的回调中。

编辑:这应该是大致你想做什么:

$(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(data) { 
    var rows = ''; 
    data.aaData.forEach(function(aa) { 
     rows += '<tr><td>Code</td><td>' + aa.cd + '</td></tr>' + 
     '<tr><td>Name</td><td>' + aa.nm_cliente + '</td></tr>' + 
     '<tr><td>cpf</td><td>' + aa.cpf + '</td></tr>'; 
    }); 
    $('#mytable').html(rows); 
    }); 
}); 

我不是直接建立HTML这样的(一个巨大的风扇普遍喜欢ReactJS,或者只是建立DOM作为最坏的情况),但这就是你以前所做的,所以我保持同样的方法。

+0

这仍然不会解决异步问题 – charlietfl 2015-03-31 18:34:35

+0

@charlietfl它是整体的sl code代码--OP可以算出逻辑,我只是​​告诉他们这个错误是什么。 – 2015-03-31 18:38:01

+0

我明白你试图在那里解释我。但不是写所有客户端的数据信息,而是写一个客户端的信息。例如,如果我点击ID = 3的客户端'James',我只需要从ID为3的字符串中写入信息,而从其他客户端写入nto。我希望我现在可以更好地解释一下= D – celsomtrindade 2015-03-31 18:49:56

0

使用开源项目jinqJs http://www.jinqJs.com你可以做到这一点。

var result = jinqJs().from('http://....').where('id = ' + cli).select(); 
+0

我会看看它。感谢您的建议= D – celsomtrindade 2015-03-31 19:36:32