2017-04-07 1 views
1

私がこれまでに実施したもの:javascriptを使用してテーブル内の特定の行を削除するにはどうすればよいですか?

  1. は、入力フィールドに値を入力し、「追加」ボタンをクリックし、入力した値が新しい行に追加されます。
  2. [削除]ボタンをクリックすると、すべての行が削除されています。私が実装する必要がどのよう

  1. チェックボックスは、すべての行に追加されるはずです。
  2. チェックボックスをオンにして[削除]ボタンをクリックすると、その特定の行だけが削除され、複数のチェックボックスを選択すると機能するはずです。 3.追加ボタンをクリックして入力フィールドをクリアします。 誰でもこれをチェックして、それを行う方法を教えてもらえますか?

//Javascript code to Add new rows onclick of a button and to delete row . 
 
function addMoreRows() { 
 

 
    var user = document.getElementById('user_id').value; 
 
    var date = document.getElementById('date').value; 
 
    var color = document.getElementById('color').value; 
 
    var table = document.getElementById('tbl_id'); 
 

 
    var row = table.insertRow(); 
 

 
    var userName = row.insertCell(0); 
 
    var Date = row.insertCell(1); 
 
    var Color = row.insertCell(2); 
 
    var checkbox = row.insertCell(3); 
 

 
    userName.innerHTML = user; 
 
    Date.innerHTML = date; 
 
    Color.innerHTML = color; 
 

 
} 
 

 
function deleteMoreRows(tableID) { 
 

 
    var table = document.getElementById(tableID); 
 
    var rowCount = table.rows.length; 
 

 
    for (var i = 0; i < rowCount; i++) { 
 

 
     table.deleteRow(i); 
 
     rowCount--; 
 
     i--; 
 
    } 
 
}
<!-- HTML markup for the input fields and table . --> 
 
<form align="center" method="GET"> 
 
    Enter your name : <input type="text" name="users" id="user_id" value="name" onfocus="if(this.value == 'name') {this.value=''}" onblur="if(this.value == ''){this.value ='name'}"><br> 
 
    Select the Date : <input type="date" id="date"><br> 
 
    Select your favorite color: 
 
    <select id="color" required> 
 
     <option value="yellow">yellow</option> 
 
     <option value="red">red</option> 
 
    </select> 
 
    <br> 
 
    <br> 
 
    <input type="button" id="mysubmit" value="Add Row" onClick="addMoreRows()"> 
 
    <input type="button" id="delete" value="Delete" onClick="deleteMoreRows('tbl_id')"> 
 
</form> 
 
<br> 
 
<br> 
 
<table id="tbl_id" style="text-align:center" align="center" valign="top"> 
 
    <thead> 
 
     <tr> 
 
     <th style="width:200px;">Name</th> 
 
     <th style="width:200px;">Date</th> 
 
     <th style="width:200px;">Color</th> 
 
     </tr> 
 
    </thead>

+0

残りのロジックを実装しようとしているときに、あなたが得ているすべての問題? –

+0

F12キーを押してJavaScriptコンソールを開きます。削除をクリックすると、どのようなエラーが表示されますか? – mkaatman

+0

@mkaatman、私はエラーを表示していませんが、削除をクリックするとすべての行が削除されます。チェックボックスの選択時に削除する特定の行が必要です。しかし、私はどのように各行にチェックボックスを追加し、検証するかわかりません。 – Vivek

答えて

2

これはあなたのために働くなら、私に教えてください:

//Javascript code to Add new rows onclick of a button and to delete row . 
 

 
var rowId = 0; 
 

 
function addMoreRows() { 
 

 
    var user = document.getElementById('user_id').value; 
 
    var date = document.getElementById('date').value; 
 
    var color = document.getElementById('color').value; 
 
    var table = document.getElementById('tbl_id'); 
 

 
    var row = table.insertRow(); 
 

 
    var rowBox = row.insertCell(0); 
 
    var userName = row.insertCell(1); 
 
    var Date = row.insertCell(2); 
 
    var Color = row.insertCell(3); 
 
    var checkbox = row.insertCell(4); 
 

 
    rowBox.innerHTML = '<input type="checkbox" id="delete' + getRowId() + '">'; 
 
    userName.innerHTML = user; 
 
    Date.innerHTML = date; 
 
    Color.innerHTML = color; 
 

 
} 
 

 
function deleteMoreRows(tableID) { 
 

 
    var table = document.getElementById(tableID); 
 
    var rowCount = table.rows.length; 
 
    var selectedRows = getCheckedBoxes(); 
 

 
    selectedRows.forEach(function(currentValue) { 
 
     deleteRowByCheckboxId(currentValue.id); 
 
    }); 
 
} 
 

 
function getRowId() { 
 
    rowId += 1; 
 
    return rowId; 
 
} 
 

 
function getRowIdsFromElements($array) { 
 
    var arrIds = []; 
 

 
    $array.forEach(function(currentValue, index, array){ 
 
    arrIds.push(getRowIdFromElement(currentValue)); 
 
    }); 
 

 
    return arrIds; 
 
} 
 

 
function getRowIdFromElement($el) { 
 
    return $el.id.split('delete')[1]; 
 
} 
 

 
//ref: http://stackoverflow.com/questions/8563240/how-to-get-all-checked-checkboxes 
 
function getCheckedBoxes() { 
 
    var inputs = document.getElementsByTagName("input"); 
 
    var checkboxesChecked = []; 
 

 
    // loop over them all 
 
    for (var i=0; i < inputs.length; i++) { 
 
    // And stick the checked ones onto an array... 
 
    if (inputs[i].checked) { 
 
     checkboxesChecked.push(inputs[i]); 
 
    } 
 
    } 
 

 
    // Return the array if it is non-empty, or null 
 
    return checkboxesChecked.length > 0 ? checkboxesChecked : null; 
 
} 
 

 
//ref: http://stackoverflow.com/questions/4967223/delete-a-row-from-a-table-by-id 
 
function deleteRowByCheckboxId(CheckboxId) { 
 
    var checkbox = document.getElementById(CheckboxId); 
 
    var row = checkbox.parentNode.parentNode;    //box, cell, row, table 
 
    var table = row.parentNode; 
 

 
    while (table && table.tagName != 'TABLE') 
 
     table = table.parentNode; 
 
    if (!table) return; 
 
    table.deleteRow(row.rowIndex); 
 
}
<!-- HTML markup for the input fields and table . --> 
 
<form align="center" method="GET"> 
 
    Enter your name : <input type="text" name="users" id="user_id" value="name" onfocus="if(this.value == 'name') {this.value=''}" onblur="if(this.value == ''){this.value ='name'}"><br> 
 
    Select the Date : <input type="date" id="date"><br> 
 
    Select your favorite color: 
 
    <select id="color" required> 
 
     <option value="yellow">yellow</option> 
 
     <option value="red">red</option> 
 
    </select> 
 
    <br> 
 
    <br> 
 
    <input type="button" id="mysubmit" value="Add Row" onClick="addMoreRows()"> 
 
    <input type="button" id="delete" value="Delete" onClick="deleteMoreRows('tbl_id')"> 
 
</form> 
 
<br> 
 
<br> 
 
<table id="tbl_id" style="text-align:center" align="center" valign="top"> 
 
    <thead> 
 
     <tr> 
 
     <th style="width:200px;">Delete</th> 
 
     <th style="width:200px;">Name</th> 
 
     <th style="width:200px;">Date</th> 
 
     <th style="width:200px;">Color</th> 
 
     </tr> 
 
    </thead>

+0

ありがとうございました!これはまさに私が探していたものです。それは完全に動作します。再度、感謝します。 – Vivek

+0

私はあなたのために働いてうれしいです。ハッピーコーディングの友人:) –

関連する問題