Scrolling horizontally to display dynamic columns based on a search query in AngularJS

I am facing a challenge with implementing search functionality in a table that has multiple dynamic columns with a horizontal scrollbar. The goal is for users to be able to search for a specific column name or data, and if a match is found, the scrollbar should automatically scroll to that particular column.

While I have attempted to implement this feature, I have been unable to find a solution without using any third-party libraries or tools. Ideally, I would like to achieve a similar functionality to what is seen in the Google Chrome browser, where searching directs the user to the relevant column, but without relying on browser-search or external tools.

var app = angular.module("myapp", []);
app.controller("ListController", ['$scope', function($scope) {
    $scope.personalDetails = [
        {
            'fname':'Abc',
            'lname':'Abc',
            'email':'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89c8ebeac9c8ebeaa7eae6e4">[email protected]</a>',
            'demo': 'xyz.com',
            'Pnumber': '9892XXXXXX',
            'Address': 'XYZ'
        }
        ];
}]);
.btn-primary{
    margin-right: 10px;
}
.container,.search{
  margin: 20px 0;
}

form{
    width: 200px;
    overflow-y: hidden;
    overflow-x: scroll;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myapp" ng-controller="ListController">     
    <div class="container">
        <div class="row">
            <div class="col-md-8">
               <div class='search'>
                 <input type="search" placeholder="search column" required/>
               </div>
                        <form>
                            <table class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th><input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" /></th>
                                        <th>Firstname</th>
                                        <th>Lastname</th>
                                        <th>Email</th>
                                        <th>demo</th>
                                        <th>Phone number</th>
                                        <th>Address</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr ng-repeat="personalDetail in personalDetails">
                                        <td>
                                            <input type="checkbox" ng-model="personalDetail.selected"/></td>
                                        <td>
                                            <input type="text" class="form-control" ng-model="personalDetail.fname" required/></td>
                                        <td>
                                            <input type="text" class="form-control" ng-model="personalDetail.lname" required/></td>
                                        <td>
                                            <input type="email" class="form-control" ng-model="personalDetail.email" required/></td>
                                             <td>
                                            <input type="text" class="form-control" ng-model="personalDetail.demo" required/></td>
                                             <td>
                                            <input type="text" class="form-control" ng-model="personalDetail.Pnumber" required/></td>
                                             <td>
                                            <input type="text" class="form-control" ng-model="personalDetail.Address" required/></td>
                                    </tr>
                                </tbody>
                            </table>

                        </form>
                    </div>
                </div>
            </div>
</body>

Answer №1

To achieve this, you can utilize a combination of jQuery functions along with AngularJS. Check out this JSFiddle example

1) First, define a container element where the scrolling will occur.

2) Locate your target element and scroll to it using the following code snippet:

In your controller:

$scope.textChanged = function () 
 {
    var $container = $('#myform'),
    $scrollTo = $('th').filter(function() {
    return $(this).text() === $scope.filter;});

    if($scrollTo.length > 0)
    {
        $container.scrollLeft(
        $scrollTo.offset().left - $container.offset().left + $container.scrollLeft());
    }
    else
    {
       $container.scrollLeft(-$container.scrollLeft());
    }

 }

Note: For partial matching, use the following line of code:

$scrollTo =  $('th:contains(' + $scope.filter + ')');

Next, in your HTML file:

<input type="search" ng-change='textChanged()' ng-model='filter' placeholder="search column" required/>

Add an ID to the form for correct location targeting:

<form id='myform'>

Finally, make sure to add the $scope.filter variable to your controller like so:

