简体   繁体   English

在表中克隆和追加行

[英]cloning and appending row in a table

How can I perform the following action using jquery? 如何使用jquery执行以下操作?

I have a table with three rows and a header row. 我有一个包含三行和一个标题行的表。 something like this: When a user enters Data in "StudentId" field, "StudentFirstName" and "StudentLastName" are populated from database using jquery-Ajax. 像这样:当用户在“ StudentId”字段中输入数据时,将使用jquery-Ajax从数据库中填充“ StudentFirstName”和“ StudentLastName”。 What I want to achieve is, Once a studentId is entered in first row textbox, I want to clone the first row and append it as second row and then clear the data from first row. 我要实现的是,在第一行文本框中输入了StudentId之后,我想克隆第一行并将其附加为第二行,然后从第一行清除数据。

Hers is my Table structure: 她是我的表格结构:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%@ Import Namespace="MyModel.Model" %>
<div>
<table id="MyTableHeaders">
        <tr>
            <th>
                StudentId
            </th>
            <th>
                StudentFirstName
            </th>
            <th>
                StudentLastName
            </th>            
        </tr>
    </table>
</div>

<div>
<table id="MyTableData">
        <tr>
            <td>
                <input name="StudentId"/>
            </td>
            <td>
                <input name="StudentFirstName"/>
            </td>
            <td>
                <input name="StudentLastName"/>
            </td>            
        </tr>    
</table>
</div>

Here is my jquery: 这是我的jQuery:

$('input[name="StudentId"]').focusout(function (e) {
    e.preventDefault();
    var link = '/Student/DisplayStudentDetails';
    $.ajax({
        type: 'POST',
        url: link,
        data: { id: $('input[name="StudentId"]').val() },
        dataType: 'json',
        success: function (result) {
            $('input[name="StudentFirstName"]').val(result.FirstName);
            $('input[name="StudentLastName"]').val(result.LastName);
            $("#MyTableData tr:first").clone().appendTo("#MyTableData");
            $("#MyTableData tr:first").find("input").attr("value", "").blur();
        },
        error: function (result) {
            alert("Failed")
        }
    });
});

I am struggling with clearing data from first row and keeping data in the cloned row. 我正在努力清除第一行中的数据并将数据保留在克隆的行中。 With my code it clears all the rows. 用我的代码清除所有行。

Also, I noticed that cloned rows have the same field names, hence it will update all cloned "StudentFirstName" and "StudentLastName" fields with the studentId entered in first row. 另外,我注意到克隆的行具有相同的字段名称,因此它将使用在第一行中输入的studentId更新所有克隆的“ StudentFirstName”和“ StudentLastName”字段。 How should I approach this? 我应该如何处理?

Any help would be appreciated. 任何帮助,将不胜感激。

    success: function (result) {
        var clone = $("#MyTableData tr:first").clone();
        clone.find('input[name="StudentId"]').val(result.FirstName);
        clone.find('input[name="StudentFirstName"]').val(result.FirstName);
        clone.find('input[name="StudentLastName"]').val(result.LastName);
        $("#MyTableData ").append(clone);
    },

-edit- -编辑-

although, when i've attempted tasks such as this in the past, i've created template rows. 但是,当我过去尝试执行此类任务时,我已经创建了模板行。 a row that already exists but has display:none. 已存在但显示为:none的行。 that way if your table has no values you can always know the template row exists. 这样,如果您的表没有值,您就可以始终知道模板行存在。

-edit- if you want to add the new row at the begining, you can just use prepend instead of append. -edit-如果要在开始时添加新行,则可以只使用prepend而不是append。 or the second row would be $("#MyTableData tr").first().after(clone) 否则第二行将是$(“#MyTableData tr”)。first()。after(clone)

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

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