0

s.no. | description

  1. abcd1
  2. abcd2
  3. abcd3

i want to add more rows through input. now what i want is when i will add another row. let say {describtion="abcd4"}

then the above grid will become

s.no. | description

  1. abcd4
  2. abcd1
  3. abcd2
  4. abcd3

meaning s.no. field got updated and new row will be added at top. adding a new row on top is no issue but how could i update s.no. at same time, here i want to ask is there any specific way to do this.

12
  • is using an ordered list an option? Commented Jan 6, 2016 at 13:23
  • From the description of the grid tag: "This tag is ambiguous, please don't use it." Your question is also ambiguous. HTML doesn't have "grids", only tables. There are probably various packages that implement spreadsheet-like grids, but in order to help you, we must know how you create the grid in the first place. Commented Jan 6, 2016 at 13:25
  • I would suggest to remove the algorithm tag, one can hardly think of this as an algorithm. Once it is clear what this grid represents, the population is straightforward. Commented Jan 6, 2016 at 13:29
  • Shouldn't last line read 4. abcd3 instead of 4. abcd4? Commented Jan 6, 2016 at 13:32
  • @M Oehm @trincot , i choose description as single field here but in actual there are multiple fields like(testname,testtype, testcity etc. 20 field) but when i will add a row in the grid only s.no. will gonna change means it will get a new serial with 1 as newest added row in grid and so on and rest of the grid will not load. reloading a entire grid is not recommended for me because grid may have more than 10000 records Commented Jan 6, 2016 at 13:38

3 Answers 3

1

Here is a solution that adds rows at the top of a table and keeps the numbers updated:

document.querySelector('#btnadd').addEventListener('click', function () {
  var inp = document.querySelector('#inpadd');
  var descr = inp.value;
  if (descr === '') return; // do not add empty values
  var grid = document.querySelector('#grid');
  // first increment all row numbers
  for (var i = 1, row; row = grid.rows[i]; i++) {
    row.cells[0].textContent = i+1;
  }
  // add new row
  var row = grid.insertRow(1);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  cell1.textContent = 1;
  cell2.textContent = descr;
  // clear input
  inp.value = "";
});
New description: <input type="text" id="inpadd"><button id="btnadd">Add</button>
<table id="grid">
  <tr><th>s.no.</th><th>description</th></tr>
<table>

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to insert new text description in the beginning of the ordered list, you can use 'insertBefore' javascript code:

list.insertBefore(entry, list.firstChild);

It should add the new text in the beginning of the list. Refer below code if it helps your problem.

<!DOCTYPE html>
<html>
<body>

<p>Input the text description and click 'Add Description' button to insert in list:</p>

<form>
Description Text:<br>
<input type="text" name="description" id="description">
<br>
<input type="button" value="Add Description" onclick='appendDescription()'>
</form>

<ol id="desclist">
<li>abcd1</li>
<li>abcd2</li>
<li>abcd3</li>
</ol>

<script>
function appendDescription(){
var description= document.getElementById('description').value;
var entry = document.createElement('li');
entry.appendChild(document.createTextNode(description));

var list = document.getElementById('desclist');

list.insertBefore(entry, list.firstChild);
}
</script>
</body>
</html>

1 Comment

if some of the grid cell contain buttons and hyperlinks then how ordered list will work.. thank you for your time
0
function pushAtStarting(array, element){
    array.unshift(element); // new element has s.no = 0
    for (index in array){
       array[index].sno++
    }
}
var array = [];
pushAtStarting(array, {sno: 0, description: "abc"});

it works if your grid is java script arrays and elements are json elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.