Issue
My code use 2 same value of dropdown list of Product : in html and javascript.
When i edit my product data, need double update in 2 place.
In html:
<td ><select name="product_text" id="new_product">
<option value="A" id="option-1">A</option>
<option value="B" id="option-2">B</option>
<option value="C" id="option-3">C</option>
</select>
</td>
In Javascript
product.innerHTML='<select id="product_text'+no+'">\
<option value="A" id="option-1">A</option>\
<option value="B" id="option-2">B</option>\
<option value="C" id="option-3">C</option>\
</select>' ;
How can i use function in javascript to reduce display dropdown database again in javascript?
My code link: https://jsfiddle.net/bvotcode/b6wj85dt/21/
HTML
<meta name="Table dropdown number va date" content="dropdown number va date">
<html>
<body>
<div id="wrapper">
<table align='center' cellspacing=2 cellpadding=5 id="data_table" border=1>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Date</th>
</tr>
<tr>
<td ><select name="product_text" id="new_product">
<option value="A" id="option-1">A</option>
<option value="B" id="option-2">B</option>
<option value="C" id="option-3">C</option>
</select>
</td>
<td><input type="number" id="new_quantity"></td>
<td><input type="date" id="new_date"></td>
<td><input type="button" class="add" onclick="add_row();" value="Add Row"></td>
</tr>
</table>
</div>
</body>
</html>
Javascript
function edit_row(no)
{
document.getElementById("edit_button"+no).style.display="none";
document.getElementById("save_button"+no).style.display="block";
var product=document.getElementById("product_row"+no);
var quantity=document.getElementById("quantity_row"+no);
var date=document.getElementById("date_row"+no);
var product_data=product.innerHTML;
var quantity_data=quantity.innerHTML;
var date_data=date.innerHTML;
product.innerHTML='<select id="product_text'+no+'">\
<option value="A" id="option-1">A</option>\
<option value="B" id="option-2">B</option>\
<option value="C" id="option-3">C</option>\
</select>' ;
document.getElementById("product_text"+no).value = product_data;
quantity.innerHTML="<input type='number' id='quantity_text"+no+"' value='"+quantity_data+"'>";
date.innerHTML="<input type='date' id='date_text"+no+"' value='"+date_data+"'>";
}
function save_row(no)
{
var product_val=document.getElementById("product_text"+no).value;
var quantity_val=document.getElementById("quantity_text"+no).value;
var date_val=document.getElementById("date_text"+no).value;
document.getElementById("product_row"+no).innerHTML=product_val;
document.getElementById("quantity_row"+no).innerHTML=quantity_val;
document.getElementById("date_row"+no).innerHTML=date_val;
document.getElementById("edit_button"+no).style.display="block";
document.getElementById("save_button"+no).style.display="none";
}
function delete_row(no)
{
document.getElementById("row"+no+"").outerHTML="";
}
function add_row()
{
var new_product=document.getElementById("new_product").value;
var new_quantity=document.getElementById("new_quantity").value;
var new_date=document.getElementById("new_date").value;
var table=document.getElementById("data_table");
var table_len=(table.rows.length)-1;
var row = table.insertRow(table_len).outerHTML="<tr id='row"+table_len+"'><td id='product_row"+table_len+"'>"+new_product+"</td><td id='quantity_row"+table_len+"'>"+new_quantity+"</td><td id='date_row"+table_len+"'>"+new_date+"</td><td><input type='button' id='edit_button"+table_len+"' value='Edit' class='edit' onclick='edit_row("+table_len+")'> <input type='button' id='save_button"+table_len+"' value='Save' class='save' onclick='save_row("+table_len+")'> <input type='button' value='Delete' class='delete' onclick='delete_row("+table_len+")'></td></tr>";
document.getElementById("new_product").value="";
document.getElementById("new_quantity").value="";
document.getElementById("new_date").value="";
}
Solution
First off, consider posting this over at https://codereview.stackexchange.com/ to get an extensive review of your code, because there is a lot that could be done better. Two suggestions for now:
Avoid hard coding HTML inside your JS (this question is a first good step)
There is normally no need to give everything a unique ID. Instead you usually can reference everything in relation to each other, possibly with help of classes. For example, the edit/save/delete buttons can look up which table row their in to find the elements they needs.
In this specific case you could clone the existing select from the HTML:
val productText = document.getElementById("product_text").cloneNode();
// Change the ID
productText.id = "product_text" + no;
// Set the value
productText.value = product_data;
// Clear the parent
// (There are better ways to empty an element, but I'm just using this for now)
product.innerHTML = "";
// Insert the cloned element
product.appendChild(productText);
Answered By - RoToRa

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.