我找到了搜索詞的描述,並使用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