gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:09:45 26 4
gpt4 key购买 nike

我正在开发一个 clickButton 函数来根据用户输入的值填充表格。有 3 个字段用于输入数据。名字、姓氏、邮政编码。如果我输入firstName,单击“搜索”,我需要查看表中关联的JSON 数据。姓氏和邮政编码也是如此。当我设置 $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>

最佳答案

目前,您将所有搜索参数绑定(bind)到同一属性 - 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>

Controller :

$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
});
}
});
});
};

关于javascript - 如何使用输入值 Angular Json 填充表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31007657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com