Search code examples
jsonlistviewjquery-mobilegeturl

Pass parameter to another page to generate listview from Json


Hello I'm using this js to pass the url parameter and it's working just fine, but my problem is that when I define the path to the JSON file I don't want to use the id of the item...I want to use another Id. For example: I have the following item:

 {"id":"1",
"name":"Winery",
"street":"Chile",
"number":"898",
"phone":"4204040",
"mail":"[email protected]",
"web":"www.winery.com",
"lat":"-32.891638",
"long":"-68.846522",
"id_localidad":"1",
"id_provincia":"1"}

I want to put id_localidad at the end of the path, to generate the listview depending on the city (id_localidad is the id of the city where the shop is), not the id of the item. And this is not working for me.

Thanks in advance!

JS FILE

 $('#PuntosDeVenta').live('pageshow',function(event){


 var id =  getUrlVars()["id"];


 $.getJSON('http://localhost/CavaOnline/json_PuntosDeVentas.php?id_localidad='+id,  function(vinerias) {
 //THIS IS NOT WORKING, IS THE SAME AS PUTTING id, not id_localidad

 $.each(vinerias, function(index, vineria) {

    $('#listviewVinerias').append( '<li><a href="FichaTecnicaVineria.php?id=' + vineria[id - 1].id + '" > ' +
                '<img src="pics/' + vineria[id - 1].img_url1 + '"/>' +
                '<h4>' + vineria[id - 1].name+'</h4>' +
                '<p>' + vineria[id - 1].street+ ' ' + vineria[id - 1].number+ '</p>' +
                '</a></li>');

    $('#listviewVinerias').listview('refresh') 

  });

 });

});

function getUrlVars() {

var vars = [], hash;

var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

for(var i = 0; i < hashes.length; i++)

{

    hash = hashes[i].split('=');

    vars.push(hash[0]);

    vars[hash[0]] = hash[1];

}

return vars;

}

Div where I load the List

 <div data-role="content">

 <ul id="listviewVinerias" data-role="listview"></ul>

 </div>

Solution

  • So I'm assuming your vinerias is a variable containing a list of JSON objects, even though I don't know why you are calling [id-1] everywhere. If so, you can use the .filter() function to filter out the elements that have an id_localidad equal to the one specified.

    var filteredVinerias = vinerias.filter(function(index){
        return this["id_localidad"] === "1" //The localidad you want
    });