简体   繁体   English

Angular ui-grid以编程方式设置过滤器字段并刷新。 价值没有显示出来

[英]Angular ui-grid set filter field programatically and refresh. Value not showing up

I have a ui-grid with a bunch of columns using the built in filtering. 我有一个使用内置过滤的一堆列的ui-grid。 One of the columns is "owner". 其中一列是“所有者”。 There is a button you can click that says "My Items". 您可以单击一个按钮,显示“我的项目”。 When clicked that button should populate the Owner Filter field with the users name and filter the items. 单击该按钮时,应使用用户名填充“所有者过滤器”字段并过滤项目。 I am setting the filter as follows as specified in the ui-grid documentation: 我按照ui-grid文档中的说明设置过滤器如下:

$scope.gridApi.grid.columns[3].filters[0] = "somename"; $ scope.gridApi.grid.columns [3] .filters [0] =“somename”;

However "somename" never shows up in the column filter header, and the data never refreshes. 但是“somename”永远不会出现在列过滤器标头中,并且数据永远不会刷新。 I've tried calling refresh() as well as notifyDataChange but nothing seems to work. 我试过调用refresh()以及notifyDataChange,但似乎没有任何工作。

Thanks. 谢谢。

Here is the correct way of doing it. 这是正确的做法。 By the way, there is no need to call refresh() function. 顺便说一句,没有必要调用refresh()函数。

  $scope.gridApi.grid.columns[3].filters[0] = {
    term: somename
  };

I was trying to work this answer, but was having problems. 我试图解决这个问题,但遇到了问题。 I solved it with a slight syntax alteration (changed grids.columns[2] to grid.getColumn('mycolumn') ) 我通过轻微的语法更改解决了这个问题(将grids.columns[2]更改为grid.getColumn('mycolumn')

$scope.grid1Api.grid.getColumn('speicialty').filters[0] = {
   term: whatever
};

Hope that helps for anyone looking 希望对任何人都有帮助

For my particular case, this is all of my code: 对于我的特殊情况,这是我的所有代码:

Controller: 控制器:

function skillsFunc(job) {
   console.log(job);
   $scope.grid1Api.grid.getColumn('speicialty').filters[0] = {
      term: job
   };
};

HTML: HTML:

<div class="input-field col s2 locator-margin3">
  <label for="skills1" class="locator-label">SPECIAL SKILLS</label>
  <select ng-model="vm.skills" ng-change="vm.skillsFunc(vm.skills)" id="skills1" class="browser-default locator-select ">
        <option value="1">SKILLS</option>
        <option value="Audiolog">Audiologist</option>
        <option value="Allerg">Allergist</option>
        <option value="Androlog">Andrologist</option>
   </select>
</div>

Okay, I realize the OP figured it out (despite marking an answer.) For me it was a bit more of a pain. 好吧,我意识到OP已经弄明白了(尽管有一个答案。)对我而言,这有点痛苦。 The answer was hidden between the lines. 答案隐藏在线条之间。 Here is what worked for me: 这对我有用:

$scope.MyGridName.grid.columns[2].filters[0] = { term: "CP" };
$scope.MyGridName.grid.refresh();

The "CP" is actually what comes from a KendoUI Chart. “CP”实际上来自KendoUI图表。 But you can put in whatever you want there. 但你可以在那里放任何你想要的东西。 The 2 in columns[2] was my 3rd column showing. 列[2]中的2是我的第3列。 I had no hidden fields like the OP. 我没有像OP那样隐藏的领域。

I find working answer and create directive: 我找到了工作答案和创建指令:

.directive('uiGridSaveFilter', function ($compile, $timeout, $window) { // use $window to save local variable
    return {
        priority: 0,
        scope: false,
        require: '^uiGrid', 
        replace: true,
        link: function ($scope, $elm, $attrs) {
            $window.gridState = []; 
            $timeout(function () {

                $scope.gridApi.core.on.filterChanged($scope, saveState); //filter listener
                $scope.gridApi.core.on.rowsRendered($scope, restoreState); //rebuild listener

                function saveState() {
                    $window.gridState = [];
                    var grid = this.grid;

                    /// set filters to local array with config

                    angular.forEach(grid.columns, function (value, key) {
                        if (value.filters[0].term) {
                            var dummy = {};
                            dummy['k'] = value.colDef.name;
                            dummy['v'] = value.filters[0].term;
                            $window.gridState.push(dummy);
                        }
                    });
                }

                function restoreState() {

                    var grid = this.grid;
                    $timeout(function () {

                       ///loop columns and check is any filters added to field

                        angular.forEach(grid.columns, function (value, key) {
                            angular.forEach($window.gridState, function (value2, key2) {
                                if (value.field === value2.k) {
                                    value.filters[0].term = value2.v;
                                }
                            });
                        });
                    });
                }
            });
        }
    };
});

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

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