[Solved] Plugin works fine in HTML not after JSON response

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 4643 Views - Last Post: 18 December 2013 - 03:39 AM

#16 Keylogger   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 353
  • Joined: 14-February 11

Re: [Solved] Plugin works fine in HTML not after JSON response

Posted 18 December 2013 - 02:13 AM

Hey sorry to answer only now.
@andrew, yes without any problem. And btw, this is a common problem with all plugins..it might be a bug (?).
@Laytonsdad, the code its almost the same I posted in the beginning of topic, but with php code (that I've post as well). I've just created that zip so people won't need to copy paste the code, they just need to extract to the xampp/wamp folder. Also it has already the .js files (plus the plugin .js) and css files.

But the code is this (HTML):
<html>
	<head>
		<link rel="stylesheet" href="css/jquery.mobile-1.3.2.css" />
		<link type="text/css" rel="stylesheet" href="js/raty/doc/css/stylesheet.css"/>
		<script src="js/jquery-1.9.1.min.js"></script>
		<script src="js/jquery.mobile-1.3.2.min.js"></script>
		<script type="text/javascript" src="js/raty/jquery.raty.js"></script>
	</head>
	<body>
		<div data-role="page" id="main">
			<div data-role="content" id="content">
				<ul data-role="listview" id="listviewA" data-inset="true" data-mini="true" style="margin-top: 0px">
					<li style='height: 55px'>
						<h2 style='margin-top: 0px; padding-top: 0px'>DEFAULT</h2>
						<p class='ui-li-aside'><strong style='font-size: 17px; color: #CC4E4B'>DEFAULT</strong>
							<br/>
							<div id="score" style="float: right; margin-top: 0px"></div>
						</p>
					</li>
				</ul>
			</div>
		</div>
		<script type="text/javascript">
			$(function(){
				var info = "";
				$.getJSON('info.php', function(data){
					$.each(data, function(index, item){
						info += "<li style='height: 55px'><h2 style='margin-top: 0px; padding-top: 0px'>" + item.id + "</h2>"+
						"<p class='ui-li-aside'><strong style='font-size: 17px; color: #CC4E4B'>" + item.product + "</strong>"+
						"<br/><div id='score' style='float: right; margin-top: 0px'></div></p></li>";
					});
					$("#listviewA").append(info); 
					$("#listviewA").trigger("change");
					$("#listviewA").listview("refresh");
				});
			});
			$('#score').raty({
				score: function() {
					return $(this).attr('data-rating');
				}
			});
		</script>
	</body>	


PHP:
<?php 
	$arr = array();
	for($i = 0; $i <= 3; $i++){
		$arr[] =  array("id" => $i, "product" => "test");
	}
	echo json_encode($arr);
?>


Edit: To make it clear, I also tried to put this code:
$('#score').raty({
	score: function() {
		return $(this).attr('data-rating');
	}
});

In many different places than its in this code, such as inside the $(function(), below the listviewA.refresh(), etc, without any results.

This post has been edited by Keylogger: 18 December 2013 - 02:19 AM

Was This Post Helpful? 0
  • +
  • -

#17 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: [Solved] Plugin works fine in HTML not after JSON response

Posted 18 December 2013 - 02:34 AM

Quote

And btw, this is a common problem with all plugins..it might be a bug (?).

Ahem, it is unfortunate that all the plugins you try seem to have this bug :dontgetit: :whistling:

$('#score').raty({

This code runs as the page is loading and applies raty() to the single div-element with the id of score.

When the json data is returned you construct a number of divs with this same id of score. Ids must be unique on the page. However, raty() is not attempted to be added to any of the newly inserted elements.

BTW I haven't installed your files because these errors can be seen in the above code.
Was This Post Helpful? 1
  • +
  • -

#18 Keylogger   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 353
  • Joined: 14-February 11

Re: [Solved] Plugin works fine in HTML not after JSON response

Posted 18 December 2013 - 02:45 AM

But @andrew, as you can see in my previous posts I also have tried to put unique ID's and the problem persists.

$(function(){
	var info = "";
	$.getJSON('info.php', function(data){
		$.each(data, function(index, item){
			info += "<li style='height: 55px'><h2 style='margin-top: 0px; padding-top: 0px'>" + item.id + "</h2>"+
			"<p class='ui-li-aside'><strong style='font-size: 17px; color: #CC4E4B'>" + item.product + "</strong>"+
			"<br/><div id='score" + item.id + "' style='float: right; margin-top: 0px'></div></p></li>";
		});
		$("#listviewA").append(info); 
		$("#listviewA").trigger("change");
		$("#listviewA").listview("refresh");
	});
	$('#score1').raty({
		score: function() {
			return $(this).attr('data-rating');
		}
	});
});


I only tried to put the raty{} in the #score1 div (which is created in that loop), nothing happens. Also, you can see the div.score1 in google chrome console (in the HTML section), created.
Was This Post Helpful? 0
  • +
  • -

#19 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: [Solved] Plugin works fine in HTML not after JSON response

Posted 18 December 2013 - 02:56 AM

Yes, but you are still running raty() as the page is loaded, and score1 doesn't yet exist. As I mentioned earlier, you need to apply raty() after these new elements have been inserted in the DOM. Move raty() up.. inside the getJSON call.
Was This Post Helpful? 1
  • +
  • -

#20 Keylogger   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 353
  • Joined: 14-February 11

Re: [Solved] Plugin works fine in HTML not after JSON response

Posted 18 December 2013 - 03:05 AM

Trust me, I have tried to put that raty in all imaginary places. Also, in my first post, the raty is inside the DOM(), in this case inside the getJSON call. But I've tried again, without success:
$(function(){
	var info = "";
	$.getJSON('info.php', function(data){
		$.each(data, function(index, item){
			info += "<li style='height: 55px'><h2 style='margin-top: 0px; padding-top: 0px'>" + item.id + "</h2>"+
			"<p class='ui-li-aside'><strong style='font-size: 17px; color: #CC4E4B'>" + item.product + "</strong>"+
			"<br/><div id='score" + item.id + "' style='float: right; margin-top: 0px'></div></p></li>";
			
			$('#score1').raty({
				score: function() {
					return $(this).attr('data-rating');
				}
			});
		});
		$("#listviewA").append(info); 
		$("#listviewA").trigger("change");
		$("#listviewA").listview("refresh");
	});
});

I get no errors in console.
Was This Post Helpful? 0
  • +
  • -

#21 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: [Solved] Plugin works fine in HTML not after JSON response

Posted 18 December 2013 - 03:19 AM

You have now moved it into the each() but, as I said before, the raty() needs to occur AFTER the new elements have been APPENDED to the DOM.

Quote

I have tried to put that raty in all imaginary places.

To be honest, this gives the strong impression that you are just moving code randomly around, rather than trying to understand why it doesn't work and, from this, to know where the code needs to go.

Elements must be present on the page before you can refer to them and apply raty() to them.

[It is possible to use jQuery on() and event delegation, to attach events to elements that aren't yet in the DOM. However, this is a different approach, and isn't necessary. You just need to apply raty() after inserting the new elements.]

This post has been edited by andrewsw: 18 December 2013 - 03:21 AM

Was This Post Helpful? 1
  • +
  • -

#22 Keylogger   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 353
  • Joined: 14-February 11

Re: [Solved] Plugin works fine in HTML not after JSON response

Posted 18 December 2013 - 03:28 AM

Ok, you're right. The problem was that I put inside the DOM, inside the getJSON call but not after the listview. Although I can almost swear that I've tried this option earlier..but ok. Tell me, since it has unique ID's I must loop now..this is what I've tried and works, if there's any other option let me know please.

$(function(){
	var info = "";
	var all_info = [];
	$.getJSON('info.php', function(data){
		$.each(data, function(index, item){
			info += "<li style='height: 55px'><h2 style='margin-top: 0px; padding-top: 0px'>" + item.id + "</h2>"+
			"<p class='ui-li-aside'><strong style='font-size: 17px; color: #CC4E4B'>" + item.product + "</strong>"+
			"<br/><div id='score" + item.id + "' style='float: right; margin-top: 0px'></div></p></li>";
	
			all_info[index] = item.id;
		});
		$("#listviewA").append(info); 
		$("#listviewA").trigger("change");
		$("#listviewA").listview("refresh");
		
		for(var i = 0; i <= all_info.length - 1; i++){
			$('#score' + all_info[i]).raty({
				score: function() {
					return $(this).attr('data-rating');
				}
			});
		}
	});
});


Thank you very much!
Was This Post Helpful? 0
  • +
  • -

#23 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: [Solved] Plugin works fine in HTML not after JSON response

Posted 18 December 2013 - 03:39 AM

Got there in the end! ;)

The page for raty has an example that applies to all elements of a particular class-name.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2