dongtun3328 2013-12-26 10:22
浏览 65
已采纳

使用knock out js不显示从ajax请求滚动获得的数据

I have used a code that I got from a site for infinite scrolling using knockout js, with some changes in it.

Here's my html and javascript code:

<div id="main" data-bind="foreach: items, event: { scroll: scrolled }">
    <div data-bind="text: name"></div>
</div>

<script type="text/javascript">
       var viewModel = {
            items: ko.observableArray([]),
            scrolled: function(data, event) {
                var elem = event.target;
                if (elem.scrollTop > (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 < 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);

    </script>

And here is the php file code from where I am getting the data:

<?php
   include 'dbconfig.php';
   $jsonarr=json_decode($_POST['json'],true);
   $cnt=$_POST['cnt'];
   if(isset($_POST['id'])){
       $offset=$_POST['id'];
   }
   if($offset<=$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);
  ?>

On scroll, I get the correct number of div added but the data in items array is not displayed in the div with id main.

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.

Please help me fix this.

  • 写回答

1条回答 默认 最新

  • dongzanghua8422 2013-12-26 10:37
    关注

    Your server side logic is incorrect. You are sending back an array of strings and not an array of objects. So when you get the data on the client side your items does not have a name property.

    So you need to add objects in your $json array which has the correct property names, there are multiple ways to do it (use proper classes or use anonymous types) , here is one example:

    $json[]= (object)array('name' => $name, 'profilepic' => $profilepic, 'city' => $city);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题