The search filter in Angular is limited in its ability to search through the entire table

I am facing an issue with the search filter in my table. Currently, it only searches records from the current page but I need it to search through the entire table. How can I modify it to achieve this?

<input type="text" placeholder="Search By Any..." ng-model="search.$"/>
<table dir="ltr" width="477" border="1" class="table table-striped table-bordered">
    <thead>
        <tr>
            <th><a style="font-size: 16px;" href="#" ng-click="changeSort('name')">User</a></th>
            <th><a style="font-size: 16px;" href="#" ng-click="changeSort('contentType')">Content Type</a></th>
            <th><a style="font-size: 16px;" href="#" ng-click="changeSort('contentName')">Content Name</a></th>
            <th><a style="font-size: 16px;" href="#" ng-click="changeSort('startTime')">Start Time</a></th>
            <th><a style="font-size: 16px;" href="#" ng-click="changeSort('endTime')">End Time</a></th>
            <th><a style="font-size: 16px;" href="#" ng-click="changeSort('duration')">Duration(In Secs)</a></th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="record in filteredRecords | filter: search | orderBy:sort:reverse track by $index">
            <td>{{record.user}}</td>
            <td>{{record.contentType}}</td>
            <td>{{record.contentName}}</td>
            <td>{{record.startTime}}</td>
            <td>{{record.endTime}}</td>
            <td>{{record.duration}}</td>
        </tr>
    </tbody>
</table>
<pagination class="pull-right" style="cursor: pointer;" num-pages="numPages()" current-page="currentPage" on-select-page="pageChanged(page)"></pagination>

Here is the Angular code snippet:

angular.module("contentViewStatusApp")
       .controller("contentViewStatusController", function($scope, contentViewStatusService){
    $scope.records = contentViewStatusService.list();
    $scope.currentPage = 1
    $scope.numPerPage = 10
    $scope.maxSize = 5;
    $scope.numPages = function(){
        return Math.ceil($scope.records.length / $scope.numPerPage);
    };
    $scope.changeSort = function(value){
        if ($scope.sort == value){
            $scope.reverse = !$scope.reverse;
            return;
        }
        $scope.sort = value;
        $scope.reverse = false;
    }
    $scope.$watch('currentPage + numPerPage', function(){
        var begin = (($scope.currentPage - 1) * $scope.numPerPage), end = begin + $scope.numPerPage;
        $scope.filteredRecords = $scope.records.slice(begin, end);
    });
});

Answer №1

If you're looking to implement pagination without creating a separate array, consider using filters instead. This allows for a search through the entire array before narrowing down the results.

Check out this resource for an example of pagination with filters.

Start by inputting the search query to search through the entire array efficiently.

Update:

For a demonstration of pagination that dynamically updates based on search text, take a look at this link.

The script monitors changes in the search text and adjusts the page range after filtering:

$scope.$watch('searchText.name', function (v) {
    $scope.currentPage = 0;
    $scope.pages = $scope.range();
});

The pageCount function is then utilized to update the number of pages based on the filtered results:

$scope.pageCount = function () {
    var pages = $filter('filter')($scope.items, $scope.searchText);
    return Math.ceil(pages.length / $scope.itemsPerPage);
};

Answer №2

Hey, I totally agree with @agreco's point of view. In order to ensure that filtered records always interact smoothly with your search.$ model, it is crucial to define them in a specific way. Take a look at this example for guidance.

I hope this solution works out for you! Best of luck.

Answer №3

Here is a possible solution:

/**
 * @param name A string to be compared with the input
 */
checkName(name: string, input: string): boolean {
    var match = true;
    if(input.length == 0) {
      return true;
    } else {      
        for(var i = 0, j = 0; i < input.length; i++) {
          if(name.charAt(j) == input.charAt(i)) {
            j++;
            match = true;  
          } else {
            match = false;
          }
        }
        return match;
    }
}

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

Show only the items in bootstrap-vue b-table when a filter is actively applied

How can I set my bootstrap-vue b-table to only show items when a filter is applied by the user (i.e., entered a value into the input)? For example, if "filteredItems" doesn't exist, then display nothing? This is primarily to prevent rendering all rows ...

What is the best way to transfer values or fields between pages in ReactJS?

Is there a way to pass the checkbox value to the checkout.js page? The issue I'm facing is on the PaymentForm page, where my attempts are not yielding the desired results. Essentially, I aim to utilize the PaymentForm fields in the checkout.js page as ...

Learn the technique of initiating one action from within another with Next Redux

