我有一个问题。我需要使用 Angular.js 使用 restaurant name
从表中过滤数据。我有一个搜索框。如果用户输入名称的第一个字母,则应过滤与该字母相关的所有名称。它不会那样发生。我在下面解释我的代码。
<div class="input-group" style="margin-bottom:10px; width:300px;">
<input class="form-control" placeholder="Type Restaurant Name" name="q" type="text" ng-model="searchProduct.rest_name">
</div>
<table class="table table-bordered table-striped table-hover" id="dataTable" >
<thead>
<tr>
<th>Sl. No</th>
<th>Restaurant Name</th>
</tr>
</thead>
<tbody id="detailsstockid">
<tr dir-paginate="cus in ($parent.labelResults=(listOfCustomerData | filter:searchProduct.rest_name:startsWith | orderBy:'rest_name')) | itemsPerPage:5 track by $index" current-page="currentPage">
<td>{{itemsPerPage *(currentPage-1)+$index+1}}</td>
<td>{{cus.rest_name}}</td>
</tr>
</tbody>
</table>
我的 Controller 端代码如下。
$http({
method:'GET',
url:"php/customerInfo.php?action=disp",
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function successCallback(response){
console.log('res',response.data);
$scope.listOfCustomerData=response.data;
},function errorCallback(response) {
})
$scope.startsWith = function (actual, expected) {
var lowerStr = (actual + "").toLowerCase();
return lowerStr.indexOf(expected.toLowerCase()) === 0;
}
这里假设我有很多餐厅名称,例如 Anjum、A&P Chinese Food Express、Bookers BBQ & Crab Shack、Butcher And The Baker、Cactus Club Stephen Avenue、Cactus Club - Macleod Trail。在这里,当用户仅在搜索框中输入“a”时,名称以“should”过滤器开头,但事实并非如此。如果我将静态值分配给对象 $scope.listOfCustomerData
,此功能将按预期工作,但我使用 PHP 动态获取数据。但在我的情况下,它不会发生这样的情况。请帮我。
最佳答案
尝试使用这个 -
<input class="form-control" placeholder="Type Restaurant Name" name="q" type="text" ng-model="searchProduct">
</div>
<tr dir-paginate="cus in listOfCustomerData | filter:searchProduct | orderBy:'rest_name' | itemsPerPage:5 track by $index" current-page="currentPage">
在您的 Controller 中定义 - $scope.searchProduct = '';
关于javascript - 使用 Angular.js 和 PHP 时过滤器功能无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38244292/