我找到了搜索词的描述,并使用JQuery自动填充获得了该词。现在我想为每个描述显示搜索词(单词)的数量。如何计算来自db的搜索内容
<strike>
if (!string.IsNullOrEmpty(searchTerm))
{
//searching description and name in TrainingTopic
var modeltrainingtopic = db.TrainingTopicRepository.Get().Where(x => x.Description.ToLower().Contains(searchTerm.ToLower()) || x.Name.ToLower().Contains(searchTerm.ToLower())).Select(x => new SearchViewModel { Id = x.Id, IsTrainingTopic=true, Description = x.Description, Name = x.Name, RedirectionLink = "" }).Take(10).ToList();
//searching description, content and name in SubTopic
var modelsubtopic = db.SubTopicRepository.Get().Where(x => x.Description.ToLower().Contains(searchTerm.ToLower()) || x.Name.ToLower().Contains(searchTerm.ToLower()) || x.SubTopicContent.ToLower().Contains(searchTerm.ToLower())).Select(x => new SearchViewModel { Id = x.Id,IsSubTopic=true, Description = x.Description, Content = x.SubTopicContent, RedirectionLink = "" }).Take(10).ToList();
//searching description, content and name in SubSubTopic
var modelsubsubtopic = db.SubSubTopicRepository.Get().Where(x => x.Description.ToLower().Contains(searchTerm.ToLower()) || x.Name.ToLower().Contains(searchTerm.ToLower()) || x.Content.ToLower().Contains(searchTerm.ToLower())).Select(x => new SearchViewModel { Id = x.Id,IsSubSubTopic=true, Name = x.Name, Description = x.Description, Content = x.Content, RedirectionLink = "" }).Take(10).ToList();
return modeltrainingtopic.Concat(modelsubtopic).Concat(modelsubsubtopic);
}
</strike>
在我的视图模型中,我已经添加了StringCount。现在我想在每个对象(modeltrainingtopic,modelsubtopic,modelsubsubtopic)中添加搜索项的计数。下面我怎么添加的jQuery
$(function() {
var loc = window.location.pathname.split('/')[1];
$("#srch-term").autocomplete({
source: function (request, response) {
$.ajax({
url: "/" + loc + "/api/ResourceLanding/SearchString?searchTerm=" + request.term,
type: "Get",
success: function (data) {
if (!data.length) {
var result = [
{
id: 0,
label: 'No matches found'
}
];
response(result);
}
else {
response($.map(data, function (item) {
return { label: item.Description, value: item.Description, Id:item.Id, IsTrainingTopic: item.IsTrainingTopic, IsSubTopic: item.IsSubTopic, IsSubSubTopic: item.IsSubSubTopic, Name: item.Name, Content: item.Content };
}))
}
}
})
}
}).autocomplete("instance")._renderItem = function (ul, item) {
var icon;
if (item.IsTrainingTopic) {
icon = '<i class="fa fa-globe" aria-hidden="true"></i>'
}
else if (item.IsSubTopic) {
icon = '<i class="fa fa-cog" aria-hidden="true"></i>'
}
else if (item.IsSubTopic) {
icon = '<i class="fa fa-cogs" aria-hidden="true"></i>'
}
if (icon !== undefined) {
return $("<li>")
.append("<div style='width:100%' data-id=" + item.value + ">" + icon + " " + item.label + "</div>")
.appendTo(ul);
}
else {
return $("<li>")
.append("<div style='width:100%' data-id=" + item.value + ">" + item.label + "</div>")
.appendTo(ul);
}
};
});
请帮我
感谢您的答复。但我必须在每个创建的对象中存储搜索词计数。我想知道如何编写代码。请帮助我,如果你有任何想法 – Sriram
我已编辑我的答案。请检查。希望它会帮助你。 –
我们将这些方法作为列表存储在列表中。如何在列表中直接找到搜索词? – Sriram