0

I've created a dynamic listview from database on my jQuery mobile #page1. That works all perfect. I can see a list with all items from my database (ID, name ...)

Now I want to pass the ID garm.ID from my database item to the next page, after I've clicked on a item in the listview.

My JavaScript:

$(document).on("pagecreate", "#page1",function(){ 
    $(function() {
        $.ajaxSetup( {
            "async": false
        } );
        $.getJSON("http://server/kas1.js", function(data) {
            for(var i = 0; i < data.length; i++) {
                var garm = data[i];
                $.getJSON("http://server/kas2.js", function(data) {
                    var no = data[0].no;
                    $("#content-page1").append(
                        "<ul data-role=\"listview\">" +
                        "<li><a href=\"#page2\">" +
                        "<h2>Hello</h2>" +
                        "<p>Status:"+data[0].no+"</p>" +
                        "</a></li>" +
                        "</ul><br>"
                        );
                });
            }
        });
        $.ajaxSetup( {
            "async": true
        } );
    });   
});

You can see the line: "<li><a href=\"#page2\">" +

With this link you will redirect to the next page and on the next page I want to use the passed ID.

2 Answers 2

2

Try this:

$(document).on("pagecreate", "#page1",function(){ 

    $(function() {
        $.ajaxSetup( { "async": false } );


        $.getJSON("http://server/kas1.js", function(data) {
            for(var i = 0; i < data.length; i++) {
                var garm = data[i];

                $.getJSON("http://server/kas2.js", function(data) {
                    var no = data[0].no;

                    $("#content-page1").append(
                        "<ul data-role=\"listview\">" +
                            "<li><a href='#page2?garm_id='" + garm.ID  + ">" +
                                "<h2>Hello</h2>" +
                                "<p>Status:"+data[0].no+"</p>" +
                            "</a></li>" +
                        "</ul><br>"
                    );
                });

            }
        });


        $.ajaxSetup( { "async": true } );
    });

});

On page two:

function queryStringParameters (keyValueString) {
    var query_string, whole_query_string, query_string_obj = {};
    if (typeof keyValueString === "undefined") {
        whole_query_string = window.location.search;
        if (whole_query_string[0] === "?") {
            query_string = whole_query_string.replace('?', '');
        } else {
            query_string = whole_query_string;
        }
    } else {
        query_string = keyValueString
    }
    var query_string_array = query_string.split('&');
    if (query_string !== '') {
        for (var i = 0; i < query_string_array.length; i++) {
            var query = query_string_array[i],
                query_array = query.split('=');
            query_string_obj[query_array[0]] = query_array[1] || '';
        }
        return query_string_obj;
    } else {
        return {};
    }
}


var hash = window.location.hash,
    queryString = hash.split('?')[1],
    garm = queryStringParameters(queryString),
    garm_id = garm.id;
Sign up to request clarification or add additional context in comments.

1 Comment

Could you give me an example, how I can receive the variable on page 2? :)
0

I hope this works:

'<li><a href="#page2,garmId='+ garm.ID +'">'

then, in page 2 you can (hopefully) parse the hashtag to get the id

Hope this helps, cheers

1 Comment

Some question here, could you give me an example too, how to receive the variable on page 2?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.