javascript - 使用 AngularJS/MVC5 在列表页面上实现搜索

标签 javascript angularjs asp.net-mvc-5

我正在尝试在列表页面上实现搜索。

以下代码在将数据显示为列表时有效,但我不确定如何开始为此页面实现搜索功能。

HTML:

<body ng-app="myApp">
    <div ng-controller="empCtrl" class="container">

      <div>
         <input type="text" ng-model="search_filter" placeholder="Search" />
       </div>

        <div class="container" infinite-scroll="pagedata()">
            <div>
                <div>
                    Id
                </div>
                <div>
                    Name
                </div>
                <div>
                    Description
                </div>
            </div>

            <div ng-repeat="emp in emps | filter: search_filter"> //<<<<
                <div class="column fifth">
                    {{emp.Id}}
                </div>
                <div class="column fifth">
                    {{emp.Name}}
                </div>
                <div class="column fifth">
                    {{emp.Description}}
                </div>
            </div>
        </div>
    </div>
</body>

MVC Controller :

public ActionResult Employee()
{
    var model = new EmployeeViewModel();
    var employees = GetEmployees();
    model.EmployeeList = employees;
    return View("List", model);
}

Angular :

myApp.controller('empCtrl', function ($scope, $http, $window) {
    $scope.employees = $window.EmployeeViewModel;

    $scope.pagedata = function () {
        $http.get($scope.getBaseUrl()).success(function (data) {
        });
    }
});

最佳答案

由于您对服务器端搜索感兴趣,因此您需要稍微改变一下您的方法。

您正在检索的数据会在检索页面时填充。我建议将它移动到单独的 Action 或 WebAPI Controller ,如下所示:

[HttpGet]
public ActionResult List(string text)
{
    var employees = GetEmployeesByText(text);
    return Json(employees, JsonRequestBehavior.AllowGet);
}

我假设您将实现具有必要功能的 GetEmployeesByText

然后在 Angular Controller 中获取数据:

myApp.controller('empCtrl', function ($scope, $http) {
    $scope.employees = null;
    $scope.search_filter = null;

    $scope.getEmployees = function(text){
        $http.get('/Employee/List?text=' + $scope.search_filter)
            .success(function(data){
                $scope.employees = data; 
            });
    };

    $scope.getEmployees();
});

最后:

<body ng-app="myApp">
    <div ng-controller="empCtrl" class="container">

      <div>
         <input type="text" ng-model="search_filter" placeholder="Search" ng-change="getEmployees()" />
       </div>

        <div class="container">
            <div>
                <div>
                    Id
                </div>
                <div>
                    Name
                </div>
                <div>
                    Description
                </div>
            </div>

            <div ng-repeat="emp in employees">
                <div class="column fifth">
                    {{emp.Id}}
                </div>
                <div class="column fifth">
                    {{emp.Name}}
                </div>
                <div class="column fifth">
                    {{emp.Description}}
                </div>
            </div>
        </div>
    </div>
</body>

关于javascript - 使用 AngularJS/MVC5 在列表页面上实现搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25729547/

相关文章:

angularjs - E2E 模拟 $httpBackend 对我来说实际上并没有通过

powershell - 如何添加在 Visual Studio 2013 脚手架中使用的新脚手架?

c# - 将值从 View 传递到 Controller,我缺少什么?

javascript - 使用 javascript 禁用 requiredfieldvalidator

javascript - 在 Node.js/Express.js 中使用超出回调函数范围的变量

JavaScript Moment.js 试图获得 2 个日期时间之间的差异

javascript - Impress.js 和使用 HTML5 数据属性代替 CSS 转换

javascript - 单击另一个函数即可触发 Angular 函数

javascript - 动态 ng-messages 和验证

c# - 从 azure 创建到数据库的连接