我试图根据用户的特点对用户列表进行分组。例如:我想将所有家庭医学提供者分组并显示他们的名字:
家庭医学:
- 项目清单
- 项目清单
- 列出项目
这是我的js控制器:
exports.provider_list = function(req, res, next) {
provider.find()
.sort([['SpecialtyName', 'ascending']])
.exec(function (err, list_providers) {
if (err) { return next(err); }
//Successful, so render
res.render('provider_list', { title: 'Provider List', list_providers: list_providers});
});
};
帕格列表:
extends layout
block content
h1= title
ul.list-group
each val in list_providers
li
a(href=val.url) #{val.SpecialtyName}
| #{val.ProviderName}
else
li There are no provider.
答案 0 :(得分:0)
据我所知,您希望列出按专业名称分组的所有提供商名称。
我猜您的数据(list_providers
)看起来像这样:
[{
"ProviderName": "P1",
"SpeacialyName": "S1",
"url" : "url_1"
}, {
"ProviderName": "P2",
"SpeacialyName": "S2"
}, {
"ProviderName": "P3",
"SpeacialyName": "S3"
}, {
"ProviderName": "P3",
"SpeacialyName": "S1"
}, {
"ProviderName": "P4",
"SpeacialyName": "S2"
}]
如果您的数据如上所述。您可以修改数据,将其转换为:
[{
"SpeacialyName": "S1",
"url": "url_1",
"ProviderNames": ["P1", "P3"]
}, {
"SpeacialyName": "S2",
"ProviderNames": ["P2", "P4"]
}, {
"SpeacialyName": "S3",
"ProviderNames": ["P3"]
}
]
这是后端的转换代码:
//Successful, so render
var providers = {}
for (var i = 0; i < list_providers.length; i++) {
var item = list_providers[i];
if (!providers[item.SpeacialyName]) {
providers[item.SpeacialyName] = item;
providers[item.SpeacialyName].ProviderNames = [item.ProviderName];
} else {
providers[item.SpeacialyName].ProviderNames.push(item.ProviderName)
}
delete providers[item.SpeacialyName].ProviderName;
}
//convert object to array
var providersArray = [];
for (const item in providers) {
providersArray.push(providers[item])
}
res.render('provider_list', { title: 'Provider List', list_providers: providersArray });
最后,这里是要列出的pug文件
ul.list-group
each val in list_providers
li
a(href=val.url) #{val.SpeacialyName}
ul
each name in val.ProviderNames
li
a(href="")=name
else
li There are no speacialy.
else
li There are no provider.