Responsive Data Table Form With Increment Option Using Html

Viewed 40

I'm looking for data table with increment option (Flask project).

This table is for admin managmenet - Create order with selected product and save it.

(Once click on save only the selected product will be transfer to orders page)

I need to do it more efficient because I have like 100 products.

There is 2 main problems:

  1. The increment only affect the first input
  2. I don't want copy-paste the td,buttun and input per product..

Ataached my code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>

  <style>
table {
  margin-bottom:200px;
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  border: 1px solid #ddd;
}

th, td {
  text-align: center;
  padding: 8px;
}

tr:nth-child(even){background-color: #f2f2f2}
</style>


  <body>

<div style="overflow-x:auto;">
  <table>
    <tr>
      <th>Product</th>
      <th>Price</th>
      <th>Quantity</th>
    </tr>
    <tr>
      <td>Product A</td>
      <td>20$</td>
      <td><button onclick="decrement()">-</button>
      <input id="demoInput" type=number min=0 max=1000 style="text-align:right; width: 15%;">
      <button onclick="increment()">+</button>
    </tr>
    <tr>
      <td>Product B</td>
      <td>20$</td>
      <td><button onclick="decrement()">-</button>
      <input id="demoInput" type=number min=0 max=1000 style="text-align:center; width: 15%;">
      <button onclick="increment()">+</button>
      </td>
    </tr>
  </table>

</div>

  </body>

<script>
    function increment() {
        document.getElementById('demoInput').stepUp();
    }
    function decrement() {
        document.getElementById('demoInput').stepDown();
    }
</script>

</html>

1 Answers

I would prrefer to use React for this purposes, but if you want to use vanillaJS I would do something like this:

const increment = (element) => {
  element.closest('tr').getElementsByTagName('input')[0].stepUp();
  
} 

const decrement = (element) => {
  element.closest('tr').getElementsByTagName('input')[0].stepDown(); // gets input of the tr from where's the call
}


// in your case that's the items from the database sended via flask to JS.
const test_products = {} // stores items in { item: price } order.


const create_test_products = () => {  // inserts hundred items to the test_products object
  
  for (var item=1; item<101; item++){
    
    var price = item + 2 // just an example to make different prices
    
    test_products[item] = price;

  }`enter code here`

}


const create_markup = () => { // will create a tablerow foreach item in the object
  
    const table = document.getElementById('main_table'); 
     for (var item in test_products){
   var tableRow = `<tr>
      <td>${item}</td>
      <td>${test_products[item]}$</td>
      <td><button onclick="decrement(this)">-</button>
      <input type=number min=0 max=1000 style="text-align:right; width: 15%;">
      <button onclick="increment(this)">+</button>
    </tr>`
    table.insertAdjacentHTML('beforeend', tableRow);
    }


}

window.onload = create_test_products();
window.onload = create_markup();
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>

  <style>
table {
  margin-bottom:200px;
  border-collapse: collapse;
  border-spacing: 0;
  width: 100%;
  border: 1px solid #ddd;
}

th, td {
  text-align: center;
  padding: 8px;
}

tr:nth-child(even){background-color: #f2f2f2}
</style>


  <body>

<div style="overflow-x:auto;">
  <table id="main_table">
    <tr>
      <th>Product</th>
      <th>Price</th>
      <th>Quantity</th>
    </tr>
  </table>

</div>

  </body>


</html>

Related