AngularJS:为什么搜索工作不正常?

时间:2015-07-03 05:59:00

标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat

我正在尝试使用角度搜索桌面上的文本..我能够在表格中搜索文本。但是当我按下回车键或“搜索按钮”时,我的搜索工作。例如,当我写“活动”时,它不会显示结果但是当我按下回车键或按搜索按钮时它会显示输出。我们可以像自动完成一样搜索。例如,当我按下“a”时,显示从“a”开始的所有项目。然后如果用户写“ac”然后显示“ac”值..就像这样。当用户写“活动”时,它会显示没有使用搜索按钮的“活动”行或输入.can,我们可以添加过滤器以使其正常工作吗?

这是我的代码

$scope.searchInvoices = function(evt, queryval) {
        $scope.haveNorecordFound = true;

        if (typeof queryval != "undefined" && queryval.length === 0 || evt.keyCode === 13 || evt.type === 'click') {
            if (typeof queryval == "undefined" || queryval.length === 0) {
                console.log("if===")

                isfilterOccured = false;
                $scope.tasklist_records = $scope.total_tasklist_records;
                $scope.totalNumberOfrecord = $scope.tasklist_records.length + " records found."
            } else {
                console.log("esle===")
                var recordset = $scope.serachObject;
                results = [];
                var recordsetLength = recordset.length;
                var searchVal = queryval.toLowerCase();
                var i, j;

                for (i = 0; i < recordsetLength; i++) {
                    var record = recordset[i].columns;

                    for (j = 0; j < record.length; j++) {
                        if (record[j].value != null) {
                            var invoice = record[j].value.toLowerCase();
                            if (invoice.indexOf(searchVal) >= 0) {
                                results.push(recordset[i]);
                            }
                        }
                    }
                }
                var nameOrPathValues = results.map(function(o) {
                    var result = {};
                    o.columns.forEach(function(c) {
                        result[c.fieldNameOrPath] = c.value;
                    });
                    return result;
                });
                console.log("serach");

                console.log(nameOrPathValues);

                var objectarray = nameOrPathValues.map(function(o) {
                    var result = {};
                    collectNameOrPath.forEach(function(name) {
                        result[name] = o[name];
                    });
                    return result;
                });
                isfilterOccured = true;


                $scope.tasklist_records = objectarray;
                if ($scope.tasklist_records.length == 0) {
                    $scope.haveNorecordFound = false;
                } else {
                    $scope.totalNumberOfrecord = $scope.tasklist_records.length + " records found."
                }



            }
        }
    };

1 个答案:

答案 0 :(得分:0)

在代码中调试后,我发现您在enter button presssearch click上进行了重置功能。这就是为什么它不在ng-keyup上工作的原因。请从以下代码中替换您的功能。

$scope.searchInvoices = function(evt, queryval) {
      console.log(queryval)
      $scope.haveNorecordFound = true;
      var recordset = $scope.serachObject;
                results = [];
                var recordsetLength = recordset.length;
                var searchVal = queryval.toLowerCase();
                var i, j;

                for (i = 0; i < recordsetLength; i++) {
                    var record = recordset[i].columns;

                    for (j = 0; j < record.length; j++) {
                        if (record[j].value != null) {
                            var invoice = record[j].value.toLowerCase();
                            if (invoice.indexOf(searchVal) >= 0) {
                                results.push(recordset[i]);
                            }
                        }
                    }
                }
                var nameOrPathValues = results.map(function(o) {
                    var result = {};
                    o.columns.forEach(function(c) {
                        result[c.fieldNameOrPath] = c.value;
                    });
                    return result;
                });
                console.log("serach");

                console.log(nameOrPathValues);

                var objectarray = nameOrPathValues.map(function(o) {
                    var result = {};
                    collectNameOrPath.forEach(function(name) {
                        result[name] = o[name];
                    });
                    return result;
                });
                isfilterOccured = true;


                $scope.tasklist_records = objectarray;
                if ($scope.tasklist_records.length == 0) {
                    $scope.haveNorecordFound = false;
                } else {
                    $scope.totalNumberOfrecord = $scope.tasklist_records.length + " records found."
                }

    };