Enhancing List Page Functionality with AngularJS/MVC5 Search Feature

As I work on enhancing the List page, my main focus is on implementing a search feature.

While the code below effectively displays data in a list format, I am uncertain about how to start incorporating a search functionality into this page.

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

Answer №1

For those focused on server-side search, a slight adjustment in approach is necessary.

The current method retrieves data when the page loads. It would be better to move this process to a separate Action or WebAPI controller:

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

Assuming that you will create the GetEmployeesByText function with the required logic.

Subsequently, retrieve the data in the 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();
});

Lastly:

<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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for including body text in a Bootstrap tooltip?

I am attempting to enhance a Bootstrap tooltip by adding body text, as it typically only includes a basic title. After reviewing the Bootstrap 5 documentation, it seems that the simplest approach is to utilize the template option: template Base HTML used ...

Transfer the selected user content from one canvas to another

After successfully implementing a simple selection area on canvasA, I encountered an issue with copying the area to canvasB. The user is supposed to select an area and then see that selection appear on another canvas once they finish making the selection b ...

What is the best way to access dropdown sub-menu options from a complex multi-level navigation bar

Struggling to figure out how to open my dropdown sub-menu using CSS. Hoping to make it so the user can open it by hovering over the corresponding tag. I attempted to use #lablinksDD to trigger the opening of #ddSbMenu when hovered over #menuDD with #labLin ...

How can I utilize columns from a junction table in the "where" clause in sequelize?

Looking to execute a Sequelize query on multiple related models: SELECT Cou.country_id, cou.country_name, Sta.state_id, Sta.state_name, Dis.district_id, Dis.district_name, Cit.city_id, Cit.city_name, Loc.location_id, Loc.location_name, Sub_Loc.sub_locatio ...

Displaying multiple categories of articles on a single page with Node.js

Seeking a solution to limit the number of posts displayed for each category using a .limit() function, but uncertain on how to proceed. Currently utilizing Mongoose and Express in my code setup. The existing code snippet is outlined below: router.get(&a ...

how can I enable pass-through functionality in express middleware?

Currently, I have a POST API endpoint named /users which is used to retrieve the list of users. The reason behind using POST instead of GET is because the request body can be quite large and it may not fit in the URL for a GET request. In this scenario, i ...

The method mongoose.connect() is not defined

Having a bit of trouble connecting to my MongoDB using Mongoose - keep getting this error. const { mongoose } = require('mongoose'); const db = 'dburl.com/db' mongoose.connect(db, { useNewUrlParser: true }) .then(() => console ...

Capturing mouse clicks in Javascript: a guide to detecting when the mouse moves between mousedown and mouseup events

I have been working on a website that features a scrolling JavaScript timeline, inspired by the code found in this tutorial. You can check out the demo for this tutorial here. One issue I've encountered is when a user attempts to drag the timeline an ...

Utilize JavaScript and jQuery to locate a particular character within a string and move it one position back in the sequence

Can you locate a particular character within a string and move it to the position before? For instance: Consider the following string: Kù Iù Mù The desired output is: ùK ùI ùM ...

Getting the specific information you need from a JSON object in Angular 2

Struggling to extract specific data from a JSON file with nested objects like this: { "results" : [ { "address_components" : [ { "long_name" : "277", "short_name" : "277", "types" : [ "street_number" ] ...

Creating a cascading select box with two levels in PHP and MySQLExplanation on how to generate a two-tier connected

While I have successfully retrieved values from a MySQL database using a select box in PHP, I am struggling with implementing a two-level chained select box. Does anyone have any sample code or suggestions on how to achieve this? Thank you. ...

Unleash the full power of Angular Components by enhancing them with injected

I am facing a challenge with handling the destruction event of an Angular component in my external module that provides a decorating function. I've run into issues trying to override the ngOnDestroy() method when it includes references to injected ser ...

Issue with exporting Three.js to Maya

I've been attempting to utilize the Three.js exporter for Maya found here. However, when I try to load the threeJsFileTranslator.py plug-in from the plug-ins manager in Maya, I encounter an error in the Script Editor: // Error: line 1: invalid synta ...

Develop a unique Kotlin/JS WebComponent featuring custom content

I am trying to create a custom tag using Kotlin that includes default content. While the example I found works well, I am having trouble adding default content (such as an input element) inside the custom tag. After attempting various approaches, I have o ...

After the JavaScript calculation is completed, the following display may show a value of NaN

Check out my JavaScript calculation in action: Live Preview I've encountered an issue with the calculation script. After the initial calculation, it shows a NAN value on subsequent calculations without reloading the page. Is there a way to enable rep ...

The Angular date filter appears to be malfunctioning in Firefox

When formatting dates in my Angular application, I encountered an issue with the date value appearing differently in Firefox compared to Chrome. In Firefox, the date value displayed as: undefined NaN, NaN NaN:NaN:NaN PM However, in Chrome, it displaye ...

Ways to ensure all images have been successfully uploaded to Firebase before executing a function

Here's a function I've created that takes image URIs from the state, uploads them to Firebase Storage, and then updates the state with the download URLs: uploadImages = async (userID) => { // Loop through each image for (var index = 0 ...

A step-by-step guide on implementing ng-annotate to your code

Is there a way to run ng-annotate through the command line? I am attempting to combine and minify Angular, Angular Routes, and my own script.js into one file. When I use grunt uglify:app1, I encounter an injection error. While my code successfully minifies ...

Is there a way to position my character at the exact center of my mouse click coordinates instead of the top left corner?

In my game, players can click on a 2D HTML5 canvas to set a point for their character to move to. However, I've noticed that when I click, the character appears in the lower right corner of where my mouse is clicked. After some research, I realized th ...

What is the best method for expanding the width of a <rect> element with animateTransform?

How can I make a <rect> in SVG expand its width using animateTransform based on a specified value? Additionally, I want the color of the <rect> to change according to the following conditions: If the <rect> value is between 0 and 29: {f ...