Ensure that the view remains consistent while navigating a table that contains expanding concealed information

My table is dynamically populated with data within a div that has overflow: scroll and height set properties. The issue I face is that the data populates from the top, making it difficult to keep track of specific rows when scrolling within the #container div. In my case, the data being populated is quite complex and at random rates (it's live chat data aggregation).

I am looking for a way to keep the content static while freely scrolling through the table. Given that the app is already quite intricate, I would prefer any native CSS solutions to address this challenge.

Fiddle: https://jsfiddle.net/iamyojimbo/HB7LU/12102/

JS

var myApp = angular.module('myApp',[]);
function MyCtrl($scope, $interval) {
    $scope.data = [];

    var c = 0;
    $interval(function(){
        $scope.data.push({value:c});
        c++;
    }, 150);
}

HTML

<h1>Data</h1>
<div ng-controller="MyCtrl">
    <div id='container'>
        <table>
            <tbody>
                <tr ng-repeat="element in data | orderBy : '-value'">
                    <td>{{element.value}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

CSS

#container {
    height: 295px;
    overflow:scroll;
}

Answer №1

HTML

<div ng-controller="MyCtrl">
    <div style="height: 200px;overflow-y:scroll;background-color:#ccc;"id='container'>

        <table>
            <thead>
                <tr>
                    <th>Index</th>
                    <th>Data</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="element in data">
                    <td>{{element}}</td>
                </tr>
            </tbody>
        </table>

    </div>
</div>

Script

var myApp = angular.module('myApp',[]);


function MyCtrl($scope, $interval) {
    $scope.name = 'Superhero';
    $scope.data = [];

    var c = 0;
    $interval(function(){
        $scope.data.unshift(c);
        c++;
        fixScroll();
    }, 500);
    fixScroll();
}

var oldheight = document.getElementById('container').scrollHeight;
function fixScroll(){
    var newheight = document.getElementById('container').scrollHeight;
    var newAddition = newheight - oldheight;
    document.getElementById('container').scrollTop = document.getElementById('container').scrollTop + newAddition;
    oldheight = document.getElementById('container').scrollHeight;
}

Access the live demo here: https://jsfiddle.net/rdkha9jk/

To enhance the functionality, consider implementing a condition to prevent unnecessary scroll adjustments when at minimum or maximum values. With this adjustment, allow the content to populate naturally and observe the specified behavior.

Answer №2

Although CSS may not be the solution to your problem, implementing JavaScript should prove to be relatively simple if I understand correctly. There are multiple ways to achieve this.

One of the simplest methods would involve using "element.scrollIntoView()" on the newly generated element.

To see a functioning solution, you can view the modified fiddle here: https://jsfiddle.net/HB7LU/12103/

JavaScript

var myApp = angular.module('myApp',[]);

//This function was added to locate the last child element in the table and scroll to it.
function scrollToElement() {
  //Retrieve the last element of the table and scroll it to the bottom of its container.
  if (document.getElementById('test').lastElementChild != null) {
    document.getElementById('test').lastElementChild.scrollIntoView(false);
    //false scrolls the bottom to the bottom. true scrolls top to top.
  }   
}

function MyCtrl($scope, $interval) {
    $scope.name = 'Superhero';
    $scope.data = [];

    var c = 0;
    $interval(function(){
        $scope.data.push(c);
        c++;

        //Waiting for Angular to complete its task...
        window.setTimeout(scrollToElement, 0);
    }, 500);
}

HTML

