/**
* 实现栏目列表搜索
* add by hjgu
*/
(function($) {
$.fn.byColumnSearch = function(options) {
var defaults = {
dataURL: '',
siteId: '',
columnId: '',
usePageIndex: true, //是否开启分页
position: "",
searchBtn: "doSearch",
page: "",
search: true,
configs: {}
}, $this = $(this);
var options = $.extend(defaults, options);
this.each(function() {
initHandleClick();
initpageClick(); //跳转页面
initFirstPageClick(); //首页
initLastPageClick(); //尾页
initPrevPageClick(); //上一页
initNextPageClick(); //下一页
var isColSearch = defaults.search;
if (!isColSearch) {
initClickletters();
}
});
/**
* 初始化click事件
*/
function initHandleClick() {
$(document).on('click', "#" + defaults.searchBtn, function(event) {
loadContents(1);
});
}
/**
* 初始化跳转页面
* @returns {unresolved}
*/
function initpageClick() {
$(document).on('click', ".new_pages .new_pagingJump", function(event) {
var pageNum = $(".new_page_jump .pageNum").val();
//alert(pageNum);
if (pageNum === "") {
alert("请输入页码!");
return;
}
var pageCount = $(".new_pages .all_pages").attr("pageCount");
if (isNaN(pageNum) || pageNum <= 0 || pageNum > pageCount) {
alert("请输入正确页码!");
return;
}
loadContents(pageNum);
});
}
/***
* 初始化首页
*/
function initFirstPageClick() {
$(document).on('click', ".new_pages .new_page_nav .first", function(event) {
loadContents(1);
});
}
/**
* 初始化尾页
*/
function initLastPageClick() {
$(document).on('click', ".new_pages .new_page_nav .last", function(event) {
var allPages = $(".new_pages .all_pages").attr("pageCount");
// alert(allPages);
loadContents(allPages);
});
}
/***
* 初始化上一页
* @returns {unresolved}
*/
function initPrevPageClick() {
$(document).on('click', ".new_pages .new_page_nav .prev", function(event) {
var pageCount = $(".new_page_jump .curr_page").attr("curr_page");
var prev = --pageCount;
if (prev <= 0) {
return;
}
loadContents(prev);
});
}
/***
* 初始化下一页
* @returns {unresolved}
*/
function initNextPageClick() {
$(document).on('click', ".new_pages .new_page_nav .next", function(event) {
var pageCount = $(".new_page_jump .curr_page").attr("curr_page");
var next = ++pageCount;
var allPages = $(".new_pages .all_pages").attr("pageCount");
if (allPages < next) {
return;
}
loadContents(next);
});
}
/**
* 加载内容
* @param {type} pageIndex
* @returns {undefined}
*/
function loadContents(pageIndex) {
//var orders = JSON.stringify(getOrders());
var conditions = JSON.stringify(getConditions());
var returnInfos = JSON.stringify(getReturnInfos());
var rows = 14;
var configs = defaults.configs;
var target = "_blank"
if (configs != undefined) {
rows = configs.total;
target = configs.target;
}
$.ajax({
url: defaults.dataURL,
type: 'POST',
contentType: "application/x-www-form-urlencoded; charset=utf-8",
async: false,
dataType: 'json',
data: {
siteId: defaults.siteId,
columnId: defaults.columnId,
pageIndex: pageIndex,
rows: rows,
conditions: conditions,
// orders: orders,
returnInfos: returnInfos
},
success: function(result) {
var html = '';
var pageHtml = '';
var total = 0;
var pageCount = 0;
if (result != null) {
html = '
';
total = result.total;
pageCount = result.pageCount;
for (i = 0; i < result.data.length; i++) {
var art = result.data[i];
var index = i + 1;
html += ('- ');
html += (' ');
html += ('
');
html += (' ' + art.publishTime + ' ');
html += ('
');
html += (' ');
}
html += ("
");
if (defaults.usePageIndex) {
pageHtml += ('');
pageHtml += ('
');
pageHtml += ('
');
}
}
var tag = "#" + defaults.position;
var pageTag = "#" + defaults.page;
if (pageTag == "") {
html += pageHtml;
} else {
$(pageTag).html(pageHtml);
}
$(tag).html(html);
}
});
}
/**
* 查询条件
* @returns {undefined}
*/
function getConditions() {
//条件参数
var conditdata = [];
var config = defaults.configs;
if (config != undefined) {
var articleType = config.listArticleType;
if (articleType == 1) {
conditdata.push({field: "imgLogo", value: "1", judge: "="});
} else if (articleType == 2) {
conditdata.push({field: "videoLogo", value: "1", judge: "="});
} else if (articleType == 3) {
conditdata.push({field: "fileLogo", value: "1", judge: "="});
}
}
var isColSearch = defaults.search;
if (isColSearch) {
//关键字、作者、标题、内容
var word = $("#keyword").val();
var keyJson = [];
if (word != '' && word != "请输入关键字") {
word = "%" + word + "%";
keyJson.push({field: "key", value: word, judge: "like"});
keyJson.push({field: "author", value: word, judge: "like"});
keyJson.push({field: "title", value: word, judge: "like"});
keyJson.push({field: "content", value: word, judge: "like"});
}
if (keyJson != null && keyJson.length > 0) {
conditdata.push({orConditions: keyJson});
}
} else {
var title = $("#title").val();
if (title != '') {
title = "%" + title + "%";
conditdata.push({field: "title", value: title, judge: "like"});
}
var professor = $("#professor").val();
if (professor != '') {
conditdata.push({field: "title", value: professor, judge: "="});
}
//字母 需要区分大小写
var letter = $('#sc_letters').val();
if (letter !== "") {
var upletter = letter.toUpperCase();
var lwLetter = letter.toLowerCase();
conditdata.push({field: "letter", value: upletter, judge: "="});
conditdata.push({field: "letter", value: lwLetter, judge: "="});
}
}
return conditdata;
}
/**
* 排序方式
* @returns {undefined}
*/
function getOrders() {
}
/**
* 返回值
* @returns {Array}
*/
function getReturnInfos() {
var configs = defaults.configs;
var titlelp = 15;
var dateF = "yyyy-MM-dd";
if (configs != undefined) {
titlelp = configs.titleLength;
dateF = configs.pubDateFormat;
}
var returnInfosdata = [
{name: "url"},
{name: "title"},
{field: "shortTitle", name: "title", pattern: [{name: "lp", value: titlelp}]},
{name: "publishTime", pattern: [{name: "d", value: dateF}]}
];
return returnInfosdata;
}
/**
* 根据字母查询
*/
function initClickletters() {
$(document).on('click', '.letterSelector .letters', function(event) {
var selectedIds = "";
var letters = $('.letterSelector .letters');
letters.each(function() {
var selectedSpan = $(this).children("span");
if (selectedSpan.hasClass("checked")) {
selectedIds = $(this).attr("id").substring("2");
}
});
$('#sc_letters').val(selectedIds);
loadContents(1);
});
}
};
})(jQuery);