Using a button to create a new component

Preston

I'm working in VueJs and I've created a few components, called SiteList.vue, Site.vue, and CreateSite.vue. These components make up a bigger component hosted in a file called ListContainer.vue. These components all together are a list of websites that the user can reactively add, change the title, and remove sites, and edit function. ListContainer.vue outputs the whole thing to App.vue. Inside App.vue I've created an html table in the template, and call the listcontainer element inside the table tags. I have a button element that I am trying to use to continue creating the listcontainer element inside the tabletags. So instead of just one listcontainer element, the button will be able to create many.

I attempted to make an array that had all the formatting for a template element inside it, but it did not work and felt hacky.

I read the section on dynamic components and I feel like that is much more close, but I just can't wrap my head around how I should do it. I'm genuinely stuck on something I think might be extremely easy...

App.vue

<template>   <div id="app">   <!-- Injection goes here -->
    <time-component/>
    <table>
    <tr>
    <td><listcontainer/></td>
    <td><listcontainer/></td>
    <td><div><button @click="listcontainer++">MAKE NEW LIST</button></div></td>
    </tr>
    </table>   </div> </template>

<script> 


import TimeComponent from './components/Time.vue' 
import listcontainer from "./components/ListContainer.vue"

export default {   name: 'App',   components: {
    TimeComponent,
    listcontainer,   },   methods:{
    addListContainer(){
    
    }

  }    } </script>

<!-- CSS for global --> <style>
#app {   font-family:serif;   -webkit-font-smoothing: antialiased;   -moz-osx-font-smoothing: grayscale;   text-align: center;   margin-top: 60px;   background-color: #fff; } </style>

ListContainer.vue

<template>
    <div class="large-container">
        <sitelist @changeLabel="setSiteLabel" :siteListLabel="siteListLabel_d"/>
    </div>
</template>

<script>

import sitelist from "./SiteList.vue"

export default {
    name: "list-container",
    data(){
        return {
            siteListLabel_d: "",

        };
    },
    methods: {
        setSiteLabel(siteListLabel){
            this.siteListLabel_d = siteListLabel;
        },
    },
    components: { sitelist }  
};

</script>

<style scoped>
.large-container{
    padding: 20px;
    margin-left: 20px;
    margin-top: 200px;
    border: 1px solid;
    max-width: 400px;
}
</style>

SiteList.vue

<template>
   <div class="sitelist-container">
        {{  siteListLabel }}
       <form @submit.prevent="changeLabel()">
           <input
           type="text"
           v-model="siteListLabel_d"
           />
           <button @click="changeLabel"> Change </button>
        </form>
       <ul>
           <create-site @on-new-site="addSite($event)" />
           
           <site
           v-for="(site, index) in sites" 
           :key="index"
           :siteName="site.siteName"
           :staticRef="site.staticRef"
           @site-remove="removeSite(site)"
           @edit-el="editSite(site, $event)"
           />
      </ul>
    </div>
</template>

<script>

import Site from "./Site.vue"
import createSite from "./CreateSite.vue"

export default {
    name: "site-list",
    props: {
        siteListLabel: String,

    },
    data(){
        return {
            sites: [ { staticRef: "https://", siteName: "www.cnn.com" }, 
                     { staticRef: "https://", siteName: "www.nyt.com" }, 
                     { staticRef: "https://", siteName: "washingtonpost.com" }
            ],
            // Shouldn't edit props so I made this which is based off
            // the value of the prop but not using the prop directly.
            siteListLabel_d: ""
        };
    },
    methods: {
        addSite(newSite){
            this.sites.push({ staticRef: "https://", siteName: newSite });
        },
        // create a new array (callback)
        removeSite(removedSite){
            this.sites = this.sites.filter(site => site !== removedSite);
        },
        editSite(site, newSiteName){
            site.siteName = newSiteName;
        },
        changeLabel(){
            this.$emit("changeLabel", this.siteListLabel_d);
            //this.siteListLabel = this.siteListLabel_d
        }
    },
    components: { Site, createSite }
};

</script>

<style scoped>

</style>

Site.vue

<template>
<div class="container">
    <li> 
        <span><a :href="staticRef + siteName">{{ siteName }}</a> <!-- array {{ siteName }} --> </span> 
        <button @click="doEdit() ; isHidden = !isHidden"> Toggle-Edit </button>
        <button @click="$emit('site-remove')">Remove</button>
    </li>
    <form @submit.prevent="stopEdit()" v-if="!isHidden">
            <input
                type="text"
                v-model="newSiteName"
                @blur="stopEdit()"
            />
        </form>
</div>
</template>

<script>
export default {

    data: function() {
        return{
            isEditable: false,
            isHidden: true,
            newSiteName: ""
        };
    }, 
    props: {
        siteName: String,
        staticRef: String,
    },
    methods: {
        doEdit(){
            if (this.isEditable){
                this.stopEdit();
            } else {
                this.newSiteName = this.siteName;
                this.isEditable = true;
            }
        },
        stopEdit(){
            this.isEditable = false;
            this.$emit("edit-el", this.newSiteName)
        }
       
    }
};
</script>


