Ways to dynamically insert new pages | displaying multiple pages within a single page

Is there a way to make multiple 'pages' appear dynamically within a wrapper? For example, if I have a website with a text area where users can enter information, I want that information to be displayed in a div with fixed dimensions. When the content exceeds the height of the div, I would like additional divs to be added dynamically to display the overflowed text. This is what I envision:

<div id="app">
  <div style="display: flex; flex-direction: column;">
    <span>
      <strong>Add text:</strong>
    </span>
    <textarea v-model="text" id="textarea" name="textarea" rows="6" cols="50" />
  </div>

  <div v-for="page in pages" :key="page" class="page">
    <span>
      <strong>Page 1</strong>
    </span>
    <div class="editor-area">{{text}}</div>

    <!-- The goal here is to automatically add a 'second page' when the content overflows 
    the first page. The extra text should then flow into the second page.-->

  </div>
</div>

I'm looking for a dynamic solution so users can input as much content as they need.

Here's a simple codesandbox with basic code for reference.

Thank you!

Answer №1

Below is a flawed implementation that requires your attention, presenting two unresolved issues:

  1. An accurate calculation of the character count that fits within your fixed div:

The variable aproxlength should reflect the estimated number of characters accommodated by your fixed div. It's recommended to input an approximate value based on your browsing experience, but keep in mind this can vary across browsers.

To address this problem effectively, consider creating a hidden div and filling it until overflowing occurs. This method provides a precise solution for determining the optimal aproxlength for individual clients. Check out this example implementation using pure JavaScript: Related implementation on pure javascript

  1. A proper method for text splitting:

Avoid blindly splitting the text as demonstrated, as it may result in arbitrary breaks. It's essential to split the text at whitespace or punctuation marks instead.

<template>
  <div id="app">
    <div style="display: flex; flex-direction: column;">
      <span>
        <strong>Add text:</strong>
      </span>
      <textarea id="textarea" name="textarea" rows="6" cols="50" v-model="text"/>
    </div>

    <div v-for="page in pagesc" :key="page" class="page">
      <span>
        <strong>Page 1</strong>
      </span>
      <div class="editor-area">{{text.slice((page-1)*approxlength,page*approxlength)}}</div>

      <!-- Ideally, when the 'text height' exceeds the div's height,
    a new 'second page' should be added. The excess text from the first 
      page must flow into the second page.-->
    </div>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld";

export default {
  name: "App",
  components: {
    HelloWorld
  },
  data() {
    return {
      pages: 1,
      approxlength: 330,
      text:
        "Lorem Ipsum is simply simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic."
    };
  },
  computed: {
    pagesc() {
      return Math.floor(this.text.length/this.approxlength)+1
    }
  }
};
</script>

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

Unable to generate StumbleUpon traffic for a URL through AJAX due to the API returning text/plain

I'm attempting to acquire StumbleUpon views for a specific URL using $.ajax. This is the API I am utilizing: http://www.stumbleupon.com/services/1.01/badge.getinfo?url=http://example.com/ Here is the complete code snippet: $.ajax({ type: "GET ...

What is the method to retrieve the parameter names of a function in TypeScript?

In an attempt to create a wrapper function that takes a function as input and returns a new typed function that allows for both a list of parameters and an object containing parameter names as keys. I have developed the following code, which functions as ...

Utilizing Cordova for Windows 8.1 in Visual Studio 2015 for external image retrieval and insertion into img tag

I am encountering an issue with displaying external images in a Cordova app. While the DOM is functioning correctly, the images are failing to load. I am specifically trying to resolve this for Windows 8.1 only. In my Cordova project for JavaScript, I have ...

Exploring the DOM beyond the ng-app scope

In the scenario of having an angularJS application, the importance of not manipulating the DOM through a service or controller is clear. Instead, using a directive for such manipulations is recommended. But what if the desired modification lies outside the ...

What is the proper sequence for binding jQuery to elements?

I'm currently facing a challenge with using jQuery to link a function to an element. Essentially, I'm loading a series of divs via JSON, with each item in the JSON having an "action" assigned to it that determines which function should be trigger ...

