javascript hide row in table local storage -
im using formula hide whole html table.
function tableexpander(tableid) { // our flag determine if rows hidden or not var rowsvisible = localstorage.getitem('rowsvisible-'+tableid), // table handler table = document.getelementbyid(tableid); // "rowhidden" not exists in localstorage yet if (rowsvisible === null) { rowsvisible = true; } else { // localstorage return string rowsvisible = rowsvisible === 'true' ? true : false; } toggledisplay(table, rowsvisible ? '' : 'none'); table.getelementsbyclassname('togglebtn')[0].addeventlistener('click', function() { toggledisplay(table); }, false); function toggledisplay(tbl) { var tblrows = table.rows, mode = rowsvisible ? '' : 'none'; (i = 0; < tblrows.length; i++) { if (tblrows[i].classname != "headerrow") { tblrows[i].style.display = mode; } } localstorage.setitem('rowsvisible-'+tableid, rowsvisible); rowsvisible = !rowsvisible; }; }; tableexpander('table'); tableexpander('table1');
how can modify hide 1 row @ time , change button color red when hidden? example here http://jsfiddle.net/sxaz4/85/
demo: http://jsfiddle.net/neh89/31/ (code below)
<table> <tr class='row-1'> <td>foo</td> </tr> <tr class='row-2'> <td>bar</td> </tr> <tr class='row-3'> <td>rab</td> </tr> </table> <!-- button toggle elements css class "row-1" --> <a class='btn hide-1' data-hide-element='row-1' data-default-mode='visible'>toggle row class "row-1"</a> <!-- button toggle elements css class "row-2" --> <a class='btn hide-2' data-hide-element='row-2' data-default-mode='hidden'>toggle row class "row-2"</a> <!-- button toggle elements css class "row-3" --> <!-- if @data-default-mode not specified "visible" --> <a class='btn hide-3' data-hide-element='row-3'>toggle row class "row-3"</a>
/** * function toggle (shows/hides) elements , * remember visibility in browsers localstorage. * * @param {string} buttonscssclass buttons css class trigger toggle action. * @return {void} */ function toggleelements(buttonscssclass) { var // our object of flags determine if elements hidden or not elementsvisible = localstorage.getitem('elementsvisible'), // list of buttons btns = document.getelementsbyclassname(buttonscssclass); // "rowhidden" not exists in localstorage yet if (elementsvisible === null) { elementsvisible = {}; } else { // localstorage return string elementsvisible = json.parse(elementsvisible); } // bind buttons var = btns.length, cls, mode, elements, x, btn; while (i--) { btn = btns[i]; cls = btn.getattribute('data-hide-element'); mode = btn.getattribute('data-default-mode'); if (typeof elementsvisible[cls] !== 'boolean') { elementsvisible[cls] = (mode === 'visible' || mode === null) ? true : false; } btn.addeventlistener('click', function() { var _cls = this.getattribute('data-hide-element'); elementsvisible[_cls] = ! elementsvisible[_cls]; localstorage.setitem('elementsvisible', json.stringify(elementsvisible)); toggledisplay(_cls, elementsvisible[_cls]); }, false); toggledisplay(cls, elementsvisible[cls], true); } function toggledisplay(cls, visible) { var elements = document.getelementsbyclassname(cls); x = elements.length; while (x--) { elements[x].style.display = visible ? '' : 'none'; } // change color of buttons var btns = document.queryselectorall('[data-hide-element="'+cls+'"]'), = btns.length; while (i--) { btns[i].style.backgroundcolor = visible ? 'white' : 'red'; } }; }; toggleelements('btn');
Comments
Post a Comment