I would like to retrieve the data “project.id” to record it when I click on the button “add study”. When I click on this button, I send the name and the status but i would like to send the id of project too via my API. Si this is the code :
JavaScript
x
35
35
1
<template>
2
<div>
3
4
<div v-for="(projet,index) in arrayProjects.projects" v-bind:key="index" class="box-project">
5
6
<h2>{{projet.title}} - {{projet.nameStructure}}</h2>
7
8
<ProjectTable v-bind:id-project="projet.id" />
9
10
<div>
11
<a-button type="secondary" @click="showModalStudy">
12
Add study {{projet.id}}
13
</a-button>
14
</div>
15
<a-modal
16
title="Add study :"
17
:visible="visible"
18
:confirm-loading="confirmLoading"
19
@cancel="cancelClick"
20
@ok="sendClick"
21
>
22
<div>
23
<a-form>
24
<a-form-item >
25
<a-input
26
v-model="newStudy.nameStudy"
27
/>
28
</a-form-item>
29
</a-form>
30
</div>
31
</a-modal>
32
</div>
33
</div>
34
</template>
35
And the javascript :
JavaScript
1
52
52
1
import ProjectTable from "@/components/ProjectTable";
2
import axios from "axios";
3
4
export default {
5
6
name: "ProjectCard",
7
components: {ProjectTable},
8
props: ["arrayProjects"],
9
data() {
10
return {
11
visible:false,
12
confirmLoading: false,
13
newStudy: {
14
nameStudy:"",
15
}
16
}
17
},
18
methods: {
19
20
21
showModalStudy() {
22
this.visible = true;
23
},
24
25
cancelClick() {
26
this.visible= false;
27
console.log("click cancel")
28
},
29
30
sendClick() {
31
console.log("send")
32
console.log(this.newStudy.nameStudy)
33
this.confirmLoading = true;
34
axios
35
.post('http://127.0.0.1:8000/api/studies', {
36
name : this.newStudy.nameStudy,
37
status: "On load",
38
39
})
40
41
setTimeout(() => {
42
this.visible = false;
43
this.confirmLoading = false;
44
}, 1000);
45
46
}
47
48
}
49
50
51
}
52
How can I add my id of my project in my axios lines to send it ? Thanks for your help
Advertisement
Answer
You should determine the current project.id
with a container in data
scope for the Add study handler sendClick()
with passing project.id
to showModalStudy()
to overwrite the defined data currentId
for the next API call
JavaScript
1
4
1
<a-button type="secondary" @click="showModalStudy(project.id)">
2
Add study {{ projet.id }}
3
</a-button>
4
JavaScript
1
7
1
data() {
2
return {
3
currentId = null // will be a id value when user click the button
4
5
};
6
},
7
JavaScript
1
10
10
1
showModalStudy(projectId) {
2
this.visible = true;
3
this.currentId = projectId
4
},
5
6
sendClick() {
7
// do something with this.currentId
8
// ...
9
}
10