Displaying or hiding table columns in Vue.js Element-UI el-table based on screen width: a step-by-step guide

Here is my code snippet for generating a table:

<el-table :data="tableData" empty-text="No data to display :( " v-if="window.width > 855">
  <el-table-column
   v-for="column in tableColumnsMd"
   :key="column.label"
   :min-width="column.minWidth"
   :prop="column.prop"
   :label="column.label"
  >
</el-table-column>

Javascript part:

data() {
      return {
        tableColumnsMd: [
          {
            prop: "name",
            label: "Name",
            minWidth: 150,
            show:true,
          },
          {
            prop: "status",
            label: "Status",
            minWidth: 200,
            show:false,
          },
          {
            prop: "registrationDate",
            label: "Registration Date",
            minWidth: 150,
            show:false,
          },
          {
            prop: "accountId",
            label: "Account ID",
            minWidth: 100,
            show:false,
          },
        ],
        //synchronized data with the table
        tableData: [],
      };
    },

Total number of columns: 4.

I would like to have a responsive design: if the screen width is less than 853px, then only display the first 3 columns or maybe show the first and last column instead.

Thank you!

Answer №1

If I were to approach it, I would do the following:

  1. First, create a computed property that checks for the condition:
computed: {
 shouldDisplay() {
   return window.innerWidth > 853
 }
}

Next, you can use this computed property in your template using either v-show (to always render and hide based on condition) or v-if.

Here's an example implementation:

<el-table :data="tableData" empty-text="No data available :( " v-if="shouldDisplay">
  <el-table-column
   v-for="column in tableColumnsMd"
   :key="column.label"
   :min-width="column.minWidth"
   :prop="column.prop"
   :label="column.label"
  >
</el-table-column>

Note:

For this to be reactive, make sure to add an event listener as explained in this post:

Computed property reactive to window.innerwidth in VueJS

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

Learn the process of creating a unique item layout to suit your preferences

Exploring the use of div to create the desired layout but struggling to achieve my goal .skills-container { height:50px; /* aiming for around 50px */ padding:5px; } .skill-pic { width:48px; height:48px; } .skill-content { } .skill-content p { } .progres ...

Is there a way to organize an array of elements into three columns using PHP?

My array of menu items is being displayed on the screen in 3 columns, but I want them to appear differently without altering the HTML structure. Currently, they are listed like this: 1 2 3 4 5 6 7 8 9 I would like them to display as follows: 1 4 7 ...

Unable to set the width for a div element within the bootstrap grid layout

When trying to set the width of a div tag to 100% within a bootstrap grid system (col-lg-3), I noticed that the div ends up taking the full width of the browser instead of just 100% of its parent(col-lg-3). .sidebar { color: #fff; f ...

Implementing Multiple Shared Footers in Rails 3

Looking for a solution to display unique footers in Rails? For instance, displaying one footer on the homepage and another footer on the rest of the site. Has anyone successfully implemented this before? I was considering using an if statement based on th ...

Guiding you to choose a specific child class by identifying its parent class in CSS and jQuery

Hey there! I'm currently working on a website that allows users to post content. I want to ensure that the site is mobile-friendly, so I need to make some adjustments with media queries. Unfortunately, I've run into an issue where media query CS ...

Boosting Your Theme - Eliminating Redundant Styles

Currently, I am facing more of a best practice issue rather than a coding one. I am in the process of creating a custom bootstrap theme inspired by https://github.com/HackerThemes/theme-kit. Although I have a functional theme that I am satisfied with, I am ...

`Vue.js child component not reflecting updated prop value`

Created a functionality within the App.vue parent component to communicate with all child components, instructing them to close any open modals. Additionally, implemented a feature for a child component to notify the parent to hide the overlay when the mai ...

Is there a way to ensure that the text stays positioned behind the initial image even as the window size is adjusted

<html> <head> <title> Example </title> <style> img:hover {opacity : 0.3;} img {z-index : 1; height : 33%; width : 33%; } .first { position : absolute; top: 0%; left: 0%;} .second {position : absolute; top: 0%; left: 33%; ...

Exploring Nuxt.js: Enhancing User Experience with Dynamic Page

Within my nuxt.config.js file, I included the line loading: '~/components/LoadingBar.vue'. After launching the site, I noticed that my custom page transition only works when the first page visited is anything other than the home page. For insta ...

Having difficulty changing the visibility of a div with Javascript

I've developed a piece of vanilla JavaScript to toggle the visibility of certain divs based on the field value within them. However, I'm encountering an issue where trying to unhide these divs using getElementById is resulting in null values. D ...

ElementUI datetime picker gets refreshed when popper is closed

I've been utilizing ElementUI in combination with Vue.js. My goal is to utilize the el-date-picker and only capture the input update once the picker's popper closes, excluding any earlier updates triggered by selecting a date using the mouse cli ...

Error: An uncaught exception occurred when trying to access a seekable HTML5 audio element, resulting in an IndexSize

While trying to utilize the HTML5 API along with SoundManager2, I encountered an issue. My goal was to determine the duration of a song in order to create a progress bar that would update as the song played. However, every time I attempted to retrieve the ...

Drag the label into the designated paragraph to copy the text. Click on the specific point to transfer the text

How can I copy the text from a label to a specific point by dragging it? <label id="text_to_be_copied" > i am a student </label> Below is a paragraph where I want to paste the copied text: <p> this is the content where I want to copy t ...

Adding a new row to an HTML table using JavaScript

In the code snippet below, I am facing an issue with adding a row to an HTML table and inserting it into a database. I have tried using JavaScript to add the row in order to avoid postback, but so far, I have been unsuccessful. Can someone assist me in t ...

The container holding the inner elements is slightly oversized by a couple of pixels

Check out this Plunker example. Hello everyone, I am currently working on fitting two inner divs (a title bar and a canvas) inside an outer div. These inner divs are generated dynamically and have specific fixed dimensions. Below is a sample code snippet ...

"Step-by-step guide on creating a web app with a transparent background while simultaneously running other processes

Would love to create a web application for real-time debugging similar to "android->developer options->show touch data" or "android->developer options->show CPU usage". Unfortunately, I have not been able to find a suitable method using HTML5 ( ...

Add a CSS and jQuery hover effect to display text over an image, requiring users to click twice on their mobile device

I have a bunch of panels for different categories that change the background image when hovered over, display some introductory text, and lead to a URL when clicked. However, on mobile devices, you need to tap the panel twice to activate the URL. What I&a ...

Turn an entire div into a clickable element with a functional :active CSS rule that is compatible with IE10

I am currently working on designing a clickable panel for an html app that will include multiple text elements and images. Based on my understanding, this is typically achieved using a div. An example of this would be: <div class="myButton"> &l ...

Storing formatted user input in an array with VueJS: A step-by-step guide

Looking for assistance that relates to the following question Vue.js: Input formatting using computed property is not applying when typing quick I am facing a challenge in extracting formatted values from text inputs and storing them in an array. I intend ...

Error Alert: Module 'app.vue' not found in Visual Studio Code

When I have the following line of code in my App.vue import TestComponent from '@/mytest/components/TestComponent.vue' I encounter an error message from Vscode, specifically the vetur plugin, stating that it cannot find module @/mytest/componen ...