tr td标签,使用v-for和:id确定我点击的表格联系的唯一ID。

huangapple go评论91阅读模式
英文:

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:

&lt;tr class=&quot;list_of_productss&quot; v-for=&quot;p, i in all_products&quot; :id= &quot;ep_&quot; + i {{p.pid}}&gt;
&lt;td contenteditable data-field=&quot;p_code&quot; &gt; {{ p.p_code }} &lt;/td&gt;
&lt;td contenteditable data-field=&quot;p_fullname&quot;&gt; {{ p.p_fullname }} &lt;/td&gt;
&lt;td contenteditable data-field=&quot;p_category&quot;&gt; {{ p.p_category }} &lt;/td&gt;
&lt;td contenteditable data-field=&quot;p_cost&quot;&gt; {{ p.p_cost }} &lt;/td&gt;
&lt;td contenteditable data-field=&quot;p_margin&quot;&gt; {{ p.p_margin }} &lt;/td&gt;
&lt;td contenteditable data-field=&quot;p_sell&quot;&gt; {{ p.p_sell }} &lt;/td&gt;
&lt;/tr&gt;

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(&#39;list_of_productss&#39;).addEventListener(&#39;blur&#39;, function(e) {
  //how to know which id I am typing for i here
  console.log(&quot;product_things changed&quot;);
  alert(&quot;You edit this field with index with data-field&quot; + ) //?? 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: &quot;One&quot;},
      { p_code: 2, p_fullname: &quot;Two&quot;}
    ]);
    
    onMounted( () =&gt; {
      let el = document.getElementsByClassName(&#39;list_of_productss&#39;);
      for(var i = 0; i&lt; el.length; i++) {
        el[i].addEventListener(&#39;focusout&#39;, function(e) {
          //how to know which id I am typing for i here
          console.log(&quot;product_things changed: &quot; + e.target.id);
        });      
      }
    })
    
    const handleBlur = (e) =&gt; {
      console.log(&quot;handleBlur: &quot; + e.target.id)
    }
    
    const handleFocusout = (e) =&gt; {
      console.log(&quot;handleFocusout: &quot; + e.target.id)
    }
    
    return {
      all_products, handleBlur, handleFocusout
    }
  }
}

const app = createApp(App)
app.mount(&#39;#app&#39;)

<!-- language: lang-css -->

#app { line-height: 1.75; }
[v-cloak] { display: none; }
td { padding: 30px;}

<!-- language: lang-html -->

&lt;div id=&quot;app&quot; v-cloak&gt;
&lt;table border=1&gt;
  &lt;tr class=&quot;list_of_productss&quot; v-for=&quot;p, i in all_products&quot; :id= &quot;`ep_${i}`&quot; @blur=&quot;handleBlur&quot; @focusout=&quot;handleFocusout&quot; &gt;
&lt;td contenteditable data-field=&quot;p_code&quot; :id= &quot;`ep_code_${i}`&quot; &gt; {{ p.p_code }} &lt;/td&gt;
&lt;td contenteditable data-field=&quot;p_fullname&quot; :id= &quot;`ep_fn_${i}`&quot;&gt; {{ p.p_fullname }} &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 17:16:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定