<style scoped>
.container{
    border: 1px solid;
}

</style>

CreateSite.vue

<template>
    <form @submit.prevent="addSite()">
    <input
    v-model="newSite"
    type="text"
    placeholder="Add a new site..."
     />
     </form>
</template>

<script>
export default {
    data: function(){ 
        return{
            newSite: ""
        }; 
    },
    methods: {
        addSite(){
            if (this.newSite.length > 0){
                this.$emit("on-new-site", this.newSite);
            }
        }

    }
}


</script>

<style scoped>

</style>
ssc-hrep3

The goal is to add a new array item with all the data which is necessary to a single component when clicking onto the button. You do not put the template of each element into the array but only the data. Then, you'll loop through the array with v-for and, like this, create a component for each element in the array. The data which is part of the array is bound to the component with a prop.

Here is an example:

<template>
  <div id="app">
    <button @click="addNewItem">Add</button>
    <table>
      <tr v-for="item in array" :key="item.id">
        <td>
          <listcontainer :item="item" />
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
import Listcontainer from './components/ListContainer.vue';

export default {
  name: 'App',
  components: { Listcontainer },
  data() {
    return { array: [] };
  },
  methods:{
    addNewItem() {
      this.array.push({
        id: Math.random(),
        data: ...
      });
    }
  }
</script>

The Listcontainer should then have a prop with all relevant information:

<template>
  <div>{{ item }}</div>
</template>

<script>
export default {
  name: 'Listcontainer',
  props: {
    item: {
      type: Object,
      required: true
    }
  }
};
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using a button to create a new component

From Dev

Dynamically create and add new component on button click in Angular Dart

From Java

How to create a new component in Angular 4 using CLI

From Dev

is it possible to create new button or menu item of application using applescript

From Dev

Create a VCL Button Component with sound

From Dev

Create new Child Component within a Child Component

From Dev

React Activate a Component using a button

From Dev

How do I create a class decorator for mixing new methods into a React component when using Typescript?

From Dev

Using button_to to create new record in database, error: param is missing or the value is empty

From Dev

Using button_to to create new record in database, error: param is missing or the value is empty

From Dev

Create a new file in any desktop using an excel file with a command button to execute that task

From Dev

Create and return a new component on-click in parent component

From Dev

How to Create New Panels on Button Click?

From Dev

"create new visual classes" button not clickable on eclipse

From Dev

Open and create new post button isnt working

From Dev

Click an enter button to create a new textbox

From Dev

How to Create New Panels on Button Click?

From Dev

create new html elements for button click in query

From Dev

How to create a new button in WordPress admin menu

From Dev

Create new button not working - eZ Publish

From Dev

Android: Create new ImageView by every button click

From Dev

How create button using reflection?

From Dev

Create Toggle Button Using jQuery

From Dev

How to hide a component using button in reactjs?

From Dev

Using jsonpatch to create new path

From Dev

Create a new column using a map

From Dev

Create a new div using jQuery

From Dev

New and Create actions using Cells

From Dev

Using loop to create new variables

Related Related

  1. 1

    Using a button to create a new component

  2. 2

    Dynamically create and add new component on button click in Angular Dart

  3. 3

    How to create a new component in Angular 4 using CLI

  4. 4

    is it possible to create new button or menu item of application using applescript

  5. 5

    Create a VCL Button Component with sound

  6. 6

    Create new Child Component within a Child Component

  7. 7

    React Activate a Component using a button

  8. 8

    How do I create a class decorator for mixing new methods into a React component when using Typescript?

  9. 9

    Using button_to to create new record in database, error: param is missing or the value is empty

  10. 10

    Using button_to to create new record in database, error: param is missing or the value is empty

  11. 11

    Create a new file in any desktop using an excel file with a command button to execute that task

  12. 12

    Create and return a new component on-click in parent component

  13. 13

    How to Create New Panels on Button Click?

  14. 14

    "create new visual classes" button not clickable on eclipse

  15. 15

    Open and create new post button isnt working

  16. 16

    Click an enter button to create a new textbox

  17. 17

    How to Create New Panels on Button Click?

  18. 18

    create new html elements for button click in query

  19. 19

    How to create a new button in WordPress admin menu

  20. 20

    Create new button not working - eZ Publish

  21. 21

    Android: Create new ImageView by every button click

  22. 22

    How create button using reflection?

  23. 23

    Create Toggle Button Using jQuery

  24. 24

    How to hide a component using button in reactjs?

  25. 25

    Using jsonpatch to create new path

  26. 26

    Create a new column using a map

  27. 27

    Create a new div using jQuery

  28. 28

    New and Create actions using Cells

  29. 29

    Using loop to create new variables

HotTag

Archive