简体   繁体   English

Angularjs 来自文本输入字段的 ui-grid 过滤器

[英]Angularjs ui-grid Filter from text input field

Maybe somebody can help me with a little problem.也许有人可以帮我解决一个小问题。 I am pretty new in the field of web programming, but I have programming experience.我是 web 编程领域的新手,但我有编程经验。 I would like to develop a small website that uses angularjs and ui-grid.我想开发一个使用 angularjs 和 ui-grid 的小型网站。 Unfortunately the filtering is not working from external input fields.不幸的是,过滤不适用于外部输入字段。

Could somebody please tell me where my programming bug is?有人可以告诉我我的编程错误在哪里吗?

The code can be found here: http://plnkr.co/edit/fiA666OzpBqpTrCiuXER?p=preview代码可以在这里找到: http://plnkr.co/edit/fiA666OzpBqpTrCiuXER?p=preview

    var myDummyData = [{name: "Moroni", age: 50},
        {name: "Tiancum", age: 43},
        {name: "Jacob", age: 27},
        {name: "Nephi", age: 29},
        {name: "Enos", age: 34}];
    var myDummyData2 = [{name: "aTest", age: 50},
        {name: "bTest1", age: 43},
        {name: "cTest2", age: 27},
        {name: "dTest3", age: 29},
        {name: "eTest4", age: 34}];

    $scope.filterOptions = {
        filterText: ''
    };

    $scope.gridOpts = {
        data: myDummyData,
        enableFiltering: true,
        columnDefs: [
                    {name: 'Name', field: 'name', enableFiltering: true
                        , filter: {
                            term: $scope.filterOptions.filterText //DOES NOT WORK... BUT WHY
                        }
                    },
                    {name: 'Price', field: 'age'}
                ]
    };


    $scope.updateData = function(newValue){
         //console.log($scope.gridOpts.data);

         console.log($scope.gridOpts.columnDefs[0].filter);
         $scope.gridOpts.columnDefs[0].filter = {term: newValue};
         console.log("Scope nameid set " + $scope.gridOpts.columnDefs[0].filter.term); //is set but no update
         //$scope.$apply(); //not possible gives error! WHY??


         //$scope.gridOpts.data = myDummyData; //for testing works
    };


    $scope.swapData = function () {
        if ($scope.gridOpts.data === myDummyData) {
            $scope.gridOpts.data = myDummyData2;
        }
        else {
            $scope.gridOpts.data = myDummyData;
        }
    };

    //DOES NOT WORK BUT WHY
//        $scope.$watch('filterOptions.filterText', function (newValue, oldValue) {
//            if ($scope.filterOptions.filterText) {
//                $scope.filteringText = newValue;
//                $scope.updateData(newValue);
//            }
//        });

The idea is to have a navigation bar that contains a search field.这个想法是有一个包含搜索字段的导航栏。 Later I want to filter depending on rangesliders on further columns.稍后我想根据更多列上的范围滑块进行过滤。 However not even the standard string filtering works in my example.但是,在我的示例中,即使是标准字符串过滤也不起作用。

Questions:问题:

  1. Could somebody tell me where my current problem is?有人可以告诉我我目前的问题在哪里吗? More specifically: Why does the filtering from external input fields not work?更具体地说:为什么来自外部输入字段的过滤不起作用?
  2. The other question is how can I bind min and max values of range sliders to eg the age column in my example?另一个问题是如何将范围滑块的最小值和最大值绑定到示例中的年龄列? (directly related to the binding problem in question (1)) (与问题(1)中的绑定问题直接相关)

I looked around for answers, but either this is directly a problem of the binding that I cannot grasp, a mere programming js problem, or a ngGrid update to ui-grid problem.我四处寻找答案,但这要么是我无法掌握的直接绑定问题,要么是编程 js 问题,要么是 ngGrid 更新到 ui-grid 问题。

Thanks a lot in advance非常感谢

The answer to your first question, they have not really made a global search option for the UI-Grid yet, so you have to do a bit of a work around. 对于你的第一个问题的答案,他们还没有真正为UI-Grid做出全局搜索选项,所以你必须做一些工作。 The current way that the column filters work is angular watches the column filter input field for a change, and when you type in the box, it refreshes its filter. 列过滤器工作的当前方式是角度监视列过滤器输入字段以进行更改,当您在框中键入时,它会刷新其过滤器。 So your filter will not apply unless you force this input box to fire the change event. 因此,除非您强制此输入框触发更改事件,否则您的过滤器将不适用。 The workaround is to simply filter the data and reload. 解决方法是简单地过滤数据并重新加载。 For example: 例如:

In your HTML input search box, add a refresh 在HTML输入搜索框中,添加刷新

<input ng-model="searchText" ng-change="refreshData()" placeholder="Search...">

then in your app.js 然后在你的app.js

$scope.refreshData = function() {
  $scope.myGrid.data = $filter('filter')($scope.data, $scope.searchText);
};

oh, and don't forget to include $filter in your controller 哦,不要忘记在控制器中包含$ filter

app.controller('myController', function($scope, $filter) {}

I forked your example and modified it with this workaround. 我分叉你的例子并用这种解决方法修改它。 Check it out here: http://plnkr.co/edit/Tr9cNm0lDy62dmF7LNlr?p=preview 在这里查看: http//plnkr.co/edit/Tr9cNm0lDy62dmF7LNlr?p =preview

try this: 试试这个:

$scope.gridOpts = {
    data: myDummyData,
    enableFiltering: true,
    columnDefs: [
                {name: 'Name', field: 'name', enableFiltering: true
                    , filter: {
                        noTerm: true,
                        condition: function(searchTerm, cellValue) {
                            return (cellValue+"" === $scope.filterOptions.filterText+"");
                        }
                    }
                },
                {name: 'Price', field: 'age'}
            ]
};

for input box: <input ng-model="searchText" ng-change="refresh()" placeholder="Search..."> 输入框: <input ng-model="searchText" ng-change="refresh()" placeholder="Search...">

$scope.refresh = function()
{
    $scope.gridApi.core.refresh();
}

I hope it works... 我希望它有效......

There is a simple way to filter on ui-grid.有一种简单的方法可以在 ui-grid 上进行过滤。 In HTML HTML

<input type="text" ng-model="searchText" ng-change="refreshData()" />

In your js file在你的 js 文件中

 app.controller('myController', function($scope, $filter) {
        $scope.refreshData = function () {
            $scope.myGrid.data = $filter('filter')($scope.myGridData, $scope.searchText);
        }

        $scope.myGrid = { ... }

        $scope.dataBindingWithGrid = function () { 
            ...
            $scope.myGridData = data;
            $scope.myGrid.data = data;
            ...
        }
        ...
        ...
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM