Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Saving dropdown options to server

coder_

New Coder
So far, I have the following code:

HTML:
<!DOCTYPE html>
<html>
  <head>
    <title>Company HR</title>
    <style>
      h1 {
        font-family: verdana;
        text-align: center;
        font-size: 50px;
      }

      table,
      th,
      td,
      select {
        border: 1px solid black;
        border-collapse: collapse;
        font-family: Verdana;
        font-size: 20px;
      }

      .centre {
        margin-left: auto;
        margin-right: auto;
      }
    </style>
  </head>
  <body>
    <h1>Company Employee List</h1>
    <table class="centre" id="employee-table">
      <thead>
        <tr>
          <th>Name</th>
          <th>Role</th>
          <th>Active</th>
          <th>Assigned</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>

    <script>
      const employees = [
        { name: "John Doe", role: "Co-CEO/Editor", active: "Yes", assigned: "n/a" },
        { name: "Jane Doe", role: "CEO", active: "Yes", assigned: "n/a" },
        { name: "Bob Doe", role: "Co-CEO/Animator", active: "No", assigned: "No" },
        { name: "Janet Doe", role: "Co-CEO", active: "No", assigned: "No" },
      ];

      const table = document.getElementById("employee-table").querySelector("tbody");

      function createOption(value, selected) {
        const option = document.createElement("option");
        option.value = value;
        option.textContent = value;
        if (value === selected) {
          option.selected = true;
        }
        return option;
      }

      employees.forEach((employee) => {
        const row = table.insertRow();
        const { name, role, active, assigned } = employee;

        row.insertCell().textContent = name;
        row.insertCell().textContent = role;

        const activeSelect = document.createElement("select");
        activeSelect.appendChild(createOption("Yes", active));
        activeSelect.appendChild(createOption("No", active));
        activeSelect.appendChild(createOption("n/a", active));
        row.insertCell().appendChild(activeSelect);

        const assignedSelect = document.createElement("select");
        assignedSelect.appendChild(createOption("Yes", assigned));
        assignedSelect.appendChild(createOption("No", assigned));
        assignedSelect.appendChild(createOption("n/a", assigned));
        row.insertCell().appendChild(assignedSelect);
      });
    </script>
  </body>
</html>

I need the dropdowns to save, but can't work out how.

Can anyone help?
 
To save them, loop through the rows, assign a unique id for each select item, then save the option values as indicated below.
My example code only works for one row, but you should get an idea of how to proceed.
Also, I have no idea what your back-end (server) looks like, so I am using local Storage, but I presume you will want to do some kind of fetch POST to save them to the server.
Then, at retrieval time, do a fetch GET and reverse the save operation.
Code:
const activeSelect = document.createElement("select");
activeSelect.id='activeSel'  //  you need to do some work here to get a unique id for each row

function saveOptions(){
  let arr1 = [];
  let actives = document.getElementById("activeSel");
  for(let i=0; i < actives.length; i++){
    arr1.push(actives[i].value)
   }
   localStorage.setItem('key1', arr1)
}
This, of course, is a suggested course of action, not the entire solution.
 
Back
Top Bottom