I want to return a value ("Title") from a table row (generated by a v-for loop) when a button in that row is clicked, but I can't figure out the correct method. I can console.log the value on click, but I can't get it to actually return a value in the browser.
I've tried many different methods and none work. I also get the following error when I use {{ clicked(row) }} to display the returned value in my HTML:
"Property or method "row" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property."
It doesn't work if I remove the "row" argument either.
This method logs the correct value to console, but does not return the value in browser:
methods: {
clicked(row) {
console.log(row.title);
return (row.title);
}
I've spent days trying to figure this out - any help greatly appreciated!
HTML
<div id="app">
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th>Add To Cart</th>
</tr>
</thead>
<tbody>
<tr v-for="row in rows">
<td>{{ row.title }}</td>
<td>{{ row.price }}</td>
<td><button @click="clicked (row)">Add To Cart</button></td>
</tr>
</tbody>
</table>
<p>{{ clicked(row) }}</p>
</div>
</div>
JS
var app = new Vue({
el: "#app",
data: {
rows: [{
id: 001,
title: "Lorem",
price: 5
},
{
id: 002,
title: "Ipsum",
price: 23
},
{
id: 003,
title: "Dolor",
price: 8
},
]
},
methods: {
clicked(row) {
console.log (row.title);
return (row.title);
}
}
})
I am trying to display the value for "Title" when I click the button on its row. (And if I click both rows, I want both "Titles" to display).
I currently only get various errors.