2012-05-07 9 views
1

私はようやくAJAXでデータを渡してJSONで返すことに成功しました。しかし、今私は自分のページにそれを表示する方法を知らない。ここJavaScriptでページにJSONデータを表示

は私のJSです:

$(function() { 
    $("#selectable").selectable({ 
     selected: updatefilters, 
     unselected: updatefilters 
    }); 
    function updatefilters(ev, ui){ 
     // get the selected filters 
     var $selected = $('#selectable').children('.ui-selected'); 
     // create a string that has each filter separated by a pipe ("|") 
     var filters = $selected.map(function(){return this.id;}).get().join("\|"); 
     $.ajax({ 
      type: "POST", 
      url: 'updatefilters', 
      dataType: 'json', 
      data: { filters: filters }, 
      success: function(data){ 
       $('#board').replaceWith(data.content); 
      } 
     }); 
    } 
}); 

私のモデルの実行後に放火犯に発見されて、それは次のようにエコー表示:

[{"thread_id":"226","owner_id":"1","subject":"trying to create a HUGE body for thread testi","body":"Firstly, I think 45 characters is perfect for the heading. \n\nHowever, once the body gets huge, I assume that the \"preview\" is going to get a bit out of hand if not truncated. I have truncated code in place...but it doesn't seem to be working.\n\nI'll have to keep testing to ensure we can get it shrunk down to a few hundred characters so as to not completely screw up the page format\n\nyadda yadda yadda...yadda yadda yadda...and more yadda yadda yadda...\n\nBabble Babble Babble Babble Babble \n\nBabble Babble Babble ","timestamp":"2012-05-02 15:29:19","replystamp":"2012-05-02 15:29:53","last_post_id":"1","slug":"226-trying-to-create-a-huge-body-for-thread-testi","replies_num":"1","views":"19"},{"thread_id":"219","owner_id":"3","subject":"testing icons","body":"hellow world","timestamp":"2012-05-01 11:37:08","replystamp":"2012-05-01 11:37:08","last_post_id":"3","slug":"219-testing-icons","replies_num":"0","views":"3"},{"thread_id":"212","owner_id":"1","subject":"This thread is based off entertainm","body":"Yay","timestamp":"2012-04-25 14:07:55","replystamp":"2012-04-25 14:07:55","last_post_id":"1","slug":"212-this-thread-is-based-off-entertainment","replies_num":"0","views":"4"}] 

データとDIV「ボード」の交換方法について何かアドバイスを返されたJSON配列から非常に役立つでしょう!

答えて

1

から値にアクセスすることができます。

success: function(data){ 
    var html = "<table>"; 
    for (i=0 ; i< data.length ; i++) 
    { 
     html += "<tr><td>" + data[i].subject + "</td><td>" + data[i].body + "</td></tr>"; 
    } 
    html += "</table"; 
    $('#board').html(html); 
} 

あなたはこのように書くことができます。より良いソリューションを検討する時間があればテンプレートライブラリmustache.jsを調べることをお勧めします。これにより、テンプレートをデータから分離し、Javascriptを使用してHTMLとデータの醜い連結を排除できます。 (より高度なソリューションの種類、しかし、あなたはちょうどそれが簡単で残しておきたいことがあります。)Mustache.jsエンジンは、あなたがこのように上記記述することができます:

var template = "{{#item}}<tr><td>{{subject}}</td><td>{{body}}</td></tr>{{/item}}"; 
var html = "<table>" + mustache.render(template, data) + "</table>"; 
$("#board").html(html); 
+0

私はorignally mySQLクエリから直接撮影したデータを表示するビューを持っていた。このデータを表示するためにそのビューを使用できる人はいますか?その多くのHTML ...とjavascriptでそれを置くことは非常に難しいです。 –

1
success: function(data){ 
$.each(data, function(index, value) { 
    $('#board').append(index + ': ' + value + '<br/>'); 
}); 
} 

また、直接あなたが件名と本文でテーブル内のデータを表示したいとしましょうJSON

success: function(data) { 
    $('#board').append('Thread Subject' + data.subject); 
} 
+0

感謝を!その最後に何かをしています。 0:[オブジェクトオブジェクト] 1:[オブジェクトオブジェクト] 2:[オブジェクトオブジェクト] –

+0

console.log(data)を成功に向けて、ここにfirebugによって何が印刷されて貼り付けられますか? –

+0

私は質問に投稿したものと同じものを表示すると信じています(2番目のコードポスト)。私は火かき棒に新しい、あなたが尋ねるものを達成する方法を確信していない –

関連する問題