javascript incremental_search.js
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript incremental_search.js相关的知识,希望对你有一定的参考价值。
/**
* Incremental Search
*/
var IncrementalSearch = function (targetInput, targetTable) {
this.targetInput = targetInput;
this.targetTable = targetTable;
this.query = '';
this.dataList = [];
this.filterList = [];
};
IncrementalSearch.prototype = {
init: function () {
this.initDataList();
this.attachEvent();
},
attachEvent: function () {
var self = this,
eventHandler = function (e) {
self.search(e);
};
this.util.addEvent(this.targetInput, 'input', eventHandler);
},
search: function () {
this.query = (this.targetInput.value).toLowerCase();
if (this.query.length) {
this.filterData();
} else {
this.clearFilter();
}
},
initDataList: function () {
var i, max,
targetTable = this.targetTable,
tbody = targetTable.getElementsByTagName('tbody'),
th = tbody[0].getElementsByTagName('th');
this.th = th;
for (i = 0, max = th.length; i < max; i++) {
this.dataList.push(th[i]);
}
},
filterData: function () {
this.filterList = [];
for (var i = 0, max = this.dataList.length; i < max ; i++) {
this.filterList.push(this.filter(this.th[i]));
}
this.changeDisplay();
},
filter: function (data) {
var textData = ('innerText' in document) ? data.innerText : data.textContent;
textData = textData.toLowerCase();
return textData.indexOf(this.query) > -1;
},
changeDisplay: function () {
for (var i = 0, max = this.dataList.length; i < max; i ++) {
this.th[i].parentNode.style.display = (this.filterList[i]) ? '' : 'none';
}
},
clearFilter: function () {
for (var i = 0, max = this.dataList.length; i < max; i++) {
this.th[i].parentNode.style.display = '';
}
}
};
IncrementalSearch.prototype.util = {
addEvent: function (elem, ev, fn) {
if (elem.addEventListener) {
elem.addEventListener(ev, fn, false);
} else if (elem.attachEvent) {
elem.attachEvent('on' + ev, fn);
} else {
elem['on' + ev] = fn;
}
}
};
var increSearch = new IncrementalSearch(document.getElementById('incremental-search-field'), document.getElementById('incremental-search-table'));
increSearch.init();
以上是关于javascript incremental_search.js的主要内容,如果未能解决你的问题,请参考以下文章
javascript JavaScript isset()等效: - JavaScript
JavaScript 使用JavaScript更改CSS(JavaScript)
JavaScript之基础-1 JavaScript(概述基础语法)
前端基础-JavaScript的基本概述和语法
JavaScript
JavaScript