我正在使用输入框搜索数据库中的数据。当我在输入框中输入内容时,我只从A列得到相关数据预测,如果找到与给定输入的匹配,我想要来自A列和B列的预测。如果我从B列输入数据,那么在预测中我获得了该特定行的列A的值。这些列来自同一个表。所以,请帮助.. HTML:
<div class='col-md-6'>
<input autocomplete="off" type="text" ng-model="assignName" id="assignName" placeholder="Assigned To" class="form-control" min-length="4" uib-typeahead="as.aName as as.aName for as in assign($viewValue)" typeahead-editable="false">
<input type="hidden" id="assignId" value="{{assignName.aId}}"></div>
使用Javascript:
$scope.assign = function(assignName) {
if (assignName.length >= 2) {
return servicePOST.send(appConstants.BASE_MS_URL + 'Activity/search_query.php', {
'assignName': assignName,
'case': 'getAssign'
}).then(function(response) {
var res = _.map(response, function(value, key, list) {
var obj = {
'aName': '',
'aId': '',
'aFname':''
};
obj.aName = value.user_name;
obj.aId = value.id;
obj.aFname = value.first_name;
return obj;
});
return res;
});
}
};
PHP:
$sql="SELECT DISTINCT
users.id,
users.first_name,
users.user_name
FROM
users
WHERE
(users.first_name LIKE '$assignName%' OR users.user_name LIKE '$assignName%')";
如果我从A列输入数据,那么预测来自表A本身。
如果我从B列输入数据,我从A列获得的数据只有同一行......我想要从B列开始。
答案 0 :(得分:0)
而不是返回整个对象。尝试使用过滤器返回与键入的单词相关的特定对象:
类似的东西:
$scope.assign = function(assignName) {
if (assignName.length >= 2) {
return servicePOST.send(appConstants.BASE_MS_URL + 'Activity/search_query.php', {
'assignName': assignName,
'case': 'getAssign'
}).then(function(response) {
var res = _.map(response, function(value, key, list) {
var obj = {
'aName': '',
'aId': '',
'aFname':''
};
obj.aName = value.user_name;
obj.aId = value.id;
obj.aFname = value.first_name;
//here $ means search all the properties of object
return $filter('filter')(obj , {'$': assignName});
});
return res;
});
}
};
你的HTML要:
<input autocomplete="off" type="text" ng-model="assignName" id="assignName" placeholder="Assigned To" class="form-control" min-length="4" uib-typeahead="as for as in assign($viewValue)" typeahead-editable="false">