Id of item do not pass to the bootstarap modal in vue - vue.js

I have this code that does not pass the id of the item of array to the bootstrap modal
I need help with this because it takes too much time.
<script>
export default {
data() {
return {
lists: [
{id: 1, name: "Name 1"},
{id: 2, name: "Name 2"},
{id: 3, name: "Name 3"},
{id: 4, name: "Name 4"}
],
delete_id: null
}
},
methods: {
passId(id){
this.delete_id = id;
},
postDelete() {
// this part return null
console.log(this.delete_id)
}
}
}
</script>
<template>
<div>
<!-- delete card item -->
<div class="modal fade" id="delete_todo_item" tabindex="-1" user="dialog" aria-labelledby="exampleModalCenterTitle"aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" user="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" >delete</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div style="margin: 0 auto;" class="text-center">
<h2 style="font-size: 15px;color: red;">sure_to_delete</h2>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">close
</button>
<button class="btn btn-primary" data-dismiss="modal" #click="postDelete()">delete
</button>
</div>
</div>
</div>
</div>
<div v-for="item in lists" :key="item.id">
{{item.name}}
<i class="la la-trash" href="#delete_todo_item" data-toggle="modal" #click="passId(item.id)"></i>
</div>
</div>
</template>
The issue is when I click on the delete button of the modal I return null in console.
My version of vue is 2.6.1.
I need to know if there is an issue. I really can't understand what is going wrong.

Related

Vue.js 2 components data property bound to the first or to one only instance of component

Need help. I'm fairly new to Vue.js and need some help and advice.
Context:
I have a component with BS modal inside which is rendered in a for loop and obviously has many instances.
Issue:
The very first rendered component has its data received from parent via props, and the rest component have their own (like row.id and etc.)
Question: How to fix it? Maybe the problem in BS modal?
Component:
let vSelect = Vue.component("v-select", VueSelect.VueSelect);
var scoringButton = Vue.component("scoring-button", {
props: ["loadId", "causeData"],
template: "#scoring-template",
components: {
"v-select": vSelect,
},
data: function () {
return {
scoredLoadId: this.loadId,
scoring: null,
causeId: null,
selectedCause: null,
causeList: this.causeData,
};
},
computed: {
showCauseList() {
if (this.scoring === "1" || this.scoring === null) {
return true;
}
return false;
},
},
});
Template:
<template v-cloak id="scoring-template">
<div class="scoring-block" :id="scoredLoadId">
<div class="scoring">
<button
v-if="scoring === '1'"
title="Scoring button"
type="button"
class="btn btn-success scoring-btn"
data-bs-toggle="modal"
data-bs-target="#scoringModal"
>
<i class="bi bi-hand-thumbs-up-fill"></i>
</button>
<button
v-else-if="scoring === '2'"
title="Scoring button"
type="button"
class="btn btn-danger scoring-btn"
data-bs-toggle="modal"
data-bs-target="#scoringModal"
>
<i class="bi bi-hand-thumbs-down-fill"></i>
</button>
<button
v-else
title="Scoring button"
type="button"
class="btn btn-info scoring-btn"
data-bs-toggle="modal"
data-bs-target="#scoringModal"
>
<i class="bi bi-hand-thumbs-up-fill"></i>
<i class="bi bi-hand-thumbs-down-fill"></i>
</button>
</div>
<div
class="modal fade scoring-modal"
id="scoringModal"
tabindex="-1"
role="dialog"
aria-hidden="true"
>
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content" :key="loadId">
<div class="modal-header">
<h1 class="modal-title">Load Scoring</h1>
<button
type="button"
class="btn-close"
aria-label="Close"
data-bs-dismiss="modal"
></button>
</div>
<div class="modal-body">
<div class="scoring-content">
<input
type="radio"
class="btn-check"
name="btnScore"
id="btnLike"
autocomplete="off"
checked="scoring === '1'"
value="1"
v-model="scoring"
/>
<label
class="btn btn-outline-success"
for="btnLike"
#click="clearSelectedCause"
>
<i class="bi bi-hand-thumbs-up-fill"></i> Like
</label>
<input
type="radio"
class="btn-check"
name="btnScore"
id="btnDislike"
autocomplete="off"
:checked="scoring === '2'"
value="2"
v-model="scoring"
/>
<label class="btn btn-outline-danger" for="btnDislike">
<i class="bi bi-hand-thumbs-down-fill"></i> Dislike
</label>
</div>
<div class="scoring-cause">
<v-select
:disabled="showCauseList"
label="name"
:options="causeList"
v-model="selectedCause"
placeholder="Choose a cause option"
></v-select>
</div>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-secondary"
data-bs-dismiss="modal"
>
Close
</button>
<button type="button" class="btn btn-primary">
Save changes
</button>
</div>
</div>
</div>
</div>
</div>
</template>
Parent:
var mainTableApp = new Vue({
el: "#main-table",
data: {
tableData: [],
scoringCauseList: [
{
"id": 6,
"scoring_type": 0,
"name": "Late at loading",
"mark": "late_at_loading"
},
{
"id": 7,
"scoring_type": 0,
"name": "Special conditions were not respected",
"mark": "special_conditions_were_not_respected"
},
{
"id": 8,
"scoring_type": 0,
"name": "Bad/not enough information",
"mark": "bad_not_enough_information"
}
],
},
components:{
'scoring-button': scoringButton,
}
});
Component in the main app block:
<div
v-for="row in tableData"
class="row-container"
>
....
<scoring-button
:id="row.LOAD_ID"
:key="row.LOAD_ID"
:load-id="row.LOAD_ID"
:cause-data="scoringCauseList"
>
</scoring-button>
....
</div>
I tried to resetting BS modal's data, but it didn't work. So, I went back to look for a solution in Vue part.
I know I may construct the whole thing not enough in a very right way, but this code below is the last version after many other solutions, have been tried with v-model, $emit, props etc.
Update: found solution.
Added ":id" for all "input" fields I had in my component.
So, to have reusable components their own data properties you need to have dynamic ":id" properties. So, that each data flows into their own component.
<input
type="radio"
class="btn-check"
name="btnScore"
id="btnLike" // <-- old line
:id="`btnLike-${dynamicStr}`" // <-- new modified line
autocomplete="off"
checked="scoring === '1'"
value="1"
v-model="scoring"
/>
<label
class="btn btn-outline-success"
for="btnLike" // <-- old line
:for="'btnLike-${dynamicStr}'" // <-- new modified line
#click="clearSelectedCause"
>
<i class="bi bi-hand-thumbs-up-fill"></i> Like
</label>

