动态地将元素附加到jQuery Mobile ListView

时间:2011-10-31 23:55:31

标签: jquery ajax json jquery-mobile

我想动态地将通过JSOn格式的网址收到的数据附加到我的列表视图中。但我无法弄清楚它是如何运作的。

移动网站按以下格式检索对象:

[
    {"id":1, "start":"2011-10-29T13:15:00.000+10:00", "end":"2011-10-29T14:15:00.000+10:00", "title":"Meeting"}
]

在.html中,我有一个列表视图和一个函数,我尝试附加接收到的数据。我只展示身体。

<body>
       <div>   
            <ul id="listview">
                <script>$.getJSON("url",
                function(data){
                    $.each(data, function(i,data){
                        i.title.appendTo("#listview");
                    });});</script> 
            </ul>
        </div>
</body>

可能这很容易,但我是网络编程的新手,我无法弄清楚我应该如何追加检索到的数据。

有人可以帮帮我吗?

5 个答案:

答案 0 :(得分:20)

//make AJAX call to url
$.getJSON("url", function(data){

    //declare a variable with which to build our output (it's best to buffer output and only do one append at the end since DOM manipulation is CPU expensive)
    var output = '';

    //iterate through the data (we could also get rid of the jQuery here by using `for (key in data) {
    $.each(data, function(index, value){

        //add each value to the output buffer (we also have access to the other properties of this object: id, start, and end)
        output += '<li>' + value.title + '</li>';
    });

    //now append the buffered output to the listview and either refresh the listview or create it (meaning have jQuery Mobile style the list)
    $('#listview').append(output).listview('refresh');//or if the listview has yet to be initialized, use `.trigger('create');` instead of `.listview('refresh');`
});

以上是上述解决方案的一个方面(还有一个使用for(){}代替$.each()的示例):http://jsfiddle.net/VqULm/

答案 1 :(得分:1)

我正在追加这样..它正在努力追加.. 它可能对您或其他人有帮助:))

          $.each(data.values, function(key, value) {

          $('#activity_contacts').append('<li id="activity_contacts" data-id="' + value.contact_id + '">' + value.display_name + '</li>');
          $("#activity_contacts").listview("refresh");


          });

我的整个自动完成功能如下:

    function contactSearchForActivities (q) {
    $.mobile.showPageLoadingMsg( 'Searching' );
    $().crmAPI ('Contact','get',
      {'version' :'3', 'activity_sort_name': q, 'return' : 'display_name' },
      {
        ajaxURL: crmajaxURL,
        success:function (data){
          if (data.count == 0) {
            cmd = null;
          }
          else {
            cmd = "refresh";
            $('#activity_contacts').show();
            $('#activity_contacts').empty();
          }
          //console.log(data);

          //loop to go through the values in order to display them in a li as a list


          $.each(data.values, function(key, value) {

          $('#activity_contacts').append('<li id="' + value.contact_id + '" title = "' + value.display_name +'">' + value.display_name + '</li>');

   }); 

   $("#activity_contacts li").click(function() {

   $('#activity_sort_name').val(this.title);
   $('#activity_hidden_id').val(this.id);
           $("#activity_contacts").empty();
   });

          $.mobile.hidePageLoadingMsg( );
          $('#activity_contacts').listview(cmd);
        }
      });
  } 

答案 2 :(得分:0)

我认为您可能遇到的问题是返回的JSON是一个包含在数组中的对象。要解析,你必须先遍历数组:

for( var x = 0; x < data.length; x++ ) {
 var i = data[ x ];
 i.title.appendTo("#listview");
}

答案 3 :(得分:0)

请在添加或删除后刷新列表视图。 除非你刷新,否则无法看到。

$('#listview').append(output).listview('refresh');

答案 4 :(得分:0)

如果您正在尝试重新创建列表视图,那么您需要.trigger(创建)和.listview(刷新)列表。问题在http://liljosh.com/jquery-mobile-dynamicly-appending-to-listview-via-ajax-issue/

解释