vue implementing autoscroll for long lists

I am looking to implement an autoscrolling log feature on my webpage that is dynamically fetched from a REST endpoint. To handle the potentially large size of this log, I decided to use vue-virtual-scroll-list. Additionally, I wanted the log to automatically scroll to the bottom unless manually scrolled upwards, in which case I wanted the scroll position to be maintained. This functionality was achieved using vue-chat-scroll. However, after reaching a certain number of entries, the scrollbar became unstable and no longer synced with the content or auto-scrolled to the bottom.

Vue.component('log', {
  data: function() {
    return { msgs: [] }
  },

  props: {
    id: { type: String, required: true },
    length: { type: Number, required: true },
    refreshRate: { type: Number, default: 1000 }
  },

  template: 
      '<virtual-list :size="40" :remain="length" class="list-unstyled" :ref="id" v-chat-scroll="{always: false}">\
        <li v-for="msg in msgs" :key="msg.id" :class="logColor(msg.severity)">\
          <pre>[{{ shortTimestamp(msg.timestamp) }}]: {{ msg.message }}</pre>\
        </li>\
      </virtual-list>',

  methods: {
    fetchLogs: function(){
      var session = this.id;
      var start = -this.length;
      if (this.msgs.length > 0)
        start = this.msgs[this.msgs.length - 1].id;

      var vue = this;
      $.post(getUrl("/sessions/" + session + "/log"), JSON.stringify({
        start: start
      })).then(function(data) {
        for(var msg of data){
          vue.msgs.push(msg);
        }
      });
    },

    shortTimestamp: function(time) {
      var fulldate = new Date(time);
      return fulldate.toLocaleTimeString();
    },

    logColor: function(severity) {
      switch (severity) {
        case "Trace":
          return "list-group-item-light";
        case "Debug":
          return "list-group-item-dark";
        case "Information":
          return "list-group-item-info";
        case "Notice":
          return "list-group-item-primary";
        case "Warning":
          return "list-group-item-warning";
        case "Error":
          return "list-group-item-danger";
        case "Critical":
          return "list-group-item-danger";
        case "Fatal":
          return "list-group-item-danger";
      }
    }
  },

  mounted: function() {
    setInterval(function () {
      this.fetchLogs();
    }.bind(this), this.refreshRate); 
  }
})

Is there any solution to rectify this issue?

Answer №1

If you're looking for a solution, I highly recommend checking out Vuescroll.

  • One great feature of Vuescroll is its handle-resize event which allows you to detect changes in content size and respond accordingly.
  • In addition, Vuescroll provides a helpful scrollTo API that enables you to easily scroll to specific locations on the page.
  • To address concerns about dealing with excessive data, you can manually adjust your data array by utilizing the handle-scroll event based on your unique requirements.

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

The unzipper will disregard any empty directories within the file

I am a Japanese Web Developer. Unfortunately, I struggle with English. https://www.npmjs.com/package/unzipper This library is currently in use by me. Below you will find my code snippet: // unzip module import fs from 'fs-extra' import unzip ...

Angular.js is experiencing difficulties when using the input value attribute in conjunction with ng-model

