Note that there are some explanatory texts on larger screens.

plurals
  1. POData obtained on scroll from ajax request is not displayed using knock out js
    text
    copied!<p>I have used a code that I got from a site for infinite scrolling using knockout js, with some changes in it.</p> <p>Here's my html and javascript code:</p> <pre><code>&lt;div id="main" data-bind="foreach: items, event: { scroll: scrolled }"&gt; &lt;div data-bind="text: name"&gt;&lt;/div&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; var viewModel = { items: ko.observableArray([]), scrolled: function(data, event) { var elem = event.target; if (elem.scrollTop &gt; (elem.scrollHeight - elem.offsetHeight - 200)) { getItems(6); } }, maxId: 0, pendingRequest: ko.observable(false) }; function getItems(cnt) { if (!viewModel.pendingRequest()) { var entries = []; for (var i = 0; i &lt; cnt; i++) { var id = viewModel.maxId++; entries.push({ id: id }); } viewModel.pendingRequest(true); $.ajax({ type: 'POST', url: 'echojson.php', data: { json: ko.toJSON(entries), delay: .1, id:id, cnt:cnt }, success: function(entries) { ko.utils.arrayForEach(entries, function(entry) { alert(entry); viewModel.items.push(entry); }); viewModel.pendingRequest(false); }, error: function() { viewModel.pendingRequest(false); }, dataType: 'json' }); } } ko.applyBindings(viewModel); getItems(6); &lt;/script&gt; </code></pre> <p>And here is the php file code from where I am getting the data:</p> <pre><code>&lt;?php include 'dbconfig.php'; $jsonarr=json_decode($_POST['json'],true); $cnt=$_POST['cnt']; if(isset($_POST['id'])){ $offset=$_POST['id']; } if($offset&lt;=$cnt){ $offset=0; } else{ $offset=$offset-($cnt-1); } $json=array(); $sql="SELECT * FROM user LIMIT $offset,".$cnt; $exec=mysqli_query($con,$sql); while($row=mysqli_fetch_array($exec)){ $name=strtoupper($row['fname'].' '.$row['lname']); $profilepic=$row['profilepic']; $city=$row['city']; $json[]=$name.' '.$profilepic.' '.$city; } echo json_encode($json); ?&gt; </code></pre> <p>On scroll, I get the correct number of div added but the data in <code>items</code> array is not displayed in the div with id <code>main</code>. </p> <p>When I alert elements in array items, I do get the values in it. But this updated array is not getting binded to the div it seems.</p> <p>Please help me fix this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload