2

I have a big table of info which can get a bit overwhelming. I currently have buttons to hide or show certain columns to make it easier to find what you need. I use this code to show/hide.

<script type="text/javascript" charset="utf-8">    
function fnShowHide( iCol )
    {
        var oTable = $('#tablename').dataTable();
        var bVis = oTable.fnSettings().aoColumns[iCol].bVisible;
        oTable.fnSetColumnVis( iCol, bVis ? false : true );
    }
</script>

Later on, I use the following code to hide or show a certain column in the table.

<button id="button">
    <a href="javascript:void(0);" onclick="fnShowHide(0);">Column1</a>
</button>
<button id="button">
    <a href="javascript:void(0);" onclick="fnShowHide(1);">Column2</a>
</button>
<button id="button">
    <a href="javascript:void(0);" onclick="fnShowHide(2);">Column3</a>
</button>

How would I make a single button that would hide or show multiple rows rather than just single rows?

I am using DataTables to show my data and am using this example for the button above (if that makes a difference).

4
  • 1
    how about passing an array into the JS function? Commented Mar 18, 2013 at 2:59
  • 1
    Hi, according to you code, I think you not to hide/show some columns not rows, isn't it? Commented Mar 18, 2013 at 3:05
  • 1
    @ShivanRaptor That would probably work. The problem is that I'm a complete beginner and am not sure how to do that. I'll give it a google now. Commented Mar 18, 2013 at 3:11
  • @OQJF Nice spot. I meant Columns, but I will be using this same thing to hide rows later. Commented Mar 18, 2013 at 3:14

1 Answer 1

8

Rather than accepting a single integer as input, why not accept an array of rows you want to hide?

function fnShowHide( iCols )
{
    var i, iCol;
    var oTable = $('#tablename').dataTable();

    for (i = 0; i < iCols.length; i += 1) {
        iCol = iCols[i];
        var bVis = oTable.fnSettings().aoColumns[iCol].bVisible;
        oTable.fnSetColumnVis( iCol, bVis ? false : true );
    }
}

To invoke it for one row you would just use fnShowHide([2]), and for multiple rows you would pass in multiple values like fnShowHide([2,3,4])

Sign up to request clarification or add additional context in comments.

6 Comments

Buddy, just kind of curious, why you use i+=1, not i++?
Ah it's habbit from using a JavaScript linter. Either is fine.
Wait. You should check if the iCols is a valid array or not.
@fuzic EXCELLENT. This is exactly what I was looking to do, but I didn't have enough Javascript knowledge to get it to work. Thanks so much!
@Cully You should probably select this as the answer, since it definitely seems to solve your question :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.