doujiongqin0687 2014-01-19 19:20
浏览 19
已采纳

角度搜索过滤器不起作用(PHP返回的数据)

I dont get the search filter working.

projects template:

<h3>Projekte</h3>
Search: <input ng-model="searchText">
<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Title</th>
            <th>Long Title</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="(key, project) in projects | filter:searchText">
            <td>{{key}}</td>
            <td>{{project.title}}</td>
            <td>{{project.longTitle}}</td>
            <td><button class="btn btn-default" ng-click="edit()"><i class="fa fa-pencil"></i></button></td>
        </tr>
    </tbody>
</table>

controller:

var secProjects = function ($http,$scope, $modal) {

    $http.post('php/data.php').
    success(function(data) {
        $scope.projects = data;
    })

}

php:

while($row = mysql_fetch_object($result)){
        $data[$row->ID] = array ('id'=>$row->ID,'title'=>$row->Title,'longTitle'=>$row->TitleLong, 'textDE'=>$row->DescriptionDE,'start'=>$row->DateStart,'end'=>$row->DateEnd);
    }
    echo json_encode($data);

i hope you can help me :( I think the reason ist the way i get the data in my scope. but iam not sure.

EDIT: Change the PHP solved the Problem:

   $data = array();
   while($row = mysql_fetch_object($result)){
      array_push($data,['id'=>$row->ID,'title'=>$row->Title,'longTitle'=>$row->TitleLong]);
   }
   echo json_encode($data);
  • 写回答

2条回答 默认 最新

  • dtsc1684 2014-01-19 21:07
    关注

    The problem is that your backend returns the data as an object instead of an array.
    But the filter works only on arrays (according to the docs):

    Selects a subset of items from array and returns it as a new array.


    So, you have two options:

    1.
    Before assigning the data to your scope, convert them from object to array. E.g.:

    $http.post(...).success(function (data) {
        $scope.projects = [];
        for (var key in data) {
            $scope.projects.push(data[key]);
        }
    });
    

    2.
    Have your backend return the data as a JSONified array, instead of an object.
    The reason PHP is interpreting your $data "array" as an object is that the indices are not sequential (starting from 0). I.e. instead of having indices 0, 1, 2..., you had 1,2,3.... (See, this answer for more info.)
    To fix this, you can change your code, like this:

    $data[] = array (...
    

    See, also, this short demo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用