Exploring the properties of individual Vue components on a single page with v-for loop

Struggling to render a Vue component in a Rails app by iterating through an array of data fetched via Ajax. The Slim template (index.html.slim) for the index page includes a custom_form_item component, where items represent custom forms data from Rails and each item is a component.

- content_for :head do
  = javascript_pack_tag "index_listing" 
  = stylesheet_pack_tag "index_listing" 

div.container data-behavior="vue"
  <custom_form_item v-for="item in items" v-bind:item="item" v-bind:key="item.id"></custom_form_item>

The script loads index_listing.js.coffee from javascript/packs. The created event loads the data for items.

import Vue from 'vue/dist/vue.esm'
import TurbolinksAdapter from 'vue-turbolinks'
import VueResource from 'vue-resource'
import CustomFormItem from '../components/custom_form_item.vue'

Vue.use(VueResource)
Vue.use(TurbolinksAdapter)

Vue.component('custom_form_item', CustomFormItem)

document.addEventListener('turbolinks:load', ->
  index_list_vm = new Vue(
    el: '[data-behavior="vue"]'
    data: () ->
      return {
        items: []
      }
    created: () ->
      console.log "inside created()"
      this.loadItems()
    methods: {
      loadItems: () ->
        self = this
        console.log "inside loadIems()"
        url = '...'
        self.$http.get(url).then((result) ->
          console.log "Got: "+result.data
          self.items = result.data
        )
    }
  )
)

Turbolinks.dispatch("turbolinks:load")

The component includes a delete button using a method to remove the respective item from items when clicked.

<template>
  <div class="custom_form_list_item">
    <label for="item_id">ID</label>
    <input id="item_id" v-model="item.id" readonly>
    <br/>
    <label for="item_name">Name</label>
    <input id="item_name" v-model="item.name" readonly>
    <br/>
    <div class="btn-group btn-group-toggle" data-toggle="buttons">
      <button type="button" class="btn-warning btn-sm" v-on:click="redirToEditURL(item.id)">
         <i class="fas fa-edit"></i>
      </button>
      <button type="button" class="btn-danger btn-sm" v-on:click="deleteForm(item.id)">
         <i class="fas fa-trash-alt"></i>
      </button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    item: {
      type: Object,
      required: true,
    }
  },
  methods: {
    redirToEditURL: function(formid) {
      let url = ...
      window.location = url
    },
    deleteItem: function(key) {
      this.$delete(this.items, key);
    },
    deleteForm: function(formid) {
      let url = '...';
      let deleteListItem = this.deleteItem;
      $.ajax({
        url: url,
        method: "DELETE",
      }).done(deleteListItem.bind(this, formid));
    }
  }
}
</script>

<style lang="scss" scoped>
...
</style>

Encountering issues with props as it's expecting an object instead of an array. Also, facing confusion regarding passing the correct props into the component while fetching external data via Ajax.

Fixed! Solution: Use `self.items = result.data.data` as Rails passes the data as a JSON object {data: ...} that requires accessing the inner data object.

Answer №1

Give it a shot.

<div v-for="element in elements">
   <custom_input_element :element="element" :key="element.id"></custom_input_element>
</div>

Answer №2

Issue resolved! The fix involved assigning self.items the value of result.data.data (rails passed the result as a JSON object {data: ...}, requiring result.data.data).

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

Using mapState on a module that is not namespaced

Question about a module export default { namespaced: false, state, actions, mutations, getters }; In one of my components, I attempted the following: ...mapState(["user"]), ...mapState('auth',["user"]), Unfortunately, neither of t ...

Using ng-style to apply a background image with a dynamic id

Hey there, I'm facing an issue here. The link provided below seems to not be working properly. Even though the id is set correctly within the scope, it seems like there might be a parsing error occurring. Any thoughts on what might be causing this pro ...

Incorporating a HTML layout with a JS backdrop

I have successfully implemented a JavaScript background and now I want to apply this background across all my PHP files. The issue is that the HTML code either overlaps with the JS content or appears behind it. How can I resolve this? Below is the JS fil ...

Flask application experiencing JavaScript issues on local host, yet functioning properly when accessed via 127.0.0.1

My web application is built using Python with Flask for the server, and HTML with JavaScript for the user interface that utilizes callback functions and POST methods. While the application runs smoothly with redirections and REST API calls at: Upon click ...

