【问题标题】:Clear bound SmartTable textbox with AngularJS使用 AngularJS 清除绑定的 SmartTable 文本框
【发布时间】:2015-08-02 01:38:55
【问题描述】:

我在我的 Angular 1.2.x 应用程序中使用 SmartTable。具体来说,我遵循谓词列表驱动搜索过程的模式。在项目网站here 上给出了一个具体的例子。

您可以从示例中看到,当您选择一个谓词并执行搜索,然后在列表中选择另一个谓词时,文本框仍然包含先前的搜索条件。

我对 AngularJS 相当陌生,我正在尝试以 AngularJS 的方式清除谓词选择框的更改事件的搜索结果。我的第一个想法是在指令后面推动任何类型的 DOM 操作。所以我创建了一个用于重置搜索条件的调用 "tndResetSearch" 。我在jade中的语法似乎很混乱......如果有更好的方法来组织这个,我欢迎建议;):

select.form-control.tnd-reset-search(name="selectedPredicate", type="text" ng-model="selectedPredicate",
  ng-options="predicate.PredicateId as predicate.PredicateName for predicate in predicates",
  itemdata="predicate", options="#serviceLogSearchBox", resetsearch="resetSearch()")

resetsearch="resetSearch()" 绑定到我的指令的隔离范围属性。

在控制器中的实现只是简单地从$scope 中清除模型并重新填充智能表用于填充视图的集合:

$scope.resetSearch = function() {
  delete $scope.searchQuery;
  $scope.initCollection();
}

$scope.initCollection = function() {
  $scope.serviceLogCollection = '';
  $scope.serviceLogCollection = [].concat($scope.originalServiceLogCollection);
};

这执行得很好,但每次我更改选择框中的谓词时,以前的搜索条件似乎被缓存并附加到当前的搜索条件。所以我最终得到了之前搜索的一个子集。我不确定缓存发生在哪里。 $scope 中必须有 SmartTable 搜索指令在下一次搜索之前查看的内容。除非我在我的方法中做错了什么,否则我将不得不查看 SmartTable ,看看我是否可以追踪到这一点。

上面select 框中的options="#serviceLogSearchBox" 是我再次尝试获取对相关搜索框的引用并手动清除它,但这根本没有效果。

这是我对指令的第一次尝试:

angular.module('app').directive('tndResetSearch', [function() {
    return {
        restrict: 'CA',
        replace: false,
        transclude: false,
        scope: {
            index: '=index',
            predicate: '=itemdata',
            resetSearch: '&resetsearch'
        },
        link: function(scope, elem, attrs) {

            var maxNukes=100, currentNuke=0, triggerKeyDown, nukeSearch;

            triggerKeyDown = function (element, keyCode) {
              var e = angular.element.Event('keydown');
              e.which = keyCode;
              element.trigger(e);
            };

            nukeSearch = function() {
                // Trigger keydown event for bound element that uses the stSearch directive???
                // This never actually does anything, It just loops forever.
                //
                // var target = angular.element(attrs.options);
                // while (target.val().length > 0 && currentNuke < maxNukes) {
                //    triggerKeyDown(target, 8); //backspace=8
                //    currentNuke++;
                //}
                // Call referenced function on isolate scope
                scope.resetSearch();
            };

            // Modify the DOM the first time the view renders with the first item selected
            if (parseInt(scope.index)===0) {
                nukeSearch();
            }

            elem.bind('change', function (evt) {
                nukeSearch();
            });

        }
    }
}]);

有谁知道为什么我会看到我提到的行为,我会以错误的方式解决这个问题吗?如果是这样,使用 Angular 1.2.x 和 SmartTable 的最佳方法是什么?

【问题讨论】:

    标签: angularjs pug smart-table


    【解决方案1】:

    我将上面的指令tndResetSearch 简化为:

    angular.module('app').directive('tndResetSearch', ['$parse', function($parse) {
        return {
            require: '^stTable',
            restrict: 'CA',
            link: function(scope, elem, attrs, ctrl) {
    
                var tableCtrl = ctrl,
                    fn = $parse(attrs['resetSearch']);
    
                nukeSearch = function(evt) {
                    scope.$apply(function() {
                        fn(scope, {
                            $event: evt
                        })
                    });
                };
    
                elem.bind('change', function (evt) {
                    nukeSearch(evt);
                });
    
            }
        }
    }]);
    

    ...然后不是这样做:

    $scope.resetSearch = function() {
      delete $scope.searchQuery;
      $scope.initCollection();
    }
    

    我这样做了,它有效:

    $scope.resetSearch = function(evt) {
      $scope.initCollection();
      $scope.searchQuery = ' ';
    }
    

    searchQuery 是我的搜索输入框的ng-model。似乎它必须是空字符串、null 或未定义的任何内容。否则,stSearch 指令看不到任何变化,并假定之前的搜索值仍然存在。

    这里值得一提的是我的搜索文本框(翡翠):

    input.form-control(id="serviceLogSearchBox", 
      st-search="{{selectedPredicate}}", placeholder="Search", 
      type="search", ng-model="searchQuery")
    

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 2017-08-05
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多