选择一个选项后,dataList 会自动填充其他字段 - AngularJs
dataList auto fill other fields when an option is selected - AngularJs
我有一个名称数据列表,我想在选择名称时自动填充 phone 数字和位置字段,目前,这些字段从 scopes 在 angular 控制器上单击 < td > 列,这是我的代码:
HTML 来源:
<div ng-controller="facture_retour"">
<table>
<thead>
<tr>
<td><strong>Client</strong></td>
<td>
<input type="text" id="client_List" list="clientList" />
<datalist id="clientList">
<option ng-repeat="fc in facture_client_retour" data-tele="{{fc.phone_number}}" data-location="{{fc.location}}" data-id="{{fc.id}}" value="{{fc.fname}} {{fc.lname}} |{{fc.code}}" id="optIdFc">
</datalist>
</td>
</tr>
<tr>
<td ng-click="getClientAttr();"><strong>Telephone</strong></td>
<td>{{clientPhoneNumber}}</td>
</tr>
<tr>
<td><strong>Location</strong></td>
<td>{{clientLocation}}</td>
</tr>
</thead>
</table>
</div>
我没有收到任何错误,当单击 < td > 调用函数 getClientAttr() 时它工作正常,但我的需要是在选择该选项时自动调用此函数。
JS 来源:
app.controller('facture_retour',function($scope, $http){
$scope.facture_client_retour;
angular.element(document).ready(function () {
$scope.getAllClients();
});
/*This function is responsible of getting all clients Name and display them
in the data-list */
$scope.getAllClients=function(){
var url = '../php/mainPageFacture.php';
var data = {"function":"getAllClients"};
var options={
type : "get",
url : url,
data: data,
dataType: 'json',
async : false,
cache : false,
success : function(response,status) {
$scope.facture_client_retour = response;
$scope.$apply();
},
error:function(request,response,error){
// alert("Error: " + error + ". Please contact developer");
swal({
title: "Please contact your software developer",
text: "ERROR: " + error,
type: "warning",
showCancelButton: false,
confirmButtonClass: "btn-info",
confirmButtonText: "Ok",
closeOnConfirm: true
});
}
};
$.ajax(options);
$scope.call=getClientAttr();
}
/*this function getting the other fields (phone number,location) values and
display them by clicking on < td >*/
$scope.getClientAttr=function(){
//alert("here we go");
var val = $('#client_List').val()
var client_phone_number= $('#clientList option').filter(function() {
return this.value == val;
}).data('tele');
$scope.clientPhoneNumber=client_phone_number;
var val = $('#client_List').val()
var client_location= $('#clientList option').filter(function() {
return this.value == val;
}).data('location');
$scope.clientLocation=client_location;
}
});
将 ng-model 添加到您的输入中
<input type="text" ng-model="Data" id="client_List" list="clientList" />
您可以将 ng-click 添加到数据列表选项并将所选值设置为模型
<datalist id="clientList">
<option ng-repeat="fc in facture_client_retour" data-tele="{{fc.phone_number}}" data-location="{{fc.location}}" data-id="{{fc.id}}" value="{{fc.fname}} {{fc.lname}} |{{fc.code}}" id="optIdFc" ng-click="Data = fc">
</datalist>
并在模型更改时在控制器中设置位置值和 phone 编号
$scope.$watchCollection('Data', function(newValue, oldValue) {
if(newValue != undefined)
angular.forEach($scope.data, function(value, key) {
if(value.name == newValue){
$scope.clientPhoneNumber = value.phone_number;
$scope.clientLocation = value.location;
}
});
});
我有一个名称数据列表,我想在选择名称时自动填充 phone 数字和位置字段,目前,这些字段从 scopes 在 angular 控制器上单击 < td > 列,这是我的代码:
HTML 来源:
<div ng-controller="facture_retour"">
<table>
<thead>
<tr>
<td><strong>Client</strong></td>
<td>
<input type="text" id="client_List" list="clientList" />
<datalist id="clientList">
<option ng-repeat="fc in facture_client_retour" data-tele="{{fc.phone_number}}" data-location="{{fc.location}}" data-id="{{fc.id}}" value="{{fc.fname}} {{fc.lname}} |{{fc.code}}" id="optIdFc">
</datalist>
</td>
</tr>
<tr>
<td ng-click="getClientAttr();"><strong>Telephone</strong></td>
<td>{{clientPhoneNumber}}</td>
</tr>
<tr>
<td><strong>Location</strong></td>
<td>{{clientLocation}}</td>
</tr>
</thead>
</table>
</div>
我没有收到任何错误,当单击 < td > 调用函数 getClientAttr() 时它工作正常,但我的需要是在选择该选项时自动调用此函数。 JS 来源:
app.controller('facture_retour',function($scope, $http){
$scope.facture_client_retour;
angular.element(document).ready(function () {
$scope.getAllClients();
});
/*This function is responsible of getting all clients Name and display them
in the data-list */
$scope.getAllClients=function(){
var url = '../php/mainPageFacture.php';
var data = {"function":"getAllClients"};
var options={
type : "get",
url : url,
data: data,
dataType: 'json',
async : false,
cache : false,
success : function(response,status) {
$scope.facture_client_retour = response;
$scope.$apply();
},
error:function(request,response,error){
// alert("Error: " + error + ". Please contact developer");
swal({
title: "Please contact your software developer",
text: "ERROR: " + error,
type: "warning",
showCancelButton: false,
confirmButtonClass: "btn-info",
confirmButtonText: "Ok",
closeOnConfirm: true
});
}
};
$.ajax(options);
$scope.call=getClientAttr();
}
/*this function getting the other fields (phone number,location) values and
display them by clicking on < td >*/
$scope.getClientAttr=function(){
//alert("here we go");
var val = $('#client_List').val()
var client_phone_number= $('#clientList option').filter(function() {
return this.value == val;
}).data('tele');
$scope.clientPhoneNumber=client_phone_number;
var val = $('#client_List').val()
var client_location= $('#clientList option').filter(function() {
return this.value == val;
}).data('location');
$scope.clientLocation=client_location;
}
});
将 ng-model 添加到您的输入中
<input type="text" ng-model="Data" id="client_List" list="clientList" />
您可以将 ng-click 添加到数据列表选项并将所选值设置为模型
<datalist id="clientList">
<option ng-repeat="fc in facture_client_retour" data-tele="{{fc.phone_number}}" data-location="{{fc.location}}" data-id="{{fc.id}}" value="{{fc.fname}} {{fc.lname}} |{{fc.code}}" id="optIdFc" ng-click="Data = fc">
</datalist>
并在模型更改时在控制器中设置位置值和 phone 编号
$scope.$watchCollection('Data', function(newValue, oldValue) {
if(newValue != undefined)
angular.forEach($scope.data, function(value, key) {
if(value.name == newValue){
$scope.clientPhoneNumber = value.phone_number;
$scope.clientLocation = value.location;
}
});
});