Adding a specific element to an array using JQuery

I am in the process of developing a web application using jQuery to track goals and habits. Users can set a main goal such as 'Discipline' and then add sub-goals or habits related to that main goal (e.g. 'exercise every day'). Organizi ...

Creating Stylish Buttons with Icons in Bootstrap 4

Currently, my goal is to design buttons similar to the ones displayed below using Bootstrap 4: https://i.sstatic.net/fqkeU.png The font awesome icon should be perfectly centered with the text and aligned at the same y position on the screen. Furthermore, ...

Identifying the device name in Safari on iOS 13 despite the inaccurate display of the user agent - a step-by-step guide

Following the release of Apple's iOS 13, I discovered that window.navigator.userAgent in Safari on iPad iOS 13 is identical to that on MacOS. It appears like this: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) ...

Tips for transferring PHP variables into JavaScript variables?

Using PHP variables fetched from a database to populate JavaScript variables is an essential task that needs to be done. One way to achieve this is by defining JavaScript variables as shown in the code example below: var oMain = new CMain({ mon: <?p ...

What is the best way to empty a backbone collection in preparation for loading new data?

Hey everyone, I've been working on a Backbone application that involves adding and deleting or editing images. Currently, I'm using a router to navigate between different sections like the gallery and forms. However, whenever I make changes in th ...

The @output decorator in Angular5 enables communication between child and

Hello fellow learners, I am currently diving into the world of Angular and recently stumbled upon the @output decorators in angular. Despite my best efforts to research the topic, I find myself struggling to fully grasp this concept. Here's a snippet ...

Is there a way to retrieve the final value from the srcset attribute using jQuery?

Here is the code I am working with: <figure class="im-entry-thumb clearfix"> <img width="770" height="480" src="http://sample.com/blog/wp-content/uploads/2017/05/Untitled-1-770x480.jpg" class="attachment-post size-post wp-post-image" alt="" s ...

Printing a Page Using Angular 5

Is there a way to print a specific section of my website with Angular 5? I've searched online for a solution, but it seems like most options are tailored for Angular. Any advice? ...

Making an input field in Vue automatically read-only when a specific value is met

Is it possible to make an input field read-only in Vue.js based on data values? Here's a situation: <select class="form-control" id="selectCategory" :readonly="cat_id >= 1" name="cat_id"> I'm looking for a wa ...

Formatting the numbers for an unordered list located outside of the content

Can the numbers of an ordered list be formatted using the list-position-style set to outside? I am looking for a solution using only CSS since I cannot edit the HTML. Essentially, I want styled numbers with round background to replace the outside numbers. ...

Generate a graph by utilizing $getJSON and ChartJS

I am currently working on creating a bar chart using ChartJS and a JSON file. The data format is provided below, with each object containing information about the station name and arrival time. My goal is to populate an array where the x-axis represents St ...

Error encountered while attempting to import a Bootstrap JavaScript file in webpack

I am currently working on a Gridsome project (v0.7.23) where I have loaded the Bootstrap framework via npm. In this project, I am using node v14.18.0 through nvm. However, when attempting to import a Bootstrap JS component (specifically 'collapse&apo ...

Obtain user input from a form and assign it to a variable in a jQuery AJAX

How can I pass the value of an HTML Input Form to a jQuery AJAX call's URL as `amt` in `url: "http://localhost:8080/orderNo?amount=" + amt,`? The input value logs to the console when calling getAmtValue(), but I'm struggling to access the `amt` ...

Facing an error with Angular.js

In my current project, I am faced with the task of uploading weather data from an .xls file to an SQL database. This is my first experience working with AngularJS, so I followed a tutorial and everything appeared to be running smoothly. Here is my code: ...

Chrome only triggering jQuery scroll event once

I am currently developing a jQuery hover scroll navigation feature. I am utilizing the scrollLeft and scrollTop methods along with the .mouseover() and .mouseout() functions. Everything appears to be functioning correctly, except for in Chrome/Safari where ...