I have a web service which returns this JSON:
[{
"user_id": "1",
"user_name": "User1",
"user_age": "20"
}, {
"user_id": "2",
"user_name": "User2",
"user_age": "21"
}, {
"user_id": "3",
"user_name": "User3",
"user_age": "24"
}, {
"user_id": "4",
"user_name": "User4",
"user_age": "34"
}, {
"user_id": "5",
"user_name": "User5",
"user_age": "27"
}]
What I want is I want to populate it to some list and also want to get index of list item clicked so that I can retrieve more information according to that index value (user_id).
What I did is:
function fetchFunction() {
//fn.load('fetchPage.html');
console.log("fetchFunction is invoked");
var header = "<ons-list-header>My Inset ListTTT</ons-list-header>";
$.ajax({
type: 'POST',
url: "fetchJSON.php",
timeout: 5000,
//dataType:'text',
dataType:'json',
success: function(data) {
var data2 = JSON.stringify(data); //to string
//var data2 = JSON.parse(data); //to string
console.log("Index of list is: " + data2[0].user_id + " and username is: " + data2.user_name);
//alert("Index is: " + data[0]);
//$("#myListElement").html(header);
//$("#myListElement").append(data);
// Parse 'data' here
console.log("SuccessED");
},
error: function(err) {
ons.notification.alert("Error: " + err.status + ", " + err.statusText);
console.log("Error: " + err);
// console.log(err.status);
// console.log(err.statusText);
// $("#div1").text(err.responseText);
}
});
}
data2[0].user_id is doing nothing, just returning undefined.
I have a click function on that div called myListElement so I want to show the user_id on which the click function triggered.
if (event.target = "fetchPage2.html") {
$("#myListElement").click(function(){
alert("List Item Clicked!");
});
}
I will love if you give me some idea.
BTW I am using: Cordova + OnSenUI. :/
Thank You.
PS: I have this ons-list as a List. and want to populate it here.
<ons-list modifier="inset" id="myListElement">
<ons-list-header >My Inset listdc</ons-list-header>
<ons-list-item modifier="longdivider" tappable>Item A</ons-list-item>
<ons-list-item modifier="longdivider" tappable>Item B</ons-list-item>
<ons-list-item modifier="longdivider" tappable>Item C</ons-list-item>
</ons-list>
user_idI will send request back to server and will open new activity which will be having detail page of that user. But I know many people don't useOnSenUIso they may not be able to help me in list-itemons-list. That's why I generalise my question.