简体   繁体   English

更改前端NodeJs / Express / Jade中的routes变量的顺序

[英]Change the order of a routes variable in the front end NodeJs/Express/Jade

Novice to NodeJS and Express but lets say I have this route for mywebsite.com/tournaments. NodeJS和Express的新手,但可以说我有mymyksite.com/tournaments的这条路线。

router.get('/tournaments', function (req, res) {
    TournamentController.getAllTournaments(function (err, docs) {
        if (err) {
            //render error
        } else {
            res.render('tournaments', {
                data: {
                    title: 'mysite',
                    command: 'tournaments',
                    user: req.session.user,
                    tournaments: docs
                }
            });
        }
    });
});

data.tournaments is an array of tournaments in order of their date. data.tournaments是按日期顺序排列的一系列锦标赛。 Lets say in the front end I have a select/option form where the user can choose date/prize/players as the order to sort the tournaments by. 可以说在前端,我有一个选择/选项表单,用户可以在其中选择日期/奖项/玩家作为对比赛进行排序的顺序。 How can I sort the data.tournaments without having to call another route or refresh the page? 如何在不调用其他路线或刷新页面的情况下对data.tournaments进行排序? I'm using Jade on the front end. 我在前端使用Jade。

You can always sort them directly in the Browser via Javascript, either do it yourself or use a plugin like datatables . 您始终可以通过Javascript在浏览器中直接对它们进行排序,可以自己完成,也可以使用诸如datatables之类的插件进行。

If you don't wanna do that but do it on the server, you'll need an ajax call for that and a route that handles the sorting (based on the parameters you pass), and afterwards change the DOM according to the response. 如果您不想在服务器上执行此操作,则需要为此进行ajax调用,并需要一个处理排序的路由(基于传递的参数),然后根据响应更改DOM。 This goes without refreshing the page, but you'll need a route for that, or change the existing route and extend your controller to take optional parameters, something like 这无需刷新页面,但您需要为此设置路由,或更改现有路由并扩展控制器以采用可选参数,例如

router.get('/tournaments/:sort?', function (req, res) {
    TournamentController.getAllTournaments(req.param('sort'), function (err, docs) {
        /* ... */
    });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM