如何使用javascript / jquery在每一行中添加编辑和删除按钮

时间:2016-08-30 10:13:06

标签: javascript jquery html css

我想在每一行中添加编辑和删除按钮,并希望使用javascript编辑和删除行。我该怎么做请检查下面的jsfiddle。

$(equipmentTable.table().container()).on('keyup', 'tfoot tr th.textFilter input', function () {
    equipmentTable.column($(this).data('index') + 1)
                  .search(this.value)
                  .draw();
});

的javascript

First Name:<br>
<input type="text" id="fName" /><br>
Last Name: <br> 
<input type="text" id="lName" /><br>
Gender: <br>
<input type="text" id="gender" /><br>
Age: <br>
<input type="text" id ="age" /> <br>
<input type="button" id ="display" value="Display" /><br>
<table id= "table" border="1">
<tr>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
</tr>
</table>

http://jsfiddle.net/xnWSV/

1 个答案:

答案 0 :(得分:1)

使用jQuery结束,如果单击编辑,则重新填充字段,然后再次单击“显示”以解决问题。这是一个非常简单的实现,但如果您要对此进行扩展,我建议使用Angular或某种模板库。

http://jsbin.com/jevejetofo/edit?html,js,console,output

JavaScript的:

(function setup() {
    var selectedRow = null;
    var keys = ["fName","lName","gender","age"];

    function resetValues(fName,lName,gender,age){
        forEachInput(function(key){
            $("#"+key).val('');
        });
    }

    function forEachInput(callback){
        for(var i = 0; i < keys.length; i++) {
            callback(keys[i]);
        }
    }

    function createRow(){
        var row    = $(".tr_clone").clone()
        var newRow = row.appendTo("table").removeClass("tr_clone").fadeIn(1000);
        forEachInput(function(key){
            newRow.find("."+key).text($("#"+key).val());
        });
        resetValues();
        selectedRow = null;
    }

    function applyValues(row){
        forEachInput(function(key){
            row.find("."+key).text($("#"+key).val());
        });
    }


    function editRow(row){
        forEachInput(function(key){
            $("#"+key).val(row.find("."+key).text());
        });
        selectedRow = row;
    }

    $("#display").on("click", function () {
        if(selectedRow === null){
            createRow();
            resetValues();
        } else {
            applyValues(selectedRow);
            resetValues();
            selectedRow = null;
        }
    });

    $("table").delegate("button").on("click",function(e){
        e.preventDefault(); 
        if($(e.target).hasClass('edit')){
            editRow($(e.target).parent("td").parent("tr"));
        } else {
           $(e.target).parent("td").parent("tr").fadeOut(1000).remove();            
        }
    });

})();

HTML

<script src="https://code.jquery.com/jquery-2.0.3.js"></script>First Name:<br>
<input type="text" id="fName" /><br>
Last Name: <br> 
<input type="text" id="lName" /><br>
Gender: <br>
<input type="text" id="gender" /><br>
Age: <br>
<input type="text" id ="age" /> <br>
<input type="button" id="display" value="Display" /><br>
<table id= "table" border="1">
<tr>
<th>First Name</th>
  <th>Last Name</th>
<th>Gender</th>
<th>Age</th>
</tr>

      <tr class="tr_clone" style="display:none;">
        <td class="fName"></td>
        <td class="lName"><input type="text" autofocus placeholder="location" name="location" ></td>
        <td class="gender"><input type="text" placeholder="Start Date" name="datepicker_start" class="datepicker"></td>
        <td class="age"><input type="text" placeholder="End Date" name="datepicker_end" class="datepicker"></td>
        <td class="buttons"><button class="delete">Delete</button> | <button class="edit">Edit</button></td>
    </tr>
</table>