如何使用输入的值Angular Json填充表?

时间:2015-06-23 15:53:09

标签: javascript json angularjs jsp angularjs-ng-repeat

我正在使用clickButton函数根据用户输入的值填充表格。输入数据有3个字段。名字,姓氏,邮编。如果我键入firstName,请单击搜索我需要在表中查看关联的JSON数据。 lastName和zipCode也是一样的。当我设置$ scope.results = data时,将根据firstName的输入值正确填充表。我正在尝试为其他两个字段添加条件,然后将结果推送到jsp。我做错了什么?

JS

$scope.results = [];
$scope.clickButton = function(enteredValue) {
    $scope.items=this.result;
    var url = 'http://localhost:8080/application/names/find?firstName='+enteredValue+'&lastName='+enteredValue+'&zipCode='+enteredValue
    $http.get(url).success(function(data){
        angular.forEach($scope.items, function (item) {
            if(items.first_nm === enteredValue || items.last_nm ==enteredValue || items.zip_cd == enteredValue){
                 $scope.results.push({
                     firstName: item.first_nm,
                     lastName: item.last_nm,
                     zipCode: item.zip_cd
                 });
             }
         })
    })
   }; 

JSP

  <input id="firstName" name="firstName" type="text" data-ng-model="enteredValue" /> 
  <input id="lastName" name="lastName" type="text" data-ng-model="enteredValue" /> 
  <input id="zipCode" name="zipCode" type="text" data-ng-model="enteredValue" /> 

 <button class="btn btn-primary" data-ng-click='clickButton(enteredValue)'>Search</button>

 <tr data-ng-repeat="result in results" class="text-center">

 <td data-title="'ID'" >{{result.firstName}}</td>

 <td data-title="'Name'" >{{result.lastName}}</td>

  <td data-title="'Status'" >{{result.zipCode}}
  </td>

1 个答案:

答案 0 :(得分:1)

目前,您将所有搜索参数绑定到同一属性 - enteredValue。相反,您可以将它们分配给单独的属性,然后在搜索方法中相应地使用它们:

HTML:

<input id="firstName" name="firstName" type="text" data-ng-model="enteredValue.firstName" /> 
<input id="lastName" name="lastName" type="text" data-ng-model="enteredValue.lastName" /> 
<input id="zipCode" name="zipCode" type="text" data-ng-model="enteredValue.zipCode" /> 

<button class="btn btn-primary" data-ng-click='clickButton(enteredValue)'>Search</button>

控制器:

$scope.clickButton = function(enteredValue) {
    $scope.items=this.result;
    // instead of enteredValue use enteredValue.lastName and other properties
    // If your API allows it, you can even add the value only if it exists,
    // not always as it is done currently
    var url = 'http://localhost:8080/application/names/find?firstName='+
            enteredValue.firstName+'&lastName='+
            enteredValue.lastName+'&zipCode='+enteredValue.zipCode
   $http.get(url).success(function(data){
        angular.forEach($scope.items, function (item) {
            if(items.first_nm === enteredValue || items.last_nm ==enteredValue || items.zip_cd == enteredValue){
                $scope.results.push({
                    firstName: item.first_nm,
                    lastName: item.last_nm,
                    zipCode: item.zip_cd
                });
             }
         });
    });
};