后台查询结果 PDFSearchResult实体类:

  [DataContract(Name = "PDFSearchResult")]
public class PDFSearchResult
{ public PDFSearchResult(string title, string fileUrl)
{
Title = title;
FileUrl = fileUrl;
} [DataMember(Name = "title")]
public string Title { get; set; } [DataMember(Name = "fileUrl")]
public string FileUrl { get; set; } }

另外,有一个PagedResult泛型类,PagedResult<T>

  [DataContract(Name = "pagedCollection", Namespace = "")]
public class PagedResult<T>
{
public PagedResult(long totalItems, long pageNumber, long pageSize); [DataMember(Name = "items")]
public IEnumerable<T> Items { get; set; }
[DataMember(Name = "pageNumber")]
public long PageNumber { get; }
[DataMember(Name = "pageSize")]
public long PageSize { get; }
[DataMember(Name = "totalItems")]
public long TotalItems { get; }
[DataMember(Name = "totalPages")]
public long TotalPages { get; } //
// Summary:
// Calculates the skip size based on the paged parameters specified
//
// Remarks:
// Returns 0 if the page number or page size is zero
public int GetSkipSize();
}

后台,采用Web Api (/api/SearchApi/GetPDFSearchResults/), 返回值类型为 PagedResult<PDFSearchResult>, 是Json类型返回值

在页面表现UI层,有两个tab,左边的tab显示的是页面的搜索结果searchResult, 右边的tab显示的是pdf文件的搜索结果PDFSearchResult

页面SearchResult.cshtml如下:

<section id="searchListing" class="row search">
<div> <div class="tabs">
<ul class="tab">
<li data-ui="tab-nav"><a href="#">Web Page Results</a></li>
<li data-ui="tab-nav"><a href="#">PDF Results</a></li>
</ul>
</div>
<div class="tabContent">
<div id="resultsArea" class="search-results" style="display:block;" data-ui="tab-content">
<div id="NoResultsMessage" style="display:none;">
<p class="results-title">No Results For<strong class="search-term"></strong>'</p>
</div>
<div id="HasResultsMessage" style="display:none;">
<p class="results-title">Search Results For<strong class="search-term"></strong>'</p>
</div>
<div class="results" id="SearchResultsContainer">
<article class="row">
<a href="#">
<div>
<h2></h2>
<p></p> <p class="link">Read more</p>
</div>
</a>
</article>
</div>
<ul class="pagination">
<li><a href="#" class="prev"><i class="gi icon-arrow"></i></a></li>
<li class="page"><a href="#"></a></li>
<li><a href="#" class="next"><i class="gi icon-arrow"></i></a></li>
</ul>
</div>
<div id="PDFResultArea" class="search-results" style="display:none;" data-ui="tab-content">
<div id="NoPDFResultsMessage" style="display:none;">
<p class="results-title">No Results For<strong class="search-term"></strong>'</p>
</div>
<div id="HasPDFResultsMessage" style="display:none;">
<p class="results-title">Search Results For<strong class="search-term"></strong>'</p>
</div>
<div class="results" id="SearchPDFResultsContainer">
<article class="row">
<a href="#"> </a>
</article>
</div>
<ul class="pagination">
<li><a href="#" class="prev"><i class="gi icon-arrow"></i></a></li>
<li class="page"><a href="#"></a></li>
<li><a href="#" class="next"><i class="gi icon-arrow"></i></a></li>
</ul> </div>
</div>
</div>
</section>

这里,在页面展示上,应该看上去如下图:

为了在同一个页面,也就是SearchResult.cshtml中实现这个tab功能,我用JQuery写了个tab功能,如下

tab.js

function tabs(state,control,show){

    $(control + "> li").on(state, function(){

        $(this).addClass("on").siblings().removeClass("on");

        var index = $(this).index();

        $(show + "> div").eq(index).show().siblings().hide();
}) $(control + "> li").eq(0).attr("class", "on");
$(show + "> div").eq(0).show();
} tabs("click", ".tab", ".tabContent");

The css style 如下

.tabs {
position: relative;
margin-top: 60px;
margin-bottom: 20px; }
.tabs > ul {
list-style: none;
border-bottom: 1px solid #c0c0c0;
margin-top:;
padding:; }
.tabs > ul > li {
float: left;
margin-bottom: -1px; }
[dir="rtl"] .tabs > ul > li {
float: right; }
.tabs > ul > li a {
display: block;
line-height:;
margin-right: .2rem;
padding: 0 1rem;
border: 1px solid transparent;
text-decoration: none;
cursor: pointer; }
[dir="rtl"] .tabs > ul > li a {
margin-right:;
margin-left: .2rem; }
.tabs > ul > li a:hover {
background: #fff !important;
border: 1px solid #fff !important;
color: #ff7900; }
.tabs > ul > li > [aria-selected="true"], .tabs > ul > li > [aria-selected="true"]:hover {
background: red !important; }
.tabs > ul li.on {
background-color: #fff;
border: 1px solid #c0c0c0;
border-bottom-color: transparent;
color: #454545;
cursor: default; }
.tabs > ul li.on a {
color: #ff7900; }
.tabs > * {
display: none; }
.tabs > ul,
.tabs [tabIndex="0"] {
display: block; }

然后,我写了一个Jquery Plugin来实现PDFSearchResult的展示功能,也就是说JSON结果,展示到SearchResult.cshtml页面上

在这个Jquery plugin中,调用ajax来调用web api (/api/SearchApi/GetPDFSearchResults/),返回json结果,展现到web页面上

view.pdfsearch.js

(function ($) {

    $.fn.PDFSearch = function (options) {

        return this.each(function () {

            var pdfSearch = $(this);

            pdfSearch.pdfResultArea = pdfSearch.find("#PDFResultArea");

            pdfSearch.noPDFResultMessage = pdfSearch.pdfResultArea.find("#NoPDFResultsMessage");
pdfSearch.hasPDFResultMessage = pdfSearch.pdfResultArea.find("#HasPDFResultsMessage");
pdfSearch.PDFResultContainer = pdfSearch.pdfResultArea.find("#SearchPDFResultsContainer"); pdfSearch.searchPDFResultTemplate = pdfSearch.PDFResultContainer.find("article").clone(); pdfSearch.queryField = $("input[name='search']"); pdfSearch.searchTermLabel = $(".search-term"); pdfSearch.oldTerm = undefined;
pdfSearch.term = pdfSearch.queryField.val(); //Paging elements
pdfSearch.paginationWrapper = pdfSearch.pdfResultArea.find('ul.pagination');
pdfSearch.pageTemplate = pdfSearch.paginationWrapper.find('.page').clone();
pdfSearch.pagePrevious = pdfSearch.paginationWrapper.find('.prev');
pdfSearch.pageNext = pdfSearch.paginationWrapper.find('.next'); // Page tracking
pdfSearch.pageSize = 10;
pdfSearch.pageNumber = 1;
pdfSearch.totalPages = 0; // Sets up pagination for search results.
pdfSearch.paginate = function (data) { pdfSearch.totalPages = data.totalPages;
pdfSearch.paginationWrapper.find('.page').remove();
if (pdfSearch.totalPages > 1) {
pdfSearch.pagePrevious.toggle(data.pageNumber > 1);
pdfSearch.pageNext.toggle(data.pageNumber < pdfSearch.totalPages); for (i = 1; i < pdfSearch.totalPages + 1; i++) {
var page = pdfSearch.pageTemplate.clone();
var link = page.find('a');
if (i == data.pageNumber)
page.addClass('active');
page.data('page', i);
link.text(i); pdfSearch.pageNext.parent().before(page).before('\n\r');
} pdfSearch.paginationWrapper.show(0);
} else {
pdfSearch.paginationWrapper.hide(0);
}
} pdfSearch.previous = function (evt) {
return pdfSearch.goToPage(evt, pdfSearch.pageNumber - 1);
} pdfSearch.next = function (evt) {
return pdfSearch.goToPage(evt, pdfSearch.pageNumber + 1);
} pdfSearch.goToPage = function (evt, pageNumber) {
pdfSearch.pageNumber = pageNumber || $(this).data('page');
if (pdfSearch.pageNumber > pdfSearch.totalPages) pdfSearch.pageNumber = pdfSearch.totalPages - 1;
if (pdfSearch.pageNumber < 1) pdfSearch.pageNumber = 1; pdfSearch.searchPDF();
return false;
} // filter change handler
pdfSearch.handleQuery = function (evt) { pdfSearch.term = $(this).val();
if (pdfSearch.oldTerm != pdfSearch.term) {
pdfSearch.oldTerm = pdfSearch.term;
pdfSearch.searchPDF();
}
return false;
} pdfSearch.searchPDF = function () {
pdfSearch.pdfResultArea.addClass("loading"); $.ajax({
type: "GET",
url: "/umbraco/api/SearchApi/GetPDFSearchResults/",
dataType: "json",
data: {
filter: pdfSearch.term,
pageNumber: pdfSearch.pageNumber,
pageSize: pdfSearch.pageSize
},
success: function (data) {
pdfSearch.searchTermLabel.text(pdfSearch.term);
pdfSearch.noPDFResultMessage.hide();
pdfSearch.hasPDFResultMessage.hide();
pdfSearch.paginate(data);
pdfSearch.PDFResultContainer.empty(); if (!data.items || data.items.length == 0) {
pdfSearch.noPDFResultMessage.show();
}
else {
// Binding search result to UI
for (var i = 0; i < data.items.length; i++) {
var pdfResult = data.items[i];
var PdfResultTemplate = pdfSearch.searchPDFResultTemplate.clone(); _populatePDFContent(pdfResult, PdfResultTemplate); pdfSearch.PDFResultContainer.append(PdfResultTemplate);
}
pdfSearch.hasPDFResultMessage.show();
}
},
complete: function () {
pdfSearch.searchTermLabel.text(pdfSearch.term);
pdfSearch.pdfResultArea.removeClass("loading");
}
});
}; _populatePDFContent = function (result, template) { var pdfLinkElem = $(template.find("a"));
pdfLinkElem.attr("href", result.fileUrl);
pdfLinkElem.attr("target", "_blank");
pdfLinkElem.html(result.title); }; pdfSearch.queryField.on("keyup", pdfSearch.handleQuery); // Setup pagination triggers.
pdfSearch.pagePrevious.on("click", pdfSearch.previous);
pdfSearch.pageNext.on("click", pdfSearch.next); // Bind to all future page clicks.
pdfSearch.paginationWrapper.on('click', 'li.page', pdfSearch.goToPage); pdfSearch.searchPDF(); });
}
}(jQuery)); $(function () {
$('section.search').PDFSearch(); });

自己在项目中写的一个Jquery插件和Jquery tab 功能的更多相关文章

  1. 【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能

    在web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面,而重定向到登陆界面的功能. 项目是用springMVC+spring+hibernate实现 (和这个没有多大关系) 第一步: 首先 ...

  2. Web 项目中分享到微博、QQ空间等分享功能

    Web 项目中分享到微博.QQ空间等分享功能 网上有很多的模板以及代码,但是有很多都不能分享内容,简单的测试了下: 以新浪微博为例,文本框中的内容是title属性,下面的链接是url属性,如果你的链接 ...

  3. C# winform项目中ListView控件使用CheckBoxes属性实现单选功能

    C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...

  4. 解决:一个项目中写多个包含main函数的源文件并分别调试运行

    自己在学c++的时候,一个项目中的多个cpp文件默认不允许多个main函数的出现,但是通过选项操作能够指定单个cpp文件进行运行,如下: 1.此时我就想运行第二个cpp文件,我们只需要把其他的两个右键 ...

  5. 在类库或winform项目中打开另一个winform项目的窗体

    假设类库或winform项目为A,另一个winform项目为B.那麽在A中添加一个接口,里面有一个Show方法,然后在B中写一个类b继承这个接口,并重写这个方法,具体内容为弹出某个窗体.然后在A中另一 ...

  6. 在angular项目中使用bootstrap的tooltip插件时,报错Property 'tooltip' does no t exist on type 'JQuery<HTMLElement>的解决方法和过程

    在angular4的项目中需要使用bootstrap的tooltip插件. 1. 使用命令安装jQuery和bootstrap npm install bootstrap jquery --save ...

  7. Laravel 项目中编写第一个 Vue 组件

    和 CSS 框架一样,Laravel 不强制你使用什么 JavaScript 客户端框架,但是开箱对 Vue.js 提供了良好的支持,如果你更熟悉 React 的话,也可以将默认的脚手架代码替换成 R ...

  8. MES项目中出现的一个事务嵌套的使用场景

    昨天在MES项目中,需要在业务逻辑的几个关键点记录错误信息,需要把错误信息写入数据表. 但是由于整个业务逻辑都是包在一个事务模板里面的 比如这样的: WhhTransactionTemplate tr ...

  9. 【Atom】在一个中/大型项目中,那些好用而强大的atom功能

      作为一个学生党,一开始使用atom时候并没有意识到atom一些小功能的巨大作用,直到自己实习参与了项目,才知道这些功能在一个项目中是能极大提高工作效率的开发利器   下面是一位不愿意透露其姓名的彭 ...

随机推荐

  1. 【BZOJ4167】永远的竹笋采摘 分块+树状数组

    [BZOJ4167]永远的竹笋采摘 题解:我们考虑有多少点对(a,b)满足a与b的差值是[a,b]中最小的.以为是随机数据,这样的点对数目可能很少,实测是O(n)级别的,那么我们已知了有这么多可能对答 ...

  2. 【BZOJ2597】[Wc2007]剪刀石头布 最小费用流

    [BZOJ2597][Wc2007]剪刀石头布 Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之 ...

  3. EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:bootstrap弹窗功能的实现

    在web前端的网页设计中,为了展示出简洁的网页风格和美观的效果,往往就会使用弹窗效果 在EasyNVR前端页面录像检索功能时,必然会播放录像,如果单独为播放录像文件排一个界面,用户在使用上会更加繁琐, ...

  4. windowsphone8.1学习笔记之应用数据(二)

    上一篇说了应用数据的应用设置,这篇说说应用文件,应用文件主要分为三种:本地应用文件.漫游应用文件和临时应用文件. 获取根目录方法如下,都是返回一个StorageFolder对象(稍后介绍这个). // ...

  5. 转换(旋转)transform

    div { transform:rotate(180deg); -ms-transform:rotate(180deg); /* IE 9 */ -moz-transform:rotate(180de ...

  6. SQL 中GROUP BY 、ROLLUP、CUBE 关系和区别

    转自:http://www.cnblogs.com/dyufei/archive/2009/11/12/2573974.html 不言自明,看SQL就完全理解了,不需要过多解释,不错,分享之: ROL ...

  7. UVA11426 GCD - Extreme (II) —— 欧拉函数

    题目链接:https://vjudge.net/problem/UVA-11426 题意: 求 ∑ gcd(i,j),其中 1<=i<j<=n . 题解:1. 欧拉函数的定义:满足 ...

  8. BZOJ 3624 [Apio2008]免费道路:并查集 + 生成树 + 贪心【恰有k条特殊路径】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3624 题意: 给你一个无向图,n个点,m条边. 有两种边,种类分别用0和1表示. 让你求一 ...

  9. HDU 2643 Rank:第二类Stirling数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2643 题意: 有n个个选手参赛,问排名有多少种情况(可以并列). 题解: 简化问题: 将n个不同的元素 ...

  10. nginx rewrite 导致验证码不正确

    配置nginx里url rewrite的时候,为了使浏览器地址栏的URL保持不变, 使用proxy_pass反向代理,但发现每次都会生成新的jsessionid 解决方法,配置中增加 proxy_co ...