<div ng-controller="MyCtrl">
    <div id='container'>
        <table>
            <thead>
                <tr>
                    <th>Index</th>
                    <th>Data</th>
                </tr>
            </thead>
            <!-- An id was added to the following element for identification -->
            <tbody id="test">
                <tr ng-repeat="element in data">
                    <td>{{element}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

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

How does the call method on array.prototype.includes work with arguments x and y?

Curious about the functionality of array.prototype.includes.call(x, y);. Discovered that includes() confirms if an array has the specified value and provides a true or false result. Learned that call() invokes this alongside any optional arguments. The ...

Is it possible to adjust an inline CSS style upon clicking a link?

I currently have a form that is hidden using inline styling style="display: none;" Is there a way to dynamically update this style to style="display: inline;" once a specific link on the page is clicked? ...

Troubleshooting: @HostListener for window scroll event not functioning as expected

Having trouble creating a sticky header that stays fixed when scrolling down in an Angular 4 application. The scroll event is not being detected. The header is located in the layout component, while the content I want to be scrollable is placed in the rou ...

The error message "Cannot read property '$scope' of undefined" indicates that there is an issue

After receiving HTML content in an Angular app, I inject it into the HTML document but struggle to retrieve it. For example, you can see a minimized code on plunker here and in JavaScript here Below is my controller: class ReadCtrl constructor: (@$sco ...

Determine if an array of objects within React JS contains additional objects

I need assistance in displaying the <div className="songs-list-header-col">Album</div> only if the tracks array contains the artist property as an object. In cases where the artist is not an object, I do not want to display the <di ...

Using just one "nav.php" file for both the homepage and subdirectories

Looking to enhance the efficiency of my websites, I have implemented a PHP file for navigation, footer, sidebar, and other elements. However, this current method involves using two separate files: one for the homepage and another for the remaining pages wi ...

Can you explain the significance of the error message stating "XMLHttpRequest.timeout cannot be set for synchronous http(s) requests made from the window context"?

I'm experiencing some timeouts with a synchronous XML HTTP request in Safari on my Mac. In an attempt to fix this issue, I added a timeout setting like this: req.open(this.method, fullURL, this.isAsync); req.setRequestHeader('Content-Typ ...

How come the margin-bottom is displaying as the margin-top?

When I attempt to add margin-bottom on a div inside its parent, it seems to display as the parent's margin-top. Why is that? HTML: <div id="wrapper"> <div id="content"> <div id="box"></div> </div> </d ...

Tips for creating a 3D illusion by coding a div or object to rotate around another object or div

I have reached a technical and intellectual roadblock. The issue at hand is this: I need the text to encircle an image and create the illusion that it is moving in front of or behind the image, while keeping the image static. Currently, 1) the rotating te ...

The error message javax.ws.rs.NotFoundException: HTTP 404 Not Found pertains to Angular 1.5

Currently, I am working with Angular 1.5 code for a client application: var getGroupForUser = function () { var deferred = $q.defer(); $http.get(env.domain+'/Voices/authorize').then( function successCallback(respo ...

What could be causing the disappearance of my <Div> when applying a Class to the contained <span>?

My goal is to showcase a variable on my HTML page using CSS for a Graphical display in OBS streaming software. Whenever I apply the class .wrappers to the span id p1Name (and p2Name), the text disappears from the HTML output in OBS. I'm puzzled as to ...

Using TypeScript to define callback functions within the Cordova.exec method

I'm encountering an issue with the TypeScript definition for Cordova. The codrova.d.ts file doesn't allow for any function arguments in the success-callback and error-callback. To better illustrate my problem, here's a small example: Here ...

The equal height feature in Bootstrap 4's card-deck is not functioning as expected

I am having an issue with the card-deck property where it is not being applied correctly. The .card-deck class should create a grid of cards that are equal in height and width, and the layout should adjust automatically as more cards are added. Here is th ...

Issue encountered when attempting to update a specific element within an array using Mongoose

Looking to update a specific object within an array. { "_id": "543e2f8e769ac100008777d0", "createdDate": "2014-10-15 01:25 am", "cancle": false, "eventDateAndTime": "2014-02-12 12:55 am", "eventstatus": true, "userPhone": "44444444 ...

What could be the reason behind the Vue property being globally undefined in the component at the time of mounting?

Creating a custom Vue plugin was essential for me, which establishes a global property on Vue in this way: function (Vue, options) { Vue.$detector = new TranslatableStringDetector(); } Utilizing it within a computed property in my component is done l ...

What is the process for incorporating styles into an external widget?

I am currently navigating my way through NextJS and attempting to incorporate a bespoke stylesheet into my code using a third-party widget. Unfortunately, I have hit a roadblock in this process. The names I have assigned to the style sheet align with what ...

I encountered an error in my Rails 4 project where I got a NoMethodError stating "undefined method `id' for nil:NilClass" in my create.js

When I click a button, I'm attempting to display a partial. It seems like I am making an obvious mistake... and I suspect it's not related to the following code snippet: $('#modrequest').empty(); $('#modrequest').html("<%= ...

CSS media queries with precise inequality restrictions

Imagine the following scenario: @media only screen and (max-width: 600px) { nav { display: none; } } @media only screen and (min-width: 601px) { nav { display: block; } } This setup can lead to undefined behavior for a wid ...

jquery plugin causing issues with bootstrap button functionality

Currently, I am utilizing the jQuery form plug-in to post my form in an Ajax way. The post function is functioning perfectly with the default button. However, it seems to encounter issues when I try to use a custom Bootstrap button as shown below. Could so ...

Tips for importing extensions with Rselenium

I could use some assistance in understanding how to activate a chrome extension using RSelenium. The extensions are visible in the browser tabs but are not automatically loaded when working with RSelenium. https://i.sstatic.net/QKqVg.png ...