app.controller("ListController", ['$scope', function($scope) {
    $scope.filter = '';

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 best way to set a newly selected HTML option as the first choice?

I'm facing a simple problem in JavaScript that I can't seem to crack. Admittedly, I am new to working with JavaScript. My issue lies with sorting a dropdown list in alphabetical order while also keeping the selected value at the top. Whenever a ...

Chrome Flexbox Hacks: Controlling the Size of Nested Items

How can I ensure that an element nested within a flexbox member in Google Chrome is limited to the size of its container? For example, consider the following scenario: <div style="display:flex; flex-direction:column; height:100vh;"> <div style= ...

The functionality of the custom file upload button is experiencing issues on Microsoft Edge

I've been working on creating a unique custom image upload button that functions perfectly in Chrome, Firefox, and Opera based on my testing. However, I'm facing an issue where it doesn't work properly in Microsoft Edge. Feel free to check ...

What is the best way to include a pound sign in a cfoutput tag?

My challenge is that I need to display the following HTML tags after receiving data from an AJAX query. The problem arises because in cfml, any string starting with a "#" is considered as an identifier, resulting in an error. <cfoutput> ...

Customize the color of the horizontal line in React using dynamic properties

Is there a way to customize the color of an <hr /> element in a React application? If we were to use a functional component to accomplish this, how would we go about it? ...

Storing information in local storage as JSON using AngularJS

I am working on a form with the following fields: <form ng-submit="addState()"> <input ng-model="text1" type="text"> <input ng-model="text2" type="text"> <input ng-model="text3" type="text"> ...

The button must be programmed to remove a specific item from the server

I am currently developing an application to monitor my expenses using javascript, nodejs, express, and handlebars as the templating engine. Within the app, I have a "list" div that displays all of my expenses. (There is an add button next to the list, not ...

The third @media query for max-width is not functioning as expected in the CSS

Looking to create a responsive card section on my website for various screen sizes. The goal is to display 5 cards per row when the screen width is greater than 1300px, maintain 3 cards per row from 1024px to 1300px, and then switch to only 2 cards per row ...

Creating a flowchart of a finite state machine within a browser-based software platform

If you're curious about the core logic behind my application, check out this link for a great explanation: Code Golf: Finite-state machine! I plan to implement this logic to create finite state machines on my web app using a library. I've been ...

Challenges arising from the offset in an overflowing Div

Encountering issues with JQuery Offset when utilized within a div that has a fixed height and overflow. This particular div contains two columns, a main column and a sidebar. The objective is to have one of the sidebar divs scroll within its container unt ...

Adjust the text placement on the toggle switch to turn it On or Off

I am looking to create a toggle switch with a small size. I found some code on Stack Overflow that I tried but it didn't work as expected. The code snippet I attempted to use is from another helpful post. My goal is to have the "on" text align to th ...

Show a picture upon hovering the button

Welcome to my website. My goal: Show an image when a user hovers over the links. I must be making some silly error, but I can't pinpoint it. This is what I've attempted so far: HTML <ul class="nm"> <li><a href="#">Cork& ...

Angular q.all: comparing immediate and delayed responses

Seeking advice on methodology: I currently utilize $q.all to handle multiple promises in a single return, processing all results as one request. For instance: $q.all([promise1(),promise2(),promise3(),promise(4),promise5()])..then(function(response){ ...} ...

Adjusting the image width to a set height requirement

When working with images, it can be tricky to maintain the aspect ratio. For example, if you have an image set to a specific height like 500px within a div that is 960px wide, the image may stretch to fill the width. This can distort the image and give ver ...

Adjust the color of a div's background when clicked by solely using CSS

I'm looking to change the color of a specific div when it's clicked on among three different ones. The goal is to indicate which one is active without reverting back to the original color once the mouse is released, similar to a focus effect. I&a ...

When a PHP if statement is triggered by reading a text file, it activates a checked

I am working on a PHP project that involves an on-off button and a txt file. The goal is to have the button disabled (with 'unchecked' echoed) when the value in the txt file is true, and enabled (with 'checked' echoed) when the value is ...

Begin ng-repeat in AngularJS with no initial elements displayed

When using Angular JS, the first value needs to be left empty. <thead> <tr ng-repeat="(key, value) in reports"> <th>&nbsp;</th> <th ng-if="$parent.$index==0" ng-repeat="data in value">{{$index+1}}</ ...

Is there a way to retrieve the dates from last month using jQuery?

Currently, I am unable to select past dates with the provided code. How can I adjust it to allow for selecting dates from previous months? let firstDay = new Date(); $('#date_filter_startmonthly').datepicker({ startDate: firstDay, endDate: ...

Is there a way to display the button only when the user is able to enter an email in the input field?

I have a form for users to update their profile and a button to delete their account. I want the delete account button to only be visible if the user enters their email. $(document).ready(function() { var user_email = $('input[name="emp_email"]&a ...

What is the best way to retrieve the value of the nearest text input using JavaScript?

I am currently working on designing an HTML table that includes a time picker in one column for the start time and another time picker in a separate column for the end time within the same row. My goal is to retrieve both values (start time and end time), ...