I'm looking to set up user authorization logic when the page loads. My initial plan is to first check if the token is stored in the cookies using the function checkUserToken. Depending on whether the token is present or not, I will then call another f ...

Experiencing a hiccup while attempting to query the Twitter API using Node.js

I am a beginner exploring the world of node.js, but I keep encountering a perplexing "write after end" error. Despite searching for solutions online, none seem to address my specific issue. My current project involves querying the Twitter API. req.on(&apo ...

Position validation in jQuery is crucial for ensuring that form

After attempting to implement the jquery validate plugin by following the example provided at http://docs.jquery.com/Plugins/Validation, I have encountered some challenges in my own code. The issue lies in determining where to call the validation function. ...

I'm interested in learning about the most efficient practices for handling JSON, performing math operations, and utilizing loops in JS/React. What techniques

Short version: I'm working with a large array of JSON objects (60K+ elements) in my application I need to perform various mathematical operations such as comparison and addition Currently, I am handling this through multiple for loops (simplified ...

Creating a sidebar that remains fixed in place even as the page is scrolled down can be achieved by using CSS and positioning properties

I am looking to create a website with a specific layout design as shown in this image: https://i.stack.imgur.com/ndKcz.png The main focus is on making the sidebar (F/T container) function as a social network link, sticking to the right side of the page ev ...

Modifying the design of a website in real-time using the EXPRESS.js and NODE.js frameworks

I successfully set up a simple website using node.js and express.js by following this helpful tutorial. My express implementation is structured like this with a jade file for the web interface. // app.js var express = require('express'), r ...

The POST method in XHR encounters issues on IOS when used with the Canvas to Blob script

Observation I've noticed a specific part of the code that's not functioning correctly only on IOS devices. xhr.open('POST', 'upload.php', true); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); var da ...

Using Input Mask with TextField Component in Material-UI with React Hook Form

Currently, I am trying to utilize the MUI's TextInput component alongside the MaskInput component from react-input-mask and react-hook-form. Despite everything appearing to be functioning correctly, an error message related to using refs keeps popping ...

Ways to display a US map using d3.js with state names positioned outside each state and pointing towards it

Currently, I am working with d3.js and d3-geo to create a map of the USA. My goal is to display the names of some states inside the state boundaries itself, while others should have their names positioned outside the map with lines pointing to the correspo ...

Disabling the Autocomplete Drop-Down Arrow

Can the drop-down arrow icon be removed from the material-ui Autocomplete react component? My current view includes a blue arrow that I'd like to remove, opting instead for text to automatically drop down as I type. https://i.stack.imgur.com/ZTLYu.p ...

Utilizing HTML5 for page-relative translation

Is there a way to apply a translate to an element so it moves to a specific point on the page instead of relative to its starting position? For instance, in the code snippet below, the "card" elements will move 50, 100 relative to their initial position. ...

The curious case of jQuery.parseJSON() failing to decode a seemingly valid Json string on a Windows-based server

I am currently running a WordPress JavaScript function code on a Linux server that also includes a PHP function called "get_form_data". jQuery.ajax({ type: "POST", url: MyAjax.ajaxurl, data: {action: "get_fo ...

Identify the Google Maps Marker currently displayed on the screen

Is there a way to generate a list of markers within the zoom range for Google Maps, similar to the functionality on this site I'm curious if I can achieve this using jQuery or if there is a built-in function for Google Maps v3? Thank you! ...

Extract the Top X elements from a multidimensional array

Consider an Array that consists of nested arrays: [ ["2000-01-01", "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1a9a8abe091b6bcb0b8bdffb2bebc">[email protected]</a>", 1, 9, 338], ["2000-01-01", "<a href="/ ...

Troubleshooting AngularJS POST Request Error with Request Body

I am a beginner in AngularJs and I am trying to make a post request to a server with enum form. Currently, I have the following JavaScript code: function completeTaskAction2($scope, $http, Base64) { $http.defaults.headers.common['Authorization'] ...

Button for searching through the Bootstrap navigation bar

I'm currently working on adding a search menu to the navbar in two different designs - one for screens below 767px and another for screens above 767px. Although I have been successful in expanding the search bar, I am facing issues with the correct p ...

The input box refuses to accept any typed characters

I encountered a strange issue where the input box in the HTML was not allowing me to type anything. const para = document.createElement('p') const innerCard = document.getElementsByClassName('attach') for(let i = 0; i < innerCard.l ...

What is the best way to deliver an HTML document in Express from a directory that is one level higher than my server folder?

I am facing an issue while trying to access an HTML file from my main directory through my Express server, which is located one level deeper in the server folder. Below is the configuration of my server code: const express = require('express') ...