How to handle redirects based on status codes in AngularJS $http requests

My angular application includes numerous $http requests, and I am looking for a way to automatically redirect users to the login page in case the server session expires (resulting in a 401 error). Does anyone have a solution that can handle this for all ...

Can you provide guidance on effectively utilizing a Pinia store with Vue3, Pinia, and Typescript?

I'm currently facing challenges while using the Pinia store with TypeScript and implementing the store within a basic app.vue Vuejs3 option api. Here is my app.js file: import {createApp} from 'vue' import {createPinia} from "pinia&quo ...

Fetching Unicode block specials using axios in getStaticProps with Next.js

Click here to view the code and data results My attempt using the fetch method was successful, but I encountered issues when trying to use 'axios' ...

Dynamic content loading in Angular through HTML upon clicking

In the process of developing an Angular store, I aim to create anchor tags for each product so that clicking on a tag will display the selected product information in an HTML template below. Below is the current code snippet: (function() { var app = an ...

Vue Quasar Drawer Layout with Bottom Bar

Struggling to implement a footer in my q-drawer. Below is the template and component code I am currently using: <template> <q-layout view="hHh lpR fFf"> <q-header elevated> <q-toolbar> <q-btn flat de ...

Discovering relative URLs within an MVC 4 framework

Seeking a solution for storing the fully qualified URL of a relative path in MVC: ~/Content/themes/base/jquery-ui.min.css" In my scenario, I have a hidden input field: <input type="hidden" id="siteUrl" value=""/> I attempted to populate this hidd ...

Utilize Bootstrap in an Angular template to effortlessly expand one element while collapsing all others

I'm facing an issue with a table in my component. Each row has a button that, when clicked, should expand to show some calculated data specific to that row. However, the problem is that when one button is expanded, it keeps other buttons expanded as w ...

Retrieve pairs of items from a given variable

Containing values in my 'getDuplicates' variable look like this: getDuplicates = 100,120,450,490,600,650, ... These represent pairs and ranges: Abegin,Aend,Bbegin,Bend My task is to loop through them in order to apply these ranges. var ge ...

How can I filter an array to retain only the initial 5 characters of every element?

I need help using the .map function to transform this array so that only the first 5 characters are displayed, like "01:00", "02:00"? This is the array: ["01:00:00 AM", "02:00:00 AM", "03:00:00 AM", "04:00:00 AM", "05:00:00 AM", "06:00:00 AM", "07:00:00 ...

Experiencing a lack of information when trying to retrieve data through an http request in an express/react

Issue: My problem lies in attempting to retrieve data from http://localhost:3000/auth/sendUserData using the http protocol. Unfortunately, I am not receiving any data or response, as indicated by the absence of console logs. Tools at my disposal: The te ...

Tips for setting a default checked radio button in Vuejs

Upon page load, the radio button status should initially be checked. Then, when the radio button is changed, a div below it should hide and show accordingly. I have attempted to write the code for this functionality, but unfortunately it is not working a ...

The alignment of two elements is off in mobile display

Why aren't my two divs centered in mobile view? I am using Vuetify CSS flex helper justify-center to try and achieve it, but it doesn't seem to be working. Even when I use justify-sm-center, it still doesn't work. What am I missing? <v-co ...

The selection button's threshold

Welcome to my website design project! I have implemented image buttons for my web page and want to restrict the number of images a user can select. If a user tries to navigate away after selecting more than 3 images, I wish to display an alert message. Her ...

Struggling to grasp the concept of nested scope within AngularJs

I found myself puzzled while reading an article on this particular website (pitfall #5): My query is: Does this situation resemble having two variables with the same name in plain JavaScript, where one is locally defined (e.g. within a nested function ...

What is the reason my CSS formatting isn't being applied in Vue.js?

As a newcomer to web development and vue.js, I've been struggling to style my code effectively. Below is an example of my vue.js code where I have created markup for a profile description using figure, figcaption, and image elements. Could you please ...

When a Vue component collapses on itself, it prevents the next component from rendering

Within my primary component, known as App.vue, I have structured the template code like so: <template> <div class="app-wrapper"> <Header></Header> <Panel></Panel> <Showcase/> <Modal/> < ...