根据路由URL设置select的值

Set value of select based on the route URL

本文关键字:select 的值 设置 URL 路由      更新时间:2024-01-08

在我的主干应用程序中,我有一个选择菜单,可以使用路线过滤列表中的一些员工,它可以按部门进行过滤。

例如localhost/#filter/HRlocalhost/#filter/Accounting

routes: {
    "filter/:type": "urlFilter"
},
urlFilter: function (type) {     
    this.directory.filterType = type;
    this.directory.trigger("change:filterType");
},

然后在我看来:

filterByType: function () {
    if (this.filterType === "all") {
        this.collection.reset(contacts);
        app.navigate("filter/all");
    } else {
        this.collection.reset(contacts, { silent: true });
        var filterType = this.filterType,
            filtered = _.filter(this.collection.models, function (item) {
                return item.get("type").toLowerCase() === filterType;
            });
        this.collection.reset(filtered);
        app.navigate("filter/" + filterType);
    }
},  

但是,如果您直接访问其中一个URL,则选择菜单将不同步。

如何使选择与路线保持同步,其中选择选项值等于"全部"、"人力资源"answers"会计"?

将onchange处理程序添加到您的列表中,并相应地更新URL(通过添加当前列表项的参数值)。防止在更改URL时重新加载页面(谷歌搜索)。确保在使用参数打开URL时(即在页面加载时)更新当前列表项。