2015-03-31 67 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