2

在我的应用程序listView('refresh')中不起作用。这是我的代码

我动态创建 listView

 var str = "<ul data-role='listview' data-inset='true' id='mylist'>";

        for(var i = 0; i<data.length; i++ ){
            str += "<li>"+data[i].note.text+"</li>";
        }
       str += "</ul>"

        $('#content').append(str);

function addnote(){
    var note_text = $('#note_text').val();
    var note_lat  = $('#lat').val();
    var note_lng  = $('#lng').val();

    $.ajax({

        type: "POST",
        beforeSend: function (jqXHR) {
            jqXHR.setRequestHeader(KEY1, _key1);
            jqXHR.setRequestHeader(KEY2, _key2);
        },

        url:SERVER_URL+"api/addNotes/",



        data: {type: 'text',note_text: note_text, note_lat: note_lat , note_lng: note_lng},

        success: function(data, textStatus, jqXHR) {
            if (data.status == "ok"){

                $.mobile.changePage("file:///android_asset/www/index.html?"+_key1+"|"+_key2+"|");


             }
            else{
                alert("Something wrong");
            }

        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert("Error=" + errorThrown);
        },

        complete: function() {
            $('#mylist').listview('refresh');

        }
    });
}

我在那里阅读了许多报告和论坛,说我必须在 ajax 的完整功能中调用 listview('refresh') 。但是在我的代码中它不起作用,谁能告诉我这里有什么问题?

4

4 回答 4

2

刷新适用于将元素添加到现有的增强列表视图。如果您要动态创建整个列表视图,则需要在父 div 上触发“创建”。

所以如果你有<div id="container><ul></ul></div>,你需要打电话$("#container").trigger("create")

于 2012-06-01T15:05:34.947 回答
0

试试这个为你完成功能

complete: function() {
    $('#mylist').listview();
}

正如其他人提到的,刷新方法适用于将列表项添加到 JQM 已经创建的列表视图中。

于 2012-06-01T19:53:32.463 回答
0

You probably want to see your data after adding some values. If you want to do this listview.refresh doesn't help you after adding some values you have to call you data again, i.e you have to call getNote() again.

I think it helps.

于 2012-06-04T13:35:38.177 回答
0

First of all , You must understand how does the refresh work, it will just refresh the listview's css when you add a new LI so that the new LI will have a right css,

function createItem(tx,results){
var len = results.rows.length;
console.log("lisitem len "+len);
for(var i = 0;i<len;i++){
    var content = results.rows.item(i).content;
    var id = results.rows.item(i).id;
    var string = '<li><a href="#all_content" data-rel="popup" onclick="getContent('+id+');" class="ui-link-inherit">'+content+' ['+results.rows.item(i).date+']</a></li>';
    $("#all_list").append(string);
};
freshList("all_list");

}

freshList("all_list"); it will refresh the listview ,if you don't do it, all of the LI will display like below.

<li><a href="#all_content" data-rel="popup" onclick="getContent(2);" class="ui-link-inherit">           测试成功         [2013-10-02]</a></li>

the LI doesn't have a css description

that's why your ajax operation doesn't work

于 2013-10-02T09:32:11.393 回答