简体   繁体   English

如何使用Bootstrap的下拉菜单对要显示的项目进行排序

[英]How to sort items to display by using Bootstrap's Dropdown menu

I have an app that displays recipes on the index page (mywebsite.com/recipes). 我有一个在索引页面(mywebsite.com/recipes)上显示食谱的应用程序。 I want to sort them either by date or by popularity, and have a button directly on the page so the user can choose how he wants to sort the recipes. 我想按日期或受欢迎程度对它们进行排序,并直接在页面上有一个按钮,以便用户可以选择他想对食谱进行排序的方式。 I am using bootstrap's dropdown menu, which uses anchor tags (would a select menu be better suited?). 我正在使用引导程序的下拉菜单,该菜单使用锚标记(选择菜单更适合吗?)。

Here's the dropdown menu: 这是下拉菜单:

<div class="dropdown indexDropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
      Sort by
      <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
      <li><a href="#(WHAT SHOULD I PUT HERE)">Most recent</a></li>
      <li><a href="#(WHAT SHOULD I PUT HERE)">Most popular</a></li>
    </ul>
  </div>

Here's the route I want to use when "Date" is chosen or when nothing is clicked by the user (default sort): 这是选择“日期”或用户未单击任何内容时要使用的路线(默认排序):

//INDEX ROUTE (SORT BY DATE)
router.get("/WHAT SHOULD I PUT HERE", function(req, res){
    if(req.query.search && req.xhr) {
       const regex = new RegExp(escapeRegex(req.query.search), 'gi');
       // Get all recipes from DB
       Recipe.find({title: regex}).limit(9).sort({"createdAt": -1}).exec(function(err, foundRecipes){
          if(err){
             console.log(err);
          } else {
             res.status(200).json(foundRecipes);
          }
       });
   } else {
       // Get all recipes from DB if no search input
       Recipe.find({}).sort({"createdAt": -1}).limit(9).exec(function(err, foundRecipes){
          if(err){
              console.log(err);
          } else {
             if(req.xhr) {
               res.json(foundRecipes);
             } else {
               res.render("recipes/index",{recipes: foundRecipes});
             }
          }
       });
   }
});

Here's the route I want to use when "Popularity" is chosen or when nothing is clicked by the user (default sort): 这是选择“人气”或用户未单击任何内容时要使用的路线(默认排序):

//INDEX ROUTE (SORT BY POPULARITY)

    router.get("/WHAT SHOULD I PUT HERE", function(req, res){
        if(req.query.search && req.xhr) {
           const regex = new RegExp(escapeRegex(req.query.search), 'gi');
           // Get all recipes from DB
           Recipe.find({title: regex}).limit(9).sort({"createdAt": -1}).exec(function(err, foundRecipes){
              if(err){
                 console.log(err);
              } else {
                 res.status(200).json(foundRecipes);
              }
           });
       } else {
           // Get all recipes from DB if no search input
           Recipe.find({}).sort({"numberOfPins": -1}).limit(9).exec(function(err, foundRecipes){
              if(err){
                  console.log(err);
              } else {
                 if(req.xhr) {
                   res.json(foundRecipes);
                 } else {
                   res.render("recipes/index",{recipes: foundRecipes});
                 }
              }
           });
       }
    });

How should I do this? 我应该怎么做? What href should I assign to each anchor tag in the dropdown menu? 我应该在下拉菜单中为每个锚标记分配什么href? Should I just add something like href="/recipes?_sort=POPULARITY"? 是否应该添加href =“ / recipes?_sort = POPULARITY”之类的内容?

Thanks a lot! 非常感谢!

<a href='/route1'></a> creates a get request for route1. <a href='/route1'></a>创建对<a href='/route1'></a>的获取请求。 So I created two separate routes (route1 and route2) for sorting mongo data. 因此,我创建了两个单独的路由(route1和route2)来对mongo数据进行排序。

<a href='/route1'>Sort by Popularity</a>
 <a href='/route2'>Sort by Date</a>

router.get("/route1",function(){
   Sort data by popularity
})

router.get("/route2",function(){
   Sort data by Date
})

Works for me. 为我工作。 May not be an optimum method but it just works 可能不是最佳方法,但它确实有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript在下拉菜单中显示下拉菜单项? - How to display dropdown menu items inside the dropdown using javascript? 如何使用带引导程序的把手在下拉列表中选择和显示项目 - How to select and display items in a dropdown using handlebars with bootstrap 我希望先显示侧边栏,然后在单击按钮并使用引导程序将下拉列表添加到菜单项的侧面后折叠 - I want the side bar display first and collapse after clicking the button and adding the dropdown to the side of the menu items using bootstrap 如何右对齐/对齐引导程序下拉菜单项? - How to right align / justify bootstrap dropdown menu items? 如何使用jQuery突出显示引导程序下拉菜单的活动菜单? - How to highlight active menu of a dropdown menu of bootstrap using jquery? Bootstrap下拉菜单项缺少样式 - Bootstrap dropdown menu items missing style 显示下拉菜单时如何使导航栏响应 - How to make navbar responsive while displaying the dropdown's menu items 如何为移动屏幕的导航栏项目添加切换按钮,并使用 HTML、CSS 和 Bootstrap v4 将它们切换为下拉菜单 - How can I add toggle buttons for navbar items for mobile screen and toggle them for dropdown menu using HTML, CSS and Bootstrap v4 如何在引导程序中创建下拉菜单 - How to create dropdown menu in bootstrap 如何使用引导程序显示另一个导航栏而不是下拉菜单 - How to show another navbar instead of dropdown menu using bootstrap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM