function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
element1.name="chkbox[]";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount;
var cell3 = row.insertCell(2);
var element2 = document.createElement("input");
element2.type = "text";
element2.name = "item";
element2.size = "40";
cell3.appendChild(element2);
var cell4 = row.insertCell(3);
var element3 = document.createElement("input");
element3.type = "text";
element3.name = "qty";
element3.style ="text-align: right";
cell4.appendChild(element3);
}
I am creating a set of columns using this code and by using a button action I am adding such rows continuously to the page.
Now along with this, I want to simultaneously add these values to SQL database.
Plus whenever i revisit this page I should have the previously entered data.
The setup have 3 buttons Delete Row, Add Row and Save.
I want to get all the rows into Database once I click Save.
Related
I have the following js code in my jsp. Im not able to retain those textbox values below after the page is refreshed. Any idea how to retain the values?
Im calling this js function onclick of a html button so that it dynamically adds the row to the table.
<script>
function addRow(tableID) {
if(validateForm()==true){
document.getElementById('cruisedowntime2').style.display = "block";
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
/* var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
element1.name = "chkbox[]";
element1.defaultChecked="true";
cell1.appendChild(element1);*/
var cell0 = row.insertCell(0);
var element0=document.createElement("select");
element0.name="cruiselinedropdown";
var dropdownlist1=document.getElementById("droplist1");
var defaultselected=dropdownlist1.options[dropdownlist1.selectedIndex].value;
//window.location.replace("http://localhost:8085/Bridge_Downtime_Utility/Servlet?var="+defaultselected);
<%Map<String, String> map = new HashMap<String,String>();
for (int i = 0; i < crsenamelist.size(); i++) {
map.put(crsenamelist.get(i),crsecodelist.get(i));
}
%>
element0.options.add( new Option(defaultselected,defaultselected,true,true) );
element0.options.add( new Option("--","") );
<%
for(Map.Entry m:map.entrySet()){ %>
element0.options.add( new Option("<%=m.getValue()%>","<%=m.getValue()%>") );
cell0.appendChild(element0);<%}%>
var textbox3=document.getElementById('starttime');
var textbox7=document.getElementById('startdate');
var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.title="Date: YYYY-MM-DD";
element2.type = "text";
element2.name = "starttimetextbox";
element2.value=textbox7.value+" "+textbox3.value;
cell2.appendChild(element2);
var textbox5=document.getElementById('endtime');
var textbox9=document.getElementById('enddate');
var cell3 = row.insertCell(2);
var element3 = document.createElement("input");
element3.title="Date: YYYY-MM-DD";
element3.type = "text";
element3.name = "endtimetextbox";
element3.value=textbox9.value+" "+textbox5.value;
cell3.appendChild(element3);
var textbox1=document.getElementById('descrip');
var cell4 = row.insertCell(3);
var element4 = document.createElement("input");
element4.type = "text";
element4.name = "descriptextbox";
if(textbox1.value==''){element4.value="-";}
else
element4.value=textbox1.value;
cell4.appendChild(element4);
//cell4.contenteditable=true;
/* var addRowBox = document.createElement("input");
addRowBox.setAttribute("type", "button");
addRowBox.setAttribute("value", "Edit");
var cell5 = row.insertCell(5);
cell5.appendChild(addRowBox);*/
var deleteRowBox = document.createElement("input");
deleteRowBox.setAttribute("type", "button");
deleteRowBox.setAttribute("value", "X");
deleteRowBox.setAttribute("onclick","SomeDeleteRowFunction(this)");
var cell6 = row.insertCell(4);
cell6.appendChild(deleteRowBox);
}}
</script>
You can store data and then load from localStorage if you want it to persist after page refresh.
To keep your data upon page refresh you need to save it somewhere, for future (after page reload) access.
Store it somewhere at your backend, and then access your, for instance, REST service upon page load.
Store it in localStorage\sessionStorage in browser.
There are no other ways to keep data without actually keeping it :)
I have a javascript function where rows are added dynamically.
<script language="javascript">
// Add row to the HTML table
function addRow() {
var table = document.getElementById('my_table'); //html table
var rowCount = table.rows.length; //no. of rows in table
var columnCount = table.rows[0].cells.length; //no. of columns in table
var row = table.insertRow(rowCount); //insert a row
var cell1 = row.insertCell(0); //create a new cell
var element1 = document.createElement("input"); //create a new element
element1.type = "checkbox"; //set the element type
element1.setAttribute('id', 'newCheckbox'); //set the id attribute
element1.setAttribute('checked',true); //set checkbox by default checked
cell1.appendChild(element1); //append element to cell
var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "text";
element2.setAttribute('id', 'newInput'); //set the id attribute
element2.setAttribute('name', 'sl'+rowCount);
element2.setAttribute('style', 'width: 50px');
cell2.appendChild(element2);
var cell3 = row.insertCell(2);
var element3 = document.createElement("input");
element3.type = "textarea";
element3.setAttribute('rows', '4');
element3.setAttribute('cols','40');
element3.setAttribute('id', 'newInput'); //set the id attribute
element3.setAttribute('name', 'discription'+rowCount);
cell3.appendChild(element3);
var cell4 = row.insertCell(3);
var element4 = document.createElement("input");
element4.type = "text";
element4.setAttribute('id', 'newInput'); //set the id attribute
element4.setAttribute('name', 'quantity'+rowCount);
cell4.appendChild(element4);
var cell5 = row.insertCell(4);
var element5 = document.createElement("input");
element5.type = "text";
element5.setAttribute('id', 'newInput'); //set the id attribute
element5.setAttribute('name', 'price'+rowCount);
cell5.appendChild(element5);
var cell6 = row.insertCell(5);
var element6 = document.createElement("input");
element6.type = "text";
element6.setAttribute('id', 'newInput'); //set the id attribute
element6.setAttribute('name', 'CST'+rowCount);
element6.setAttribute('style', 'width: 50px');
cell6.appendChild(element6);
var cell7 = row.insertCell(6);
var element7 = document.createElement("select");
var optarr = ['vat1','vat2','vat3','vat4','vat5','vat6'];
for(var i = 0;i<optarr.length;i++)
{
var opt = document.createElement("option");
opt.text = optarr[i];
opt.value = optarr[i];
opt.className = optarr[i];
element7.appendChild(opt);
}
element7.setAttribute('id', 'vat5'); //set the id attribute
element7.setAttribute('name','tax'+rowCount);
element7.setAttribute('value','vat5');
cell7.appendChild(element7);
}
In the last "cell7" i have drop-down along with options,i am selecting only one option.But i need multiple options to be selected. For example: vat1 and vat3 once,vat3 and vat5 and vat2 once like these.I tried element7.setAttribute("multiple","").But it is turning into a list in which i dont needed it.So i tried "jquery Multiselect" but it is out of reach for me because i really dont know about jquery and ajax that much.Any help in this one will be really appreciated.
Edit:After using "setAttribute('multiple','')
I don't know what should go wrong. I copied your example and added the call to set the multiple attribute and it seems to work.
http://plnkr.co/edit/6yAVtHyVwAWBFd3TBTc7?p=preview
var element7 = document.createElement("select");
element7.setAttribute('multiple', '');
var optarr = ['vat1','vat2','vat3','vat4','vat5','vat6'];
for(var i = 0;i<optarr.length;i++)
{
var opt = document.createElement("option");
opt.text = optarr[i];
opt.value = optarr[i];
opt.className = optarr[i];
element7.appendChild(opt);
}
var container = document.getElementById('container');
container.appendChild(element7);
Perhaps check if you made a typo in your own code, that somehow didn't make it to the example posted here.
EDIT: You feel that the default HTML representation of a multiple select is ugly. I can't say i don't agree. That said, this is standart HTML and the representation do change when you see it on a PC, Mac, iPad, Android Phone, etc.
Sounds like you would get the best visual "upgrade" by using a css framework. There are many of them out there. I can name two of the most popular ones:
Bootstrap and
Foundation
I am adding rows to a table through DOM elements and innerHTML. It works, only problem is cell3. The background color won't show. I believe it's because cell3 has an appended element. Help?
var table = document.getElementById("vessel_tab");
var rowCount = table.rows.length;
var row = table.insertRow();
//row.style.backgroundColor="#7F9EBE"; doesn't work as bg for entire row
var cell1 = row.insertCell(0);
cell1.style.backgroundColor="#7F9EBE"; working
cell1.innerHTML = "<font color='#000'><strong>EMS 345</strong></font>";
cell1.colSpan = 2;
var cell3 = row.insertCell(1);
cell3.style.backgroundColor="#7F9EBE"; !NOT WORKING
var element1 = document.createElement("input");
element1.type = "checkbox";
cell3.innerHTML = "<img title='remove' src='images/delete-row-icon1.png' class='sub_icon remove'/> ";
cell3.appendChild(element1);
I want to add textbox dynamically in jsp page on "Add Row" button click. I have written java script to add it. No issues with that. But I am not able to retrieve those values in Servlet page. Any ideas?
Here is the script:
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell3 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "text";
cell3.appendChild(element2);
var cell3 = row.insertCell(2);
var element3 = document.createElement("input");
element3.type = "text";
cell3.appendChild(element3);
var cell4 = row.insertCell(3);
var element4 = document.createElement("input");
element4.type = "text";
cell4.appendChild(element4);
}
And here is the jsp where script is called:
<INPUT type="button"
value="Add Row" onclick="addRow('dataTable')" />
You need to specify the name attribute. It becomes the request parameter name.
You can just give them all the same name.
element.name = "foo";
Or if you want to "respect" IE6/7 users as well
document.createElement('<input type="text" name="foo">');
(jQuery makes it all easier and better crossbrowser compatible)
Then you can access them all as follows in the servlet
String[] foos = request.getParameterValues("foo");
They will appear in the order as they appear in the HTML DOM tree.
I'm trying to design a table in HTML which can add a row when I click "add row" button & the same for deleting an added row if i intend to delete it. I have written a javascript for adding checkbox and text. But I want even combo-boxes in it and I m stuck in the middle. Could you guys just figure it out and let me know how to do that?
This is my JS file.
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
var element2 = document.createElement("input");
element2.type = "text";
cell3.appendChild(element2);
var cell4 = row.insertCell(3);
var element3 = document.createElement("input");
element3.type = "text";
cell4.appendChild(element3);
var cell5 = row.insertCell(4);
//This is where the PROBLEM is!!
var element4 = document.createElement("select");
element4.type = "option";
cell5.appendChild(element4);
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
// JavaScript Document
NOTE: Please dont suggest SERVER_SIDE SCRIPTING. I'm just doing my homework on Java Script :)
This should do the trick:
var cell5 = row.insertCell(4);
//This is where the SOLUTION is!!
var element4 = document.createElement("select");
var option1 = document.createElement("option");
option1.value="1";
option1.innerHTML="sample1";
element4.appendChild(option1);
var option2 = document.createElement("option");
option2.value="2";
option2.innerHTML="sample2";
element4.appendChild(option2);
cell5.appendChild(element4);
Try to think of the outcome that you wish to get. In this case you wish to have HTML that looks like this:
<select>
<option></option>
<option></option>
</select>
So the question is what elements are there? There are three in my example, a select and two options. So in your JavaScript how do you create elements?
var element4 = document.createElement("select");
This creates a select. So how would you create an option?
var option1 = document.createElement("option");
maybe?
how would you add the option to the select? Same way you add the select to the cell.
element4.appendChild(option1);
then create the other options that you need and add the select to the cell.
why not trying
var sel=document.createElement("select");
// repeat this for each option you have
var opt=document.createElement("option");
opt.value="my option value";
opt.text="my option to be displayed";
sel.appendChild(opt);
// end repeat
cell5.appendChild(sel);