The requested module does not provide an export named vuejs 3

I have tried to use export default and export default legend but it still got error
The requested module '/src/legends.js?t=1637071' does not provide an export named 'legend'
in console log, how can I fix this problem?
Thank you!!
legends.js
import axios from "axios";
const API_URL = "http://localhost:8000/api";
function add(url, type) {
axios
.post(`${API_URL}/${url}`, this.newRole)
.then((res) => {})
.catch((error) => {
console.log("ERRRR:: ", error.response.data);
});
}
function remove(url, type) {
axios
.post(`${API_URL}/${url}`, this.newRole)
.then((res) => {})
.catch((error) => {
console.log("ERRRR:: ", error.response.data);
});
}
export default legend
manageRoles.vue
<template>
<div class="col-md-12">
<div class="card">
<div class="card-header d-flex flex-row">
<h4 class="card-title align-self-center">Roles Manager</h4>
<button
class="btn btn-success btn-fab btn-icon btn-round mb-3 ml-2"
data-toggle="modal"
data-target="#addRoleModal">
<i class="icon-simple-add"></i>
</button>
</div>
<div class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Role Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="role in roles" :key="role.id">
<td>
{{ role.id }}
</td>
<td>
{{ role.name }}
</td>
<Buttons />
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- MODALS -->
<!-- ADD NEW ROLE MODAL -->
<div
class="modal modal-black fade" id="addRoleModal"
tabindex="-1" role="dialog" aria-labelledby="addRoleModal" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Add new role</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
<i class="icon-simple-remove"></i>
</button>
</div>
<form class="form-horizontal">
<div class="modal-body">
<div class="d-flex flex-row">
<label class="col-md-4 col-form-label">Role name</label>
<div class="col-md-6">
<div class="form-group">
<input type="name" name="name" class="form-control"
v-model="newRole.name" />
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Cancel
</button>
<button type="submit" class="btn btn-primary" #click.stop.prevent="addRole()">
Add new role
</button>
</div>
</form>
</div>
</div>
</div>
<!-- END ADD NEW ROLE MODAL -->
<!-- REMOVE ROLE MODAL -->
<div class="modal modal-black fade" id="roleRemoveModal"
tabindex="-1" role="dialog" aria-labelledby="roleRemoveModal" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="roleRemoveModal">
Confirm delete role
<strong class="text-primary">
{{ roleInfo.name }}
</strong>
</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
<i class="icon-simple-remove"></i>
</button>
</div>
<div class="modal-body h4 text-white text-center mt-4">
Really want to delete role
<strong class="text-danger">
{{ roleInfo.name }}
</strong>?
</div>
<div class="modal-footer d-flex flex-row">
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Cancel
</button>
<button type="button" class="btn btn-danger" data-dismiss="modal" #click="removeRole()">
Delete role
</button>
</div>
</div>
</div>
</div>
<!-- END REMOVE ROLE MODAL -->
<!-- END MODALS -->
</template>
<script>
import Buttons from "../components/cores/Buttons.vue";
import { legend } from "/src/legends.js";
export default {
name: "manageRoles",
components: { Buttons },
data() {
return {
roles: [],
newRole: {
name: null,
},
roleInfo: {
id: 0,
name: "",
},
};
},
methods: {
addRole() {
legend.add(`roles/createRole`);
this.$router.push("/manager/roles");
},
removeRole() {
legend.remove(`roles/createRole`);
this.$router.push("/manager/roles");
},
},
mounted() {
this.refreshRoles();
},
};
</script>
export default is default export, as the name implies. Regardless of how the variable that holds a value of default export is called, it's supposed to be imported as default import, not named import with brackets:
const legend = ...;
export default legend;
and
import legendCanBeImportedUnderAnyName from "/src/legends.js"
Alternatively, it can be made named export, it also needs to be imported as such:
export const legend = ...;
and
import { legend } from "/src/legends.js"
Try this.import * as legend from "/src/legends.js";

