Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

javascript - Building a table that pulls data from MongoDB that filters in real time, and sorts alphabetically

As the title suggests, that is what im attempting to do, but are at a lost. My best attempt is the following: This is the nodejs code that renders the HTML page and gets the user input from HTML

app.get('/search/species', (req,res) => {
  diseaseModel.find({matched: current_matched}, function (err,disease){
  res.render('species', {
    diseaseList: disease,

  });
});
})

app.post('/search/species', async(req,res) => {
  var desc = req.body.desc;
  await diseaseModel.updateMany({matched: current_matched, "$text":{"$search" : `"${desc}"`}}, {$inc: {matched: 1}});
    current_matched ++;

    res.redirect(302, '/search/species');
  }
}
});
})

This is the HTML page that I uses to collect user input and return the filtered options:

  <form action = "/search/species" method = "POST" id="submitMessage" class="field has-addons">
              <input class="form-control input is-primary" name = "desc" id = "desc" type="text" style="width: 100%;" placeholder="Enter a Description" required/>
              <button type="submit" class="button  is-primary" id="send">Send</button>
          </form>
      </div>

  </div>
</section>

      <table class = "content-table" align="center" style="margin: 0px auto;">

        <thead>
          <tr>
            <th>Name</th>
            <th>Species</th>
            <th>Vector</th>
            <th>Agent</th>
          </tr>
        </thead>

      <tbody>
      <%diseaseList.forEach(disease =>{%>
        <tr data-href= 'http://localhost:8080/info?id=<%=disease.id%>'>
        <th><%=disease.diseaseName%></th>
        <th><%=disease.species%></th>
        <th><%=disease.vector%></th>
        <th><%=disease.agent%></th>
        </tr>
      <% })%>
    </tbody>
  </table>
    </div>
</section>

<script>
$(document).ready(function() {
  $(document.body).on("click", "tr[data-href]", function() {
    window.location.href = this.dataset.href;
  })
});

But this doesn't refresh in real time, as in when i type a character, it doesn't start filtering. Would I be able to do this via AJAX? Also how would I list MongoDB data out in the table in alphabetical order?

question from:https://stackoverflow.com/questions/65859650/building-a-table-that-pulls-data-from-mongodb-that-filters-in-real-time-and-sor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...