Skip to content

Instantly share code, notes, and snippets.

@think49
Created October 8, 2010 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/616387 to your computer and use it in GitHub Desktop.
Save think49/616387 to your computer and use it in GitHub Desktop.
TableEditor.js : table要素をフィルタリング
// TableEditor.js
var TableEditor = (function () {
var prototype;
function TableEditor () {
this.init.apply(this, arguments);
return this;
}
prototype = TableEditor.prototype;
prototype.init = function (table) {
if (typeof table !== 'object' || table.tagName !== 'TABLE') throw new TypeError(table + ' is not a HTMLTableElement');
this.table = table;
return this;
};
prototype.rowFilter = function (searchReg, narrowFlag, cellIndex) {
var dataType, cellIndexFlag, table, tBodies, rows, row, cells, cell, textContent, style, display, i, l, j, m, k, n;
dataType = typeof searchReg;
if (dataType !== 'object' && dataType !== 'function') throw new TypeError(searchReg + ' is not a Object');
dataType = typeof cellIndex;
cellIndexFlag = dataType === 'number';
if (!cellIndexFlag && dataType !== 'undefined') throw new TypeError(cellIndex + ' is not a Number');
narrowFlag = Boolean(narrowFlag);
table = this.table;
tBodies = table.tBodies;
for (i = 0, l = tBodies.length; i < l; ++i) {
rows = tBodies[i].rows;
for (j = 0, m = rows.length; j < m; ++j) {
row = rows[j];
style = row.style;
if (narrowFlag && style.display === 'none') continue;
display = 'none';
cells = row.cells;
if (cellIndexFlag) {
cell = cells[cellIndex];
textContent = cell.textContent || cell.innerText;
if (searchReg.test(textContent)) {
display = 'table-row';
}
} else {
for (k = 0, n = cells.length; k < n; ++k) {
cell = cells[k];
textContent = cell.textContent || cell.innerText;
if (searchReg.test(textContent)) {
display = 'table-row';
break;
}
}
}
style.display = display;
}
}
return table;
};
return TableEditor;
})();
@think49
Copy link
Author

think49 commented Oct 8, 2010

下記URLで解説しています。

tableEditor.js
http://vird2002.s8.xrea.com/javascript/tableEditor.html


OKWave で回答しました。

[Javascript]セル内の文字列の部分一致でテーブルの列を絞り込 | OKWave
http://okwave.jp/qa/q6234024.html


下記URLに gtlt さんの遊びコードがあります。

2010-10-04 - babu_babu_babooのごみ箱
http://d.hatena.ne.jp/babu_babu_baboo/20101004#c1286300748

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment