将元素动态附加到 jQuery Mobile ListView [英] Dynamically Appending Elements to jQuery Mobile ListView

查看:31
本文介绍了将元素动态附加到 jQuery Mobile ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将通过 JSOn 格式的 url 接收到的数据动态附加到我的列表视图中.但我不知道它是如何工作的.

I want to dynamically append data received via an url in JSOn format to my listview. But i can't figure out how it works.

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

The mobile website retrieve the object in the following format:

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

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

In the .html i have one listview and a function, where i try to append the received data. I show only the body.

可能很容易,但我是网络编程的新手,我不知道应该如何附加检索到的数据.

Probably it's very easy, but i'm new to web programming and i can't figure out how that i should append the retrieved data.

有人可以帮我吗?

推荐答案

//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');`
});

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

Here is a jsfiddle of the above solution (there is also an example of using for(){} instead of $.each()): http://jsfiddle.net/VqULm/

这篇关于将元素动态附加到 jQuery Mobile ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