【问题标题】:Deletion in List | Spring删除列表 |春天
【发布时间】:2013-06-26 18:33:01
【问题描述】:

我得到的列表如下:

<table border=1 align="center" width="90%" id="skilltableId">
        <thead>
            <tr>
                <th>Skill</th>
                <th>Levels</th>
            </tr>
        </thead>
        <tbody class="agentSkills" id="agentSkills">
            <core:forEach items="${personForm.skillList}" var="skill" varStatus="status">
                <tr  id='${skill.skillDbid}' class='trClass' onclick="selectSkill(this.id)">

                    <td>${skill.skillName}
                    <form:hidden path="skillList[${status.index}].skillName" id="hiddenSkillname"/>
                      </td>
                     <form:hidden path="skillList[${status.index}].skillDbid" id="hiddenSkilldbid"/>
                    <td colspan="2">${skill.skillLevel}
                    <form:hidden path="skillList[${status.index}].skillLevel" id="hiddenSkillLevel"/>
                     </td>
                </tr>
            </core:forEach>

        </tbody>
    </table>
    <input type="button" id="delBtn">

样本输出:

Skill                Levels

English                   5
Hindi                     4
Telugu                    8

数据将按照上述格式以表格格式列出。如果我单击“tr”,它应该突出显示该行,然后在单击按钮 (id="delBtn") 后,应该从列表中删除特定数据。我怎样才能做到这一点?

【问题讨论】:

    标签: javascript jsp spring-mvc jstl jsp-tags


    【解决方案1】:

    jQuery 就是这样。

    $(document).ready(function() {
    
        // Click Event on the tr
        $(".trClass").click(function() {
            //Remove the highlight if already selected
            if($(this).attr("data-selected") == "selected") {
                $(this).css("background-color", "InitialColor");
                $(this).attr("data-selected", "notSelected");
                return;
            }
    
            // Highlight the tr
            $(this).css("background-color", "ColorYourWant");
            $(this).attr("data-selected", "selected");
        });
    
        //Click Event on the button
        $("#delBtn").click(function() {
            // Delete the tr
            $('tr[data-selected="selected"]').remove();
    
            //AJAX can be add here if you want to do something like deleting the row from the database
        });
    
    })
    

    如果您使用此解决方案,您可以删除您的tr 上的onclick="selectSkill(this.id)

    【讨论】:

    • 我尝试使用脚本..它正在从表中删除。但它也应该从列表中删除..如何做到这一点..
    • 你在说哪个列表。 Java 列表?您是从数据库中获取列表吗?
    【解决方案2】:

    它对我有用..类似地,我有一种情况,我想从另一个 jsp 添加一行..如何做到这一点..我尝试添加的脚本,但不绑定到列表..这里是一个示例代码。

    var table = window.parent.parent.parent.personTabs.document.getElementById("skilltableId").getElementsByTagName("tbody")[0];
        //alert("table:"+table);
            //var tbody = window.parent.parent.parent.personTabs.document.getElementById("skillRow");
            //alert(tbody);
            var row = document.createElement("tr");
            //alert(row);
            var data1 = document.createElement("td");
            //alert(data1);
            data1.appendChild(document.createTextNode(document.getElementById("selecetedValue").value));
            alert(window.parent.parent.parent.personTabs.document.getElementById("hiddenSkillname").value);
            window.parent.parent.parent.personTabs.document.getElementById("hiddenSkillname").value=window.parent.parent.parent.personTabs.document.getElementById("hiddenSkillname").value+"\t"+document.getElementById("selecetedValue").value;
            var data2 = document.createElement("td");
            //alert(data2);
            data2.appendChild (document.createTextNode(document.getElementById("type").value));
            window.parent.parent.parent.personTabs.document.getElementById("hiddenSkillLevel").value=window.parent.parent.parent.personTabs.document.getElementById("hiddenSkillLevel").value+"\t"+document.getElementById("type").value;
            var data3 = document.createElement("td");
            //alert(data2);
            data3.appendChild (document.createTextNode(document.getElementById("commonDbid").value));
            window.parent.parent.parent.personTabs.document.getElementById("hiddenSkilldbid").value=window.parent.parent.parent.personTabs.document.getElementById("hiddenSkilldbid").value+"\t"+document.getElementById("commonDbid").value;
            row.appendChild(data1);
            row.appendChild(data2);
            //row.appendChild(data3);
            table.appendChild(row);
            window.parent.parent.document.getElementById("popup1").style.display="none";
    

    【讨论】:

    • 嗨,你的代码工作了。但是添加新行没有工作..我有一个同一张表,我有三个按钮(添加、删除、编辑)[第一个 jsp]。当我点击“添加”时按钮'我正在显示一个弹出窗口,其中有两个文本框和一个按钮[另一个 jsp]。应该将文本框中的这两个值添加到列表中,即,第一个 jsp 和一个新行应该生成。如何做到这一点..?而且当我单击新生成的行时,onclick 功能应该可以工作..谢谢 adv.!!
    • @Hi jean...我需要处理我在jsp中得到的列表..就像我想在新行中添加到列表对象,删除一行,编辑一行..这些应该在不去控制器的情况下发生..我正在尝试.直到现在都没有成功..让我知道..谢谢您的建议..
    猜你喜欢
    • 2016-01-29
    • 2011-09-23
    • 1970-01-01
    • 2011-11-28
    • 2020-10-13
    • 2016-05-25
    • 2014-08-22
    • 2013-11-02
    • 2014-03-13
    相关资源
    最近更新 更多