Vue Js v-for using

I'm a vue.js beginner. I want to get data from app.js using v-for in html and show it in separate divs. I want to add an independent background and an independent text for each todo.
var example = new Vue({
el:"#example",
data: {
todos : [
{id:1 , name: "ŞEHİR MERKEZİ"},
{id:2 , name: "HÜDAİ KAPLICA"},
{id:3 , name: "AFYONKARAHİSAR"}
]
}
})
<div class="container" id="example">
<div class="row">
<div class="col-lg-6">
<div class="flip flip-vertical">
<div
class="front"
style="background: url(./static/images/sandıklı.jpg)">
<h1 class="text-shadow" >
<div class="hborder" v-for="todo in todos" :key="todo">
{{todo.name}}
</div>
</h1>
</div>
<div class="back">
<h4>ŞEHİR MERKEZİ GÜZERGAHINI ONAYLIYOR MUSUNUZ ?</h4>
<button type="button" class="btn btn-primary btn-lg">EVET</button>
<button type="button" class="btn btn-danger btn-lg">HAYIR</button>
</div>
</div>
</div>
</div>
</div>
<script src="./static/js/vue#2.js"></script>
<script src="./static/js/app.js"></script>
what you could do is add the background color and description to the todos object.
Also I made a little change in your data function.
var example = new Vue({
el:"#example",
data() {
return {
todos : [
{id:1 , name: "ŞEHİR MERKEZİ", bg: '#FFF', desc: 'desc1'},
{id:2 , name: "HÜDAİ KAPLICA", bg: '#ff0000', desc: 'desc2'},
{id:3 , name: "AFYONKARAHİSAR", bg: '#cecece', desc: 'desc3'}
]
}
}
})
<div class="container" id="example">
<div class="row">
<div class="col-lg-6">
<div class="flip flip-vertical">
<div
class="front"
style="background: url(./static/images/sandıklı.jpg)">
<h1 class="text-shadow" >
<div class="hborder" v-for="todo in todos" :key="todo" :style="background-color: todo.bg;">
{{todo.name}}
{{todo.desc}}
</div>
</h1>
</div>
<div class="back">
<h4>ŞEHİR MERKEZİ GÜZERGAHINI ONAYLIYOR MUSUNUZ ?</h4>
<button type="button" class="btn btn-primary btn-lg">EVET</button>
<button type="button" class="btn btn-danger btn-lg">HAYIR</button>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue#2"></script>

After obtaining more data the open panel is not correct