I've been hard at work on an app that allows users to edit items, with those changes updating in a database. To prevent empty form submissions, I automatically fill the input fields with the current item's information. form.form-update(method="p ...

eliminating and adding a node

Is there a way to replace the existing span elements inside the div (<div id='foo'>) with newly created nodes? I have been looping through all the children of the div, using removeChild to remove each node, and then appending a new node in ...

Instructions for activating column resizing in MUI DataGrid

Is there a way to enable column resizing for users in MUI DataGrid? It's enabled by default on XGrid, but I would like to enable it on Datagrid as well. Any assistance is appreciated. <DataGrid className={classes.table} ...

Unspecified data output from jquery datepicker

I am currently utilizing a datepicker to select a date. I have implemented the jQuery datepicker, however, it is returning an undefined value. Can someone please assist me with this issue? My HTML CODE: <input type="text" class="form-control fromdate" ...

Using a Javascript method to access a sibling property within an object

Is there a way to access a sibling property from a method in JavaScript? This seemingly simple task has proven challenging for me. Take a look at the sample code below. let f = { a: 3, printMyBrother() { console.log(X) } }.printMyBrother f() ...

Retrieve my information stored within a public transportation vehicle

Hello everyone, I am currently working on a Vue project and encountering a problem that I cannot seem to resolve. The issue at hand: I am trying to transfer data between components using a bus that I have defined in my main.js file. However, I am struggl ...

Verifying user login on NodeJS through connection from an IIS-hosted website

I am currently upgrading an outdated CMS system and looking to implement a real-time chat feature. The existing CMS operates on IIS, MSSQL, and PHP. The chat feature will be hosted on a separate Linux box running Node.js and Socket.io After successfully ...

What is the best way to execute code after an element has been included in ngFor in Angular 2+?

Within my component, there is a function that adds an element to an array displayed using *ngFor in the view. After adding the element to the array, I want to target it by ID and scroll to it. However, the issue is that this code runs before the element a ...

Selecting properties from a GeoJSON object on the fly with Leaflet

I am currently attempting to dynamically modify the displayed property on my leaflet map. Below is the code I have been working with: $("#button_thermal").click(function(){ $.getJSON("physicalProperties.geojson", function(data) { var geojson2 = L.geoJson( ...

What is the proper way to safely escape a JSON string for an object that contains user-generated content?

How can I correctly use the variable s for the value-field of an option-element while escaping user input used for key and value? var key='highway'; var value='track'; var s = JSON.stringfy({ [key]:value }, undefined,''); s ...

What is the best way to merge several fetchMore functions within Apollo?

Can Apollo run multiple fetchMores simultaneously? I have a complex hook that executes two queries and combines their results into a single array. This is how it looks: export const useDashboardState = (collection: string) => { // Getting parameters ...

Bypassing redirection in Nuxt for seamless login experience

I've been exploring ways to implement authentication on my Nuxt app, but I'm struggling to find a tutorial that doesn't rely on redirecting to public or private paths. For example: if (user.isLoggedIn()) { redirect('/dashboard&apo ...

Send form data when opening a fresh page with Javascript

In the WordPress build for our company, there is a need to transfer data when previewing a page. To streamline this process, my boss wants to override the default functionality of the preview button by opening a new tab and passing the necessary data throu ...

Is there a possible method to obtain a smartphone number from a website?

Seeking a solution to retrieve the phone number of a device using HTML 5, JavaScript, or similar technology. Recently, I successfully obtained the coordinates of the device by incorporating the following JavaScript code: <!DOCTYPE html> <html> ...

Move through different screens using React JS

Can anyone help me with this issue I'm facing in React Js? I am trying to implement a button that changes to another screen when clicked, but I keep getting an error message: TypeError: Cannot read property 'push' of undefined. I have tried ...

Encountering the 'navigator is not defined' error when attempting to generate a Next JS build

After developing a custom hook in Next JS to retrieve network online status using the JavaScript navigator.onLine property, everything seemed to work flawlessly on my local machine. However, upon running npm run build to compile the project, I encountered ...

Issue with generating WebGL shaders

As I embark on creating a basic application in WebGl and JavaScript following online tutorials, I encountered a peculiar issue while working on some fundamental shaders. The function responsible for generating the shader program presents itself as follows: ...

How can I stretch a background image using jquery to cover the entire document instead of just the window

I have implemented a plugin called https://github.com/srobbin/jquery-backstretch to effectively stretch the background image. The problem arises when the content of the page is long enough to create a scrollbar. In this case, while scrolling, the backgrou ...

Guide on retrieving the innerHTML of all <a> tags within a specific span class

<span class="mgen"> <a rel="tag">Action</a> <a rel="tag">Adventure</a> <a rel="tag">Apocalypse</a> <a rel="tag">Fantasy</a> <a rel="tag" ...