简体   繁体   English

如何通过单击按钮来编辑表格中的单元格

[英]how to edit the cell in a table by click a button

Hi i am trying to display the one div portion when user click the image icon in a table but i have facing a problem with javascript code here i can get the div portion in the first row only not in other if i clicked icon from second row still i can get the div portion in the first row it self tell me how to display the div portion individually when user click on image icon sorry for long explanation , give me the tips for resolve it Thanks in Advance... current output 嗨,我尝试在用户单击表格中的图像图标时显示一个div部分,但是我在这里遇到了javascript代码问题,如果我单击第二行的图标,则只能在第一行中获得div部分我仍然可以在第一行中获得div部分,它可以自我告诉我当用户单击图像图标时如何分别显示div部分抱歉,请稍候解释,请给我解决的技巧。提前感谢... 当前输出

Html part: HTML部分:

<table  class="table" id="tab_logic">

            <tr>
                <th>SNO</th>
                <th>Task</th>
                <th>Comments</th>
                <th>Edit</th>
            </tr>
            <% for(var i=0; i < status_info.length; i++) { %>

                <tr>
                <td><%= i+1 %></td> 
                <td><%= status_info[i].Task_Name %></td>
                <td id="new_edit"><%- status_info[i].Cmnt_Details %>
                    <div id="panel">//here i am hideing this div when user click on the image this will be display in specific cell
                        <form action="/StatusData" method="POST">
                            <p id="demo"></p>
                <textarea placeholder="write something..." id="aa" name="aa"></textarea></form>
                </div>  
                </td>
                <td><input type="image" src="/Images/edit.png" alt="button" style="width: 20px" onclick="myFunction()"></td>
                </tr>
            <% } %>
        </table>

Js part: js部分:

        <style>
                #panel {
                display: none;
                }

                </style>
        <script>
    function myFunction() {
        document.getElementById("panel").style.display = "block";
        console.log("hi")
    }


    <script>

If by div portion you mean al the TD's inside the TR where the user clicked you need to get the parentNode of the clicked TD. 如果按div部分表示您是在用户单击的TR中包含TD,则需要获取所单击TD的parentNode。 Using that you can transform it however you want. 使用它可以根据需要进行转换。

You can use id of the div equals to the iteration number and send this iteration number to the myFunction as an argument and use it to change its style. 您可以使用div的id等于迭代次数,并将该迭代次数作为参数发送给myFunction并使用它来更改其样式。

Your code will look like this: 您的代码将如下所示:

<table  class="table" id="tab_logic">

        <tr>
            <th>SNO</th>
            <th>Task</th>
            <th>Comments</th>
            <th>Edit</th>
        </tr>
        <% for(var i=0; i < status_info.length; i++) { %>

            <tr>
            <td><%= i+1 %></td> 
            <td><%= status_info[i].Task_Name %></td>
            <td id="new_edit"><%- status_info[i].Cmnt_Details %>
                <div id='<%= i %>' class="panel">//here i am hideing this div when user click on the image this will be display in specific cell
                    <form action="/StatusData" method="POST">
                        <p id="demo"></p>
            <textarea placeholder="write something..." id="aa" name="aa"></textarea></form>
            </div>  
            </td>
            <td><input type="image" src="/Images/edit.png" alt="button" style="width: 20px" onclick="myFunction(<%= i %>)"></td>
            </tr>
        <% } %>
    </table>

Javascript: Javascript:

<script>
function myFunction(divId) {
    document.getElementById(divId).style.display = "block";
    console.log("hi")
}
<script>

Update: 更新:

I have added class named panel to the div. 我已经将名为panel的class添加到div中。 You can now add css using class name. 现在,您可以使用类名添加CSS。

CSS: CSS:

<style>
     .panel {
     display: none;
     }

</style>

You can make content editable with below options. 您可以使用以下选项使内容可编辑。

and

onchange event you can update or show/hide the contents. onchange事件,您可以更新或显示/隐藏内容。

<div contenteditable="true">
  This text can be edited by the user.
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM