0

I have created a course container and I want to populate the options by fetching them from the backend

(URL: https://ffcc-app.herokuapp.com/get/courses)

<div class="container">
        <form action="#" method="POST">
        <h2>Course 1</h2>
        <div class="select-box">
          <div class="options-container">
            <select class="option" id="one">
              <option value="">Select a course.. </option>
              
            </select>
          </div>
          <div class="selected">
            Select Courses
          </div>
          <div class="search-box">
            <input type="text" placeholder="Search..." />
          </div>
        </div>
        </form>
</div>        

Also when I type any character in the search box it should filter the options. Can someone help me on how to fetch the data from the URL to populate the options list?

PS: I am familiar with Fetch API for fetching the data

2 Answers 2

1

You could do something like this:

let result = {};
let allCourses = {};

function filterCourses() {
  var filter = document.getElementById("inputFilter").value;
  result.courses = allCourses.courses.filter(x => x.title.includes(filter))
  let select = document.getElementById("one");
  
  while (select.firstChild) {
        select.removeChild(select.firstChild);
  }
  
  for (let i = 0; i < result.courses.length; i++){
        let option = document.createElement("option");
        option.value = result.courses[i]._id;
        option.text = result.courses[i].title;
        select.appendChild(option);
    }
}

async function getCourses() {
    let url = 'users.json';
    try {
        let res = await fetch("https://ffcc-app.herokuapp.com/get/courses");
        return await res.json();
    } catch (error) {
        console.log(error);
    }
}

async function renderCourses() {
    allCourses = await getCourses();
    result = Object.assign({}, allCourses);
    let select = document.getElementById("one");
    for (let i = 0; i < result.courses.length; i++){
        let option = document.createElement("option");
        option.value = result.courses[i]._id;
        option.text = result.courses[i].title;
        select.appendChild(option);
    }
}

renderCourses()
<div class="container">
            <form action="#" method="POST">
            <h2>Course 1</h2>
            <div class="select-box">
              <div class="options-container">
                <select class="option" id="one">
                  <option value="">Select a course.. </option>
                  
                </select>
              </div>
              <div class="selected">
                Select Courses
              </div>
              <div class="search-box">
                <input type="text" id="inputFilter" placeholder="Search..." onchange="filterCourses()"/>
              </div>
            </div>
            </form>
    </div>  

Explanation: after loaded all the courses, I made an option for each course (with _id as option value and title as option text). This is made by function renderCourses.

Then the filterCourses function: basically it takes the value from input and looks for an option that contains what you typed in input. If you clean the input, function returns all the courses.

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

3 Comments

Thanks a lot this works just fine. I want to make the search case insensitive, so can u tell me how can I do that?
@SidharthMishra it's just necessary to use toUpperCase. So: allCourses.courses.filter(x => x.title.toUpperCase().includes(filter.toUpperCase()))
Yep got it! I guess it works for both either uppercase or lowercase right?
0

You have to get your api, create a function to manipulate your DOM, pass the data to this function so that it renders the content inside the select tag.

This is just for the select tag.

const select = document.querySelector('.option')

fetch('https://ffcc-app.herokuapp.com/get/courses')
  .then(response => response.json())
  .then(data => {
    data.courses.forEach(course => render(course))
  });

function render(course) {
  const opt = document.createElement('option')
  opt.value = course.title
  const content = document.createTextNode(`${course.title}`)
  opt.appendChild(content)
  select.appendChild(opt)
}
<div class="container">
        <form action="#" method="POST">
        <h2>Course 1</h2>
        <div class="select-box">
          <div class="options-container">
            <select class="option" id="one">
              <option value="">Select a course.. </option>
              
            </select>
          </div>
          <div class="selected">
            Select Courses
          </div>
          <div class="search-box">
            <input type="text" placeholder="Search..." />
          </div>
        </div>
        </form>
</div>  

1 Comment

And the filter function? =)

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.