javascript - 通过搜索从 JSON 填充表

标签 javascript html json angularjs angularjs-ng-repeat

我能够纯粹从 JSON 对象实现网格 - AngularJS ng-repeat to populate grid from array .但是,由于添加索引的性质,能够使用 ng-modelfilter:search 创建搜索栏不起作用 - 它只能搜索每个表格行中的第一个。

var test= angular.module("app", []);

test.controller("appControl", function($scope, $http) {
	$http.get("http://www.w3schools.com/angular/customers.php")
		.success(function (response) {
			$scope.data = response.records;
		}
	);
    $scope.getFiltered= function(obj, idx){
       //Set a property on the item being repeated with its actual index
       //return true only for every 1st item in 5 items
       return !((obj._index = idx) % 5);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<body ng-app='app' ng-controller='appControl'>
    <input type='text' ng-model='search.Country' />
    <table>
        <tr ng-repeat="work in data | filter:getFiltered | filter:search">
            <td>{{work.Country}}</td>
            <td>{{data[work._index+1].Country}}</td>
            <td>{{data[work._index+2].Country}}</td>
            <td>{{data[work._index+3].Country}}</td>
            <td>{{data[work._index+4].Country}}</td>
        </tr>
    </table>
</body>

data 的长度可能会或不会导致表格看起来像一个完美的矩形。

我正在开发一个函数来拆分数组并在 JavaScript 本身中创建网格,但我仍然不确定如何通过搜索输入对其进行过滤。

第二次尝试(使用提到的功能,但还没有过滤器......):

var test= angular.module("app", []);

function createGrid(arr, width) {
    newArr = [];
    reps = Math.ceil(arr.length/width) * width;
    k = 0;
    for (var i = 0; i < reps/width; i++) {
        newArr[i] = [];
    }
    
    for (var i = 0; i < reps/width; i++) {
        for (var j = 0; j < width; j++) {
            (arr[k]) ? newArr[i][j] = arr[k] : newArr[i][j] = "";
            //console.log(i, j, arr[k]);
            k++;
        }
    }
    
    return newArr;
}

test.controller("appControl", function($scope, $http) {
    $scope.gridWidth = 4;
	$http.get("http://www.w3schools.com/angular/customers.php")
		.success(function (response) {
			$scope.data = createGrid(Object.keys(response.records).map(function(k) { return response.records[k] }), $scope.gridWidth);
		}
	);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<body ng-app='app' ng-controller='appControl'>
    <input type='text' ng-model='search.Country' />
    <table>
        <tr ng-repeat="row in data">
            <td ng-repeat='work in row'>
                {{ work.Country }}
            </td>
        </tr>
    </table>
</body>

最佳答案

你可以尝试这样的事情:

var test= angular.module("app", []);
  
test.controller("appControl", function($scope, $http) {

	$http.get("http://www.w3schools.com/angular/customers.php")
		.success(function (response) {
	         $scope.data = response.records;
                 $scope.filteredData= response.records;
		}
	);

     $scope.$watch('search', function () {

      var array=[];
      for(var i in $scope.data)
      {
          if($scope.search==undefined || $scope.search.length == 0 ||  ($scope.data[i].Country!=undefined&&$scope.data[i].Country.toUpperCase().startsWith($scope.search.toUpperCase()))){
             array.push($scope.data[i]);
          }
      }
      $scope.filteredData=array;
    });

    $scope.getFiltered= function(obj, idx){
       //Set a property on the item being repeated with its actual index
       //return true only for every 1st item in 3 items
       return !((obj._index = idx) % 5);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<body ng-app='app' ng-controller='appControl'>
    <input type='text' ng-model='search' />
    <table>
        <tr ng-repeat="work in filteredData | filter:getFiltered | filter:search">
            <td>{{work.Country}}</td>
            <td ng-show="filteredData[work._index+1]">{{filteredData[work._index+1].Country}}</td>
            <td ng-show="filteredData[work._index+2]">{{filteredData[work._index+2].Country}}</td>
            <td ng-show="filteredData[work._index+3]">{{filteredData[work._index+3].Country}}</td>
            <td ng-show="filteredData[work._index+4]">{{filteredData[work._index+4].Country}}</td>
        </tr>
    </table>
</body>

关于javascript - 通过搜索从 JSON 填充表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907075/

相关文章:

javascript - 如果没有用户生成的内容,网站是否可以免受 XSS 攻击?

html - 如何将 background-size 设置为 "cover",再加上 CSS 中的更多内容?

javascript - jquery.nav.js 一点击后不起作用

java - 在 Android 中持久存储数组而不使用 SQLite 的最佳方法

php - 如何从 youtube google api 访问 youtube json 日期?

javascript - Vue js在单击按钮时动态添加文本框时绑定(bind)动态id

javascript - Angular 2属性绑定(bind): how do EventEmitters work?

jquery - 如何在 jqueryMobile 中正确创建类似于 iOS tabbarController 的 tabbar Controller ?

javascript - 如何在同一页面上创建多个谷歌地图(不是标记)?

javascript - 需要帮助编辑响应式菜单的 JavaScript 文件