When loading the panels the first time and opening the panel in the third position. Everything is Ok.
When loading more elements, the panel that opens is not correct but it is still the one in the third position.
<div id="load-conversation">
<div class="row" style="margin-bottom: 10px;" v-show="referencesList.length >0">
<div class="col-md-12 text-center">
<button v-on:click="getEmails" class="btn btn-sm btn-blue">Cargar más</button>
</div>
</div>
<div class="panel panel-info" v-for="email in emails">
<!-- Head Panel-->
<div class="panel-heading" v-bind:class=" email.incoming ? 'white-inbox' : 'blue-reply'"
data-toggle="collapse" :href="`#collapse-new${email.id}`">
<!--Email Title-Button -->
<div class="row">
<div class="col-md-8">
<h5 v-if="email.incoming"><span class="fas fa-reply" aria-hidden="true"></span> ${email.sender_name}</h5>
<h5 v-else><span class="fas fa-share-square" aria-hidden="true"></span> ${email.sender_name}</h5>
</div>
</div>
</div>
<!--Body Panel-->
<div :id="`collapse-new${email.id}`" class="panel-collapse collapse">
<div class="panel-body">
<!--Email Head-->
<div class="container-fluid">
<div class="row">
<strong>SUBJECT: </strong><span class="text-info">${email.subject}</span></br>
<strong>FROM: </strong><span class="text-info">${email.sender_email}</span></br>
<strong>TO: </strong><span class="text-info"><span v-for="to in email.to">${to.email}, </span></span></br>
<strong>DATE: </strong><span class="text-info">${email.date}</span></br>
<strong v-if="email.cc.length > 0">CC: </strong><span class="text-info"><span v-for="cc in email.cc">${cc.email}, </span></span>
</div>
</div>
<br>
<!--Email Body-->
<div class="row container-fluid">
<div class="list-group-item"v-html="email.content_html">
</div>
<div>
<div class="bs-component">
<ul class="pager">
<li class="previous" data-toggle="collapse" :data-target="`#open-details-new${email.id}`">
<a href="javascript:void(0)">
<span class="glyphicon glyphicon-option-horizontal" aria-hidden="true"></span>
</a>
</li>
</ul>
</div>
<div :id="`open-details-new${email.id}`" class="collapse" v-html="email.complete_html">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
The important thing is to see that the new elements are placed in the order of the old elements, that is, the order is reversed in the list. By only adding the elements without changing the order everything works correctly.
<script>
var loadMore = new Vue({
el: '#load-conversation',
delimiters: ['${', '}'],
data: {
limit : 3,
referencesList : [1,2,3,4,5],
emails : [],
showLoadBtn: true,
},
mounted() {
this.getEmails()
},
methods: {
getEmails(){
axios.post("get_more_emails", { references: this.getReferences() })
.then(response => {
//console.log(response.data);
this.emails = [ ...response.data, ...this.emails ]
}).catch(function (error) {
console.log('error', error)
});
},
getReferences(){
...
}
},
})
</script>

Vue js: Props on modal component keeps showing typeError

i'm having trouble with the data that being passed to the component.
it will only shows as an object, not the item inside the object. i have
read several questions/answers from here, but i still couldn't make it works.
when i try {{ modalData.projectName }} (in child), it will show this error in console " Error in render: "TypeError: _vm.modalData is null" "
here are my codes:
parent
<template>
<section id="portFolio">
<div class="row">
<div
v-for="project in projects"
v-bind:key="project.id"
class="col-xs-12 col-sm-6 col-md-4 col-lg-4 col-xl-3"
>
<a type="button" data-toggle="modal" data-target="#exampleModal" v-on:click="openModal(project)">
<div class="project-block">
<!-- Image -->
<div class="image-preview">
<img src="/dist/img/logo-test.png" class="img-fluid" alt="Twitter">
</div>
<!-- Details -->
<div class="project-details p-t-10 p-b-10 p-l-10 p-r-10">
<div class="">
<h5 class="text-purple m-b-5">{{ project.projectName }}</h5>
<p class="text-dark">{{ project.companyName }}</p>
</div>
<p class="subtext text-dark">{{ project.projectYear }}</p>
</div>
</div>
</a>
</div>
</div>
<app-modal ref="modal" v-bind:modalData="selectedItem"/>
</section>
</template>
<script>
// Component
import modalBlock from "./component/portfolio/portfolioModal.vue";
export default{
name: "portFolio",
components: {
'app-modal': modalBlock
},
data() {
return {
// showModal: false,
selectedItem: null,
projectName: '',
companyName: '',
projectYear: null,
previewPic: '',
testText: '',
modalData: '',
projects: [
{
projectName: 'Project One',
companyName: 'CoolCompany Sdn Bhd',
projectYear: 2019,
previewPic: "/dist/img/logo-test.png",
testText: "First object"
},
{
projectName: 'Project Two',
companyName: 'NotSoCompany Sdn Bhd',
projectYear: 2018,
previewPic: "/dist/img/logo-test.png",
testText: "Second object"
},
{
projectName: 'Project Three',
companyName: 'LameCompany Sdn Bhd',
projectYear: 2017,
previewPic: "/dist/img/logo-test.png",
testText: "Third object"
}
]
}
},
methods: {
openModal(modalData) {
this.selectedItem = modalData
let element = this.$refs.modal.$el
$(element).modal('show')
}
}
}
</script>
Child component:
<template>
<div class="modal fade bd-example-modal-xl" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-xl modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">The title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h2>{{ modalData.projectName }}</h2>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</template>
<script>
export default{
name: "modalBlock",
props:['modalData'],
data(){
return {
}
},
methods: {
close() {
this.$emit('close');
},
}
}
</script>
may i know where did i done wrong? or Did i forgot to declare something on the code?
thank you