Slot context remains unclaimed

Context not available...

Vue.component("custom-table", {
    name: 'CustomTable',
    template: "#custom-table",
    created: function() {
      console.log('Created', this.rows);
    },
    mounted: function() {
      console.log('Mounted', this.rows);
    },
    data: function() {
    },
    props: {
        rows: Array
    }
});
<script type="text/x-template" id="custom-table">
    <table>
        <thead>
            <slot name="head"></slot>
        </thead>
        <tbody slot name="body">
        </tbody>
    </table
</script>


<custom-table :rows="myRows">
  <thead slot="head">
    <tr>
      <th>Name</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody v-for="item in myRows">
    <tr slot="body">
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </tbody>
</custom-table>

I encountered this issue...

Property or method "item" is not defined on the instance but referenced during rendering. Please ensure reactive data properties are declared in the data option. (located in the root instance)

Also, myRows contains

[
  { name: 'Name1', age: 20 },
  { name: 'Name2', age: 30 }
]

Answer №1

It's important to note that using the thead and tbody elements outside of a table is not recommended. Browsers will often try to correct this issue during the parsing process. To avoid any potential problems, it's best to stick to the standard structure of a table. Here's an example of how you can create a custom table using Vue.js:

Vue.component("custom-table", {
  name: 'CustomTable',
  template: "#custom-table",
  props: {
    cols: Array,
    rows: Array
  }
})

new Vue({
  el: '#app',
  data: {
    myRows: [
      {name: 'Name1', age: 20},
      {name: 'Name2', age: 30}
    ]
  }
})
<div id="app">
  <custom-table
    :cols="['Name', 'Age']"
    :rows="myRows"
  ></custom-table>
</div>

<script type="text/x-template" id="custom-table">
  <table>
    <thead>
      <tr>
        <th v-for="col in cols">{{ col }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="it in rows">
        <td>{{ it.name }}</td>
        <td>{{ it.age }}</td>
      </tr>
    </tbody>
  </table>
</script>

<script src="https://unpkg.com/vue"></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

The null object does not have a property addEvenListener and therefore cannot be

My goal is to develop a simple single-page application without using any frameworks, focusing on providing users with tutorials on specific subjects. I am encountering an issue with the javascript code for my page, receiving the following error: Uncaug ...

Tips on restricting users to choose dates that are later than the current date

Currently, I am working with Vue3 using the options API. After reviewing this StackBlitz, my question is regarding how to correctly set the :max value for a date-picker. Even though I have assigned :max as new Date(), I am still able to select dates that ...

Ways to retrieve the initial value and proceed with a subsequent subscription method

I have created a basic angular 7 web application with Firebase database integration. In my code, I am attempting to store the initial list in an array using the subscribe method and then display that array using console.log(). However, there seems to be a ...

NextJs's React-Quill is unable to effectively highlight syntax using the highlightJS library

I have been working on a NextJs application (blog) that utilizes react-quill as a rich text-editor. As part of my setup, I am making use of the Next custom 'app' feature, where my UserProvider component wraps everything to provide global access t ...

Having trouble retrieving information from .pipe(map()) method

Encountering some issues with Observable handling. I have a function that returns an Observable. public getData(userId) { const data = this.execute({userId: userId}); return {event: "data.get", data: data} } private execute(input: SomeDt ...

Switch up your text display using JQuery and toggle between different texts

I have implemented a jQuery script to toggle the display of certain paragraphs when the "More" link is clicked. However, I am facing an issue where the link always displays "More" even after the content has been expanded. I want it to change to "Less" once ...

Interact with visible elements by automating mouse clicks with puppeteer

When attempting to click on various elements within a page, my goal is to do so only if they are visible. While achieving this in selenium with the is_displayed method was simple, I have struggled to find a similar approach in puppeteer. I attempted to imp ...

Creating a task management application using AXIOS and VUE for easy data manipulation

I am currently working on a small app to enhance my skills in VUE and Axios. However, I am facing a roadblock when it comes to the update functionality. I am struggling to pass the ID to the form for updating and despite watching numerous tutorial videos ...

What is the best way to use jest/enzyme to determine if a method from an imported class was called in a component

Here is the code snippet from my component: export class VehiclesComponent extends React.Component { constructor(props) { super(props); this.state = { data: [], }; autoBind(this); } componentDidMount () { this.fetchData(); ...

javascript change string into an array of objects

let dataString = "{lat: -31.563910, lng: 147.154312};{lat: -33.718234, lng: 150.363181};{lat: -33.727111, lng: 150.371124}"; let dataArray = dataString.split(';'); Produces the following output: let dataArray = [ "{lat: -31.563910, lng: 147 ...

Tips on creating animations for elements that are triggered when scrolling and become visible

Is there a way to animate an element when it becomes visible on scroll, rather than at a fixed position on the page? I'm currently using code that triggers the animation based on an absolute scroll position, but I'm looking for a more dynamic sol ...

Is there a way to showcase a PDF file using pdftron through npm?

pdftron/webviewer has been successfully installed "dependencies": { "@pdftron/webviewer": "^7.3.0", "body-parser": "^1.19.0", "express": "^4.17.1", ...

Is there a way to shift this modal to the right?

I am facing an issue with aligning a button modal to the right side. I attempted using bootstrap classes like : float:right, mt:20 Unfortunately, these did not have the desired effect. Below is my code snippet: <b-modal ref="my-modal" hide-fo ...

When implementing jQuery for scrolling on a website, touch gestures may become unresponsive on Safari for iOS devices

While developing a custom website with Angular, I encountered an issue specifically with Safari on iOS. The website is a single-page app with multiple menu sections that trigger a scroll animation when selected using jQuery. Everything was working smoothly ...

Guide on retrieving just the time from an ISO date format using JavaScript

let isoDate = '2018-01-01T18:00:00Z'; My goal is to extract the time of 18:00 from the given ISO date using any available method, including moment.js. ...

Customize your Angular UI Bootstrap Datepicker with unique buttons - here's how!

Currently, I have a datepicker with clear and close buttons. I tried using the append function to add more buttons to this datepicker, but it didn't work because the content is not loaded until we click the icon since it's a popup datepicker. Is ...

The dynamic trio of Laravel, inertiajs, and vuejs

I am currently working on a project that involves laravel, inertiajs, and vuejs. Whenever I attempt to delete a user from the database, I consistently encounter a 404 | NOT FOUND error. My goal is to successfully delete the user by clicking a button using ...

Ajax request in Rails not receiving a response from the controller

After troubleshooting a simple GET request to the controller action, I confirmed that the request is being made successfully and there are no issues with the controller executing the action. However, I am not receiving any response data. $.ajax({ url: ...

Enhance your JQuery skills by implementing variables within the .css() function

Attempting to randomly assign values to an image's left and right properties using JQuery. Here is the code snippet: var sides = ["left", "right"] var currentSide = sides[Math.floor(Math.random() * sides.length)]; $("#"+currentImage).css({currentSide ...

Error message: The protocol "https:" is not compatible with this function. Please use "http:" instead

I decided to use IBM Bluemix for my school project, where I am working on creating a web service. For my project, I need to fetch JSON data from an API in order to utilize the provided information. Currently, I am utilizing the http get method to retrieve ...