【问题标题】:angularj ui grid searchingangularjs ui网格搜索
【发布时间】:2013-09-14 06:34:22
【问题描述】:

我正在寻找有关如何实现用于搜索的简单输入文本的教程或示例 在网格中。 我的尝试(但 ng-keyup 需要 angularjs > 1.1.3 并且我有 1.0.7)

<input type="text" ng-keyup="mySearch()" ng-model="searchText">
$scope.getPagedDataAsync = function (pageSize, page, searchText) {
                    setTimeout(function () {
                        var data;
                        if (searchText) {
                            var ft = searchText.toLowerCase();
                            $http.get('largeLoad.json?q='+encodeURIComponent(ft)).success(function (largeLoad) {        
                                $scope.setPagingData(largeLoad,page,pageSize);
                            });            
                        } else {
                            $http.get('largeLoad.json').success(function (largeLoad) {
                                $scope.setPagingData(largeLoad,page,pageSize);
                            });
                        }
                    }, 100);
                };
$scope.mySearch = function(){
                    console.log($scope.searchText);
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage,$scope.searchText);
                }

再见

NB 这是一个针对 json 文件的虚假请求,只是为了制作示例。

更新:我正在使用 ng-grid-1.3.2

【问题讨论】:

  • 您是否使用 ng-grid 最好指定您使用的网格版本,像行聚合这样的事情随着时间的推移发生了相当大的变化。我将使用 ng-grid 展示我当前的实现。
  • 我已经用版本更新了我的问题。
  • 提供的解决方案是否由于某种原因不起作用?

标签: javascript angularjs angular-ui


【解决方案1】:

基本上要解决这个问题,我认为您可以使用类似于我在下面所做的解决方案,我只是在观察模型的属性以进行更改并触发一个函数来对该数据集进行过滤点。

文本输入的 HTML

<input type="text" placeholder="Type to filter" ng-model="gardenModel.externalFilterText"/>

过滤数据集的 JavaScript(还包括我在服务上监视的部分,以首先更新数据或刷新数据以重新应用过滤器)。

//This function is called every time the data is updated from the service or the filter text changes
$scope.filterGardens = function(filterText) {
  //Basically everything in this function is custom filtering specific
  //to the data set I was looking at if you want something closer to the
  //real implementation you'll probably have to dig through the source (I believe they separated the search filter code into it's own file in the original project)

  //Creating a temporary array so changes don't cause a bunch of firing of watchers
  var tempToShow = [];

  //doing case insensitive search so lower case the filter text
  filterText = filterText.toLowerCase();

  //If the filter text is blank just use the whole data set
  if(!filterText || filterText == "")
  {
    $scope.gardenModel.shownGardens = $scope.gardenModel.gardens;
    return;
  }

  //step through each entry in the main list and add any gardens that match
  for (var i = 0; i < $scope.gardenModel.gardens.length; i++) {
    var curEntry = $scope.gardenModel.gardens[i];
    var curGarden = curEntry.curGarden;


    if(curGarden["Garden Name"] && curGarden["Garden Name"].answer.toString().toLowerCase().indexOf(filterText)!=-1)
      tempToShow.push(curEntry);
    else if(curGarden["Address"] && curGarden["Address"].answer.toString().toLowerCase().indexOf(filterText)!=-1)
      tempToShow.push(curEntry);
    else if(curGarden["Ownership"] && curGarden["Ownership"].answer.toString().toLowerCase().indexOf(filterText)!=-1)
      tempToShow.push(curEntry);
    else if(curGarden.gardenId && curGarden.gardenId == filterText)
      tempToShow.push(curEntry);
  };
  $scope.gardenModel.shownGardens = tempToShow;
}

//Watch for any changes to the filter text (this is bound to the input in the HTML)
$scope.$watch('gardenModel.externalFilterText', function(value) {
  $scope.filterGardens(value);
});

//Watch for any changes on the service (this way if addition/edit are made and
//refresh happens in the service things stay up to date in this view, and the filter stays)
$scope.$watch( function () { return gardenService.gardens; }, function ( gardens ) {
  $scope.gardenModel.gardens = gardens;
  $scope.filterGardens($scope.gardenModel.externalFilterText);
});

编辑稍微清理一下代码格式并添加了一些 cmets。

【讨论】:

  • 谢谢,myData 是 $scope.gardenModel.gardens 的问题?
  • 不好意思又打扰你了,你可以花两个字:) about $scope.$watch( function () { return gardenService.gardens; }, function ( gardens ) { // 在这里处理。例如: $scope.gardenModel.gardens = 花园; $scope.filterGardens($scope.gardenModel.externalFilterText); });再次感谢您的时间。
  • 是的,我创建了一个 gardenModel 对象,我在其中存储了该控制器的所有内容,花园只是所有花园的数组,然后显示的花园是我用于我的数据的网格。监视表达式是这样的,如果存储在服务中的数据发生变化,那么视图中的数据也会更新。这样我就不需要知道是什么触发了更改,只是因为某些东西导致数据需要刷新(添加或编辑了某些东西)。
  • 我做了一个更简单的例子plnkr.co/edit/Uz9yl8rHf7oXCAttsS7P看看我有 $scope.$watch( function () { return $scope.myData; }, function ( value ) { if (!$scope.searchText || $scope.searchText == ""){ $scope.myData = value; return; } /* 这给了我一个错误 10 $digest() 迭代达到。中止!*/ //$ scope.filterData($scope.searchText); console.log($scope.searchText); });
  • 我再次更新了 plunker 我已经添加了 $scope.origData 并且它似乎有效:)
猜你喜欢
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多