Created
October 8, 2010 05:05
-
-
Save think49/616387 to your computer and use it in GitHub Desktop.
TableEditor.js : table要素をフィルタリング
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
下記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