自己在项目中写的一个Jquery插件和Jquery tab 功能
后台查询结果 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 功能的更多相关文章
- 【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能
在web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面,而重定向到登陆界面的功能. 项目是用springMVC+spring+hibernate实现 (和这个没有多大关系) 第一步: 首先 ...
- Web 项目中分享到微博、QQ空间等分享功能
Web 项目中分享到微博.QQ空间等分享功能 网上有很多的模板以及代码,但是有很多都不能分享内容,简单的测试了下: 以新浪微博为例,文本框中的内容是title属性,下面的链接是url属性,如果你的链接 ...
- C# winform项目中ListView控件使用CheckBoxes属性实现单选功能
C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...
- 解决:一个项目中写多个包含main函数的源文件并分别调试运行
自己在学c++的时候,一个项目中的多个cpp文件默认不允许多个main函数的出现,但是通过选项操作能够指定单个cpp文件进行运行,如下: 1.此时我就想运行第二个cpp文件,我们只需要把其他的两个右键 ...
- 在类库或winform项目中打开另一个winform项目的窗体
假设类库或winform项目为A,另一个winform项目为B.那麽在A中添加一个接口,里面有一个Show方法,然后在B中写一个类b继承这个接口,并重写这个方法,具体内容为弹出某个窗体.然后在A中另一 ...
- 在angular项目中使用bootstrap的tooltip插件时,报错Property 'tooltip' does no t exist on type 'JQuery<HTMLElement>的解决方法和过程
在angular4的项目中需要使用bootstrap的tooltip插件. 1. 使用命令安装jQuery和bootstrap npm install bootstrap jquery --save ...
- Laravel 项目中编写第一个 Vue 组件
和 CSS 框架一样,Laravel 不强制你使用什么 JavaScript 客户端框架,但是开箱对 Vue.js 提供了良好的支持,如果你更熟悉 React 的话,也可以将默认的脚手架代码替换成 R ...
- MES项目中出现的一个事务嵌套的使用场景
昨天在MES项目中,需要在业务逻辑的几个关键点记录错误信息,需要把错误信息写入数据表. 但是由于整个业务逻辑都是包在一个事务模板里面的 比如这样的: WhhTransactionTemplate tr ...
- 【Atom】在一个中/大型项目中,那些好用而强大的atom功能
作为一个学生党,一开始使用atom时候并没有意识到atom一些小功能的巨大作用,直到自己实习参与了项目,才知道这些功能在一个项目中是能极大提高工作效率的开发利器 下面是一位不愿意透露其姓名的彭 ...
随机推荐
- 九度OJ 1042:Coincidence(公共子序列) (DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2303 解决:1241 题目描述: Find a longest common subsequence of two strings. 输入 ...
- iframe式ajax调用
1.新建 a.html <!doctype html> <html> <head> <meta charset='utf-8'> <title&g ...
- HTML 学习笔记 JQuery(animation)
动画效果也是JQuery库吸引人的地方,通过JQuery的动画方法,能够轻松的为网页天假非常紧菜的视觉效果. show()方法和hide()方法 show()方法和hide()方法是JQuery中最基 ...
- Method invoke 方法
这个问题要看明白源码才能解决
- vue 组件与传值
一.表单输入绑定(v-model 指令) 可以用 v-model 指令在表单 <input>.<textarea> 及 <select> 元素上创建双向数据绑定. ...
- 【LeetCode-easy】Merge Two Sorted Lists
思路:指针p用于串联怎个链表,比较两个指针的大小,连接较小的一个.如果一个链表到达链尾,连接另外一个链表余下来的所以节点. public ListNode mergeTwoLists(ListNode ...
- ES6 Map数据结构
Map JavaScript 的对象(Object),本质上是键值对的集合(Hash 结构),但是传统上只能用字符串当作键.这给它的使用带来了很大的限制. ES6 提供了 Map 数据结构.它类似于对 ...
- JAVA- String类练习
JAVA- String类练习 需求1:去除字符串两边空格的函数,写一个自己的trim(); public class TestTrim { public static void main(Strin ...
- HDU 2643 Rank:第二类Stirling数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2643 题意: 有n个个选手参赛,问排名有多少种情况(可以并列). 题解: 简化问题: 将n个不同的元素 ...
- JS遍历获取多个控件(使用索引‘i’)
1.n个tid="n1"的input.n个tid="n2"的input.n个tid="n3"的input---循环遍历 ; i <= ...