1

I have a table where I can move rows up or down which I have put an id on the . Now I need to save the the new order of the table. I am thinking I need to save the rows[index] and but I have no experience with jquery. My table is id="rtbl", the button to call the function is "sroute". I have an alert in the function but that is not working. Just new to jquery, what do I need to do to make this work?

$("#sroute").click(function(){ alert("Hello") $("#rtbl tr").each(function() { var val1 = $(t.rows[i].cells[0]).text(); alert(val1) ; i++; }); }); 
<button id="sroute">Save Order</button> <table id='rtbl'> <tr><th>Invoice</th></tr> <tr id='789'><td>789</td></tr> <tr id='123'><td>123</td></tr> <tr id='456'><td>456</td></tr> </table> 

So the goal is to update the db field 'stopnum' where invoice 789 stopnum would be 1, invoice 123 stopnum would be 2, and invoice 456 stopnum would be 3.

1 Answer 1

2

Rough estimate for the object your db update API will need. You'll have to adjust it for that.

window.onload = (function(){ document.getElementById('sroute').onclick=( function(){ var arrayOfInvoices = $('#rtbl tr:not(:eq(0))').map( function(i,el) { return ({ invoice: el.id, stopnum: i+1 }) } ).get(); alert(JSON.stringify(arrayOfInvoices)); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="sroute">Save Order</button> <table id='rtbl'> <tr><th>Invoice</th></tr> <tr id='789'><td>789</td></tr> <tr id='123'><td>123</td></tr> <tr id='456'><td>456</td></tr> </table>

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

12 Comments

Nothing happened. I think it has to do with stopnum being a db field (shown in the jquery by user120242)
What are you expecting to happen? Are you submitting this to an API endpoint? You will need to know what the endpoint expects as input.
Right now I expected to see the three id's in the console.
Right, when you click the button it outputs: [{"invoice":"789","stopnum":1},{"invoice":"123","stopnum":2},{"invoice":"456","stopnum":3}]. That's what you wanted right?
I don't get that. Nothing happens. So I put alert("HERE"); above the var arrayOf... and I did not get the alert box.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.