英文:
tr td tag, unique id to determine which taable contact I click using v-for and :id
问题
My question is simple, I have a v-for
and unique id assigned in tr
and td
tags in a table. When I click on contenteditable
, I want to trigger an event in JavaScript to determine which id field I am changing. The dynamic ID is supposed to be ep_ + i + p.pid
, where ep_
is a text, i
is the index from v-for
, and p.pid
is assigned from v-for
. When I edit a td
with contenteditable
and lose focus, I want to display an alert indicating which table field I am editing. Here is my tr
and td
table:
<tr class="list_of_products" v-for="p, i in all_products" :id="'ep_' + i + p.pid">
<td contenteditable data-field="p_code">{{ p.p_code }}</td>
<td contenteditable data-field="p_fullname">{{ p.p_fullname }}</td>
<td contenteditable data-field="p_category">{{ p.p_category }}</td>
<td contenteditable data-field="p_cost">{{ p.p_cost }}</td>
<td contenteditable data-field="p_margin">{{ p.p_margin }}</td>
<td contenteditable data-field="p_sell">{{ p.p_sell }}</td>
</tr>
I prefer to use pure JavaScript to detect which td
I am editing when the blur
event occurs in the list_of_products
. Here's how you can achieve it:
var list_of_products = document.getElementsByClassName('list_of_products');
for (var i = 0; i < list_of_products.length; i++) {
list_of_products[i].addEventListener('blur', function(e) {
// Get the ID of the parent <tr> element
var id = e.target.parentElement.id;
// Get the data-field attribute of the <td> being edited
var dataField = e.target.getAttribute('data-field');
console.log("Product field changed");
alert("You edited the field with ID: " + id + " and data-field: " + dataField);
});
}
This code will listen for the blur
event on all elements with the class "list_of_products" and display an alert indicating which field you are editing.
英文:
My question is simple, I have a v-for
and unique id assign in tr
td
tag table, when I click contenteditable, I want to fire list_of_products
and recognize from js to know which id field I am changing, the dynamic suppose is ep_ + i + p.pid
, ep_
is print text, i
is index for v-for, and p.pid
is assign from v-for. Here when I edit td contenteditable and lost focus, it will show an alert which table field I am editing. Here is my tr td table below:
<tr class="list_of_productss" v-for="p, i in all_products" :id= "ep_" + i {{p.pid}}>
<td contenteditable data-field="p_code" > {{ p.p_code }} </td>
<td contenteditable data-field="p_fullname"> {{ p.p_fullname }} </td>
<td contenteditable data-field="p_category"> {{ p.p_category }} </td>
<td contenteditable data-field="p_cost"> {{ p.p_cost }} </td>
<td contenteditable data-field="p_margin"> {{ p.p_margin }} </td>
<td contenteditable data-field="p_sell"> {{ p.p_sell }} </td>
</tr>
I prefer pure javascript so I do when lost focus in list_of_productss, javascript will know which td I am edit at:
document.getElementsByClassName('list_of_productss').addEventListener('blur', function(e) {
//how to know which id I am typing for i here
console.log("product_things changed");
alert("You edit this field with index with data-field" + ) //?? for data-field and i
});
If I do getElementsByClassName
or getElementsByClassId
, it must be specific id otherwise I cannot called it , how to make dynamic getElementsByClassName
or getElementsByClassId
to make an alert I am edit this specific field, there is total 6 columns for each row. Any help for me is a huge support.
答案1
得分: 0
At first:
不要在Vue的属性绑定
和v-if/v-show
条件中使用Mustaches {{}}
。
查看属性绑定
Mustaches不能在HTML属性内部使用。而是使用
v-bind
指令。
使用字符串拼接或模板文字 ${}
。
Second:
<tr><td>标签似乎不会触发onblur
事件,onfocusout
会起作用。
这里是使用Vue.js和Vanilla Js事件处理的示例。
const { createApp, ref, onMounted } = Vue
const App = {
setup() {
const all_products = ref([
{ p_code: 1, p_fullname: "One"},
{ p_code: 2, p_fullname: "Two"}
]);
onMounted(() => {
let el = document.getElementsByClassName('list_of_productss');
for(var i = 0; i < el.length; i++) {
el[i].addEventListener('focusout', function(e) {
// 如何知道我正在为i输入哪个id
console.log("product_things changed: " + e.target.id);
});
}
})
const handleBlur = (e) => {
console.log("handleBlur: " + e.target.id)
}
const handleFocusout = (e) => {
console.log("handleFocusout: " + e.target.id)
}
return {
all_products, handleBlur, handleFocusout
}
}
}
const app = createApp(App)
app.mount('#app')
#app { line-height: 1.75; }
[v-cloak] { display: none; }
td { padding: 30px;}
<div id="app" v-cloak>
<table border=1>
<tr class="list_of_productss" v-for="p, i in all_products" :id= "`ep_${i}`" @blur="handleBlur" @focusout="handleFocusout">
<td contenteditable data-field="p_code" :id= "`ep_code_${i}`"> {{ p.p_code }} </td>
<td contenteditable data-field="p_fullname" :id= "`ep_fn_${i}`"> {{ p.p_fullname }} </td>
</tr>
</table>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
英文:
At first:
Do not use Mustaches {{}}
in the Vue attribute bindings
and v-if/v-show
conditions.
See, Attribute Bindings
> Mustaches cannot be used inside HTML attributes. Instead, use a
> v-bind
directive
Use string concatenation or Template literals ${}
Second:
The tags <tr><td> seems to not produce onblur
event. onfocusout
does work.
Here is the playground with Vue.js and Vanilla Js event handling.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const { createApp, ref, onMounted } = Vue
const App = {
setup() {
const all_products = ref([
{ p_code: 1, p_fullname: "One"},
{ p_code: 2, p_fullname: "Two"}
]);
onMounted( () => {
let el = document.getElementsByClassName('list_of_productss');
for(var i = 0; i< el.length; i++) {
el[i].addEventListener('focusout', function(e) {
//how to know which id I am typing for i here
console.log("product_things changed: " + e.target.id);
});
}
})
const handleBlur = (e) => {
console.log("handleBlur: " + e.target.id)
}
const handleFocusout = (e) => {
console.log("handleFocusout: " + e.target.id)
}
return {
all_products, handleBlur, handleFocusout
}
}
}
const app = createApp(App)
app.mount('#app')
<!-- language: lang-css -->
#app { line-height: 1.75; }
[v-cloak] { display: none; }
td { padding: 30px;}
<!-- language: lang-html -->
<div id="app" v-cloak>
<table border=1>
<tr class="list_of_productss" v-for="p, i in all_products" :id= "`ep_${i}`" @blur="handleBlur" @focusout="handleFocusout" >
<td contenteditable data-field="p_code" :id= "`ep_code_${i}`" > {{ p.p_code }} </td>
<td contenteditable data-field="p_fullname" :id= "`ep_fn_${i}`"> {{ p.p_fullname }} </td>
</tr>
</table>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论