使用addEventListener修改表ejs不会停止
我有一个带有nodejs的动态生成数据的表,必须像浏览器中的Excel一样修改它。我有一个让我感兴趣的元素(第一个通过了),但整个桌子的其余部分都不感兴趣我。我该如何停止?我已经尝试了e.stoppropagation()
我生成的表,我只使用“目前暂时的派别
<% for (var i=0; i<nomenclature.length; i++){ %>
<tr>
<td>
<a class="bouton" href="/nomenclature/<%= nomenclature[i].idPiece %> ">Ajout bon de commande</a>
</td>
<td id="idPiece<%=i%>" name="idPiece">
<%=nomenclature[i].idPiece %>
</td>
<td contenteditable='true' id="denomination<%=i%>" class="denomination1">
<%=nomenclature[i].denomination %>
</td>
<td contenteditable='true' class="n_piece_plan">
<%=nomenclature[i].n_piece_plan %>
</td>
<td contenteditable='true'>
<%=nomenclature[i].rev %>
</td>
<td contenteditable='true'>
<%=nomenclature[i].qte %>
</td>
<td contenteditable='true'>
<%=nomenclature[i].unite %>
</td>
<td contenteditable='true'>
<%=nomenclature[i].matiere %>
</td>
</tr><script>
My script see below
</script>
<% } %>
</table>
我的脚本在ejs
var all = document.getElementsByTagName("td");
for (var j=1; j<all.length;j++){
all[j].addEventListener("input", function(e) {
const newData = this.innerHTML;
const myID = document.getElementById("idPiece<%=i%>").innerHTML;
const myColumn = this.className;
const data = {newData, myID, myColumn};
const options = {
method: 'POST',
headers: {
'Content-Type':'application/json'
},
body: JSON.stringify(data)
};
fetch('./nomenclature/inter/modification', options);
e.stopPropagation();
}, false);
}
我的控制器
exports.nomenclatureModification = function(request, response){
let newData = request.body.newData;
let myID = request.body.myID;
newData = newData.replace(/(\r\n|\n|\r|\s)/gm,"");
myID = myID.replace(/(\r\n|\n|\r|\s)/gm,"");
let myColumn = request.body.myColumn;
console.log(newData +" ahiiiiii "+ myColumn +" rooooh "+ myID);
/*
connection.query('UPDATE nomenclature SET ?? = ? WHERE idPiece = ?;',[myColumn,newData,myID], function(error, resultSQL){
if (error) throw error;
else{
console.log("modif enregistrer en bdd");
}
});
*/
response.redirect('/nomenclature');
}
第一个元素通过控制台。log是我修改的一个,因此可以用一个元素进行SQL查询(我可以轻松地更改每个列),但因为我的脚本嵌套在循环中 想停止的其余
部分
- 。
- [i]我更改的行)也打印出我 每个脚本都可以更改ID
<td contenteditable='true' id="<%=i%>" class="n_piece_plan">
<%=nomenclature[i].n_piece_plan %>
</td>
- 以访问该ID,
for (var j=0; j<all.length;j++){
all[j].addEventListener("input", function(e) {
const newData = this.innerHTML;
let stringID = 'idPiece' + this.id;
const myID = document.getElementById(stringID).innerHTML;
const myColumn = this.className;
const data = {newData, myID, myColumn};
const options = {
method: 'POST',
headers: {
'Content-Type':'application/json'
},
body: JSON.stringify(data)
};
fetch('./nomenclature/inter/modification', options);
e.stopPropagation();
}, false);
}
我现在可以像Excel表一样修改我的表格,并在DB中进行更新。
I have a table of dynamically generated data with nodejs and have to modify it like an excel in the browser. I have the element that interest me (the first one passed) but also the rest of the whole table which doesn't interest me. How can I make it stop? I've tried e.stopPropagation()
My generated table, I've only worked with "denomination for the moment
<% for (var i=0; i<nomenclature.length; i++){ %>
<tr>
<td>
<a class="bouton" href="/nomenclature/<%= nomenclature[i].idPiece %> ">Ajout bon de commande</a>
</td>
<td id="idPiece<%=i%>" name="idPiece">
<%=nomenclature[i].idPiece %>
</td>
<td contenteditable='true' id="denomination<%=i%>" class="denomination1">
<%=nomenclature[i].denomination %>
</td>
<td contenteditable='true' class="n_piece_plan">
<%=nomenclature[i].n_piece_plan %>
</td>
<td contenteditable='true'>
<%=nomenclature[i].rev %>
</td>
<td contenteditable='true'>
<%=nomenclature[i].qte %>
</td>
<td contenteditable='true'>
<%=nomenclature[i].unite %>
</td>
<td contenteditable='true'>
<%=nomenclature[i].matiere %>
</td>
</tr><script>
My script see below
</script>
<% } %>
</table>
My script in ejs
var all = document.getElementsByTagName("td");
for (var j=1; j<all.length;j++){
all[j].addEventListener("input", function(e) {
const newData = this.innerHTML;
const myID = document.getElementById("idPiece<%=i%>").innerHTML;
const myColumn = this.className;
const data = {newData, myID, myColumn};
const options = {
method: 'POST',
headers: {
'Content-Type':'application/json'
},
body: JSON.stringify(data)
};
fetch('./nomenclature/inter/modification', options);
e.stopPropagation();
}, false);
}
My controller
exports.nomenclatureModification = function(request, response){
let newData = request.body.newData;
let myID = request.body.myID;
newData = newData.replace(/(\r\n|\n|\r|\s)/gm,"");
myID = myID.replace(/(\r\n|\n|\r|\s)/gm,"");
let myColumn = request.body.myColumn;
console.log(newData +" ahiiiiii "+ myColumn +" rooooh "+ myID);
/*
connection.query('UPDATE nomenclature SET ?? = ? WHERE idPiece = ?;',[myColumn,newData,myID], function(error, resultSQL){
if (error) throw error;
else{
console.log("modif enregistrer en bdd");
}
});
*/
response.redirect('/nomenclature');
}
The first element passed thru the console.log is the one I've modified and so my sql query can be made with that one (I can change easily every column). But because my script is nested inside the loop (to obtain idPiece[i] of my changed row) it also prints out the rest which I would like to stop.
EDIT: found a solution:
- got the script out of the loop
- changed my table with a id for each
<td contenteditable='true' id="<%=i%>" class="n_piece_plan">
<%=nomenclature[i].n_piece_plan %>
</td>
- script change to access that id
for (var j=0; j<all.length;j++){
all[j].addEventListener("input", function(e) {
const newData = this.innerHTML;
let stringID = 'idPiece' + this.id;
const myID = document.getElementById(stringID).innerHTML;
const myColumn = this.className;
const data = {newData, myID, myColumn};
const options = {
method: 'POST',
headers: {
'Content-Type':'application/json'
},
body: JSON.stringify(data)
};
fetch('./nomenclature/inter/modification', options);
e.stopPropagation();
}, false);
}
I can now modify my table in the browser exactly like an Excel table and update that in my db. Performance is ok.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论