Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
<script type="text/javascript">
       var hide1 = true;
       var hide2 = true;
       var hide3 = true;

       function hideColumn1(tableId, colIndex) {
           var table = document.getElementById(tableId);

           if (table != null) {
               for (i = 0; i < table.rows.length; i++) {
                   if (hide1)
                       table.rows[i].cells[colIndex - 1].style.display = 'none';
                   else
                       table.rows[i].cells[colIndex - 1].style.display = '';
               }
               hide1 = !hide1;
           }
       }
       function hideColumn2(tableId, colIndex) {
           var table = document.getElementById(tableId);

           if (table != null) {
               for (i = 0; i < table.rows.length; i++) {
                   if (hide2)
                       table.rows[i].cells[colIndex - 1].style.display = 'none';
                   else
                       table.rows[i].cells[colIndex - 1].style.display = '';
               }
               hide2 = !hide2;
           }
       }
       function hideColumn3(tableId, colIndex) {
           var table = document.getElementById(tableId);

           if (table != null) {
               for (i = 0; i < table.rows.length; i++) {
                   if (hide3)
                       table.rows[i].cells[colIndex - 1].style.display = 'none';
                   else
                       table.rows[i].cells[colIndex - 1].style.display = '';
               }
               hide3 = !hide3;
           }
       }



   </script>



in this script i use 3 method for same work . i find the best solution . plz give me right way to complete this script.
Posted
Updated 20-Sep-13 0:15am
v4
Comments
Richard MacCutchan 20-Sep-13 5:31am    
Do you have a question?
ankit kumar from RIT 20-Sep-13 5:35am    
yes.
Richard MacCutchan 20-Sep-13 6:07am    
Then edit your post and explain exactly what your question is. Please don't expect people to guess what you want.
V.Lorz 20-Sep-13 5:39am    
And your question is.....
ankit kumar from RIT 20-Sep-13 5:42am    
How to modify this script code to minimum line ?

If you mean compress your code to something like this:

JavaScript
function hideColumn1(e,t){var n=document.getElementById(e);if(n!=null){for(i=0;i<n.rows.length;i++){if(hide1)n.rows[i].cells[t-1].style.display="none";else n.rows[i].cells[t-1].style.display=""}hide1=!hide1}}function hideColumn2(e,t){var n=document.getElementById(e);if(n!=null){for(i=0;i<n.rows.length;i++){if(hide2)n.rows[i].cells[t-1].style.display="none";else n.rows[i].cells[t-1].style.display=""}hide2=!hide2}}function hideColumn3(e,t){var n=document.getElementById(e);if(n!=null){for(i=0;i<n.rows.length;i++){if(hide3)n.rows[i].cells[t-1].style.display="none";else n.rows[i].cells[t-1].style.display=""}hide3=!hide3}}var hide1=true;var hide2=true;var hide3=true


you may try tools like: http://jscompress.com/[^] and http://javascriptcompressor.com/[^].
 
Share this answer
 
v2
Comments
V.Lorz 20-Sep-13 5:44am    
Sorry, had to update the text 'cause something was wrong in the submit.
phil.o 20-Sep-13 6:29am    
Hehe nice one ^^
You are basically doing the same thing in all the three functions except for the boolean varible hide1, hide2 & hide3. from the code what i understand is that you are just setting the status in the variable and not used any where. if that is the case you can use only a single function and don't have to create 3 different functions for same purpose
 
Share this answer
 
JavaScript
<script type="text/javascript">
    var hide1 = true;
    var hide2 = true;
    var hide3 = true;

    function hideColumn1(tableId, colIndex,hideType,ColumnValue) {
        var table = document.getElementById(tableId);

        if (table != null) {
            for (i = 0; i < table.rows.length; i++) {
                if (hideType)
                    table.rows[i].cells[colIndex - 1].style.display = 'none';
                else
                    table.rows[i].cells[colIndex - 1].style.display = '';
            }
            if(ColumnValue == "Column1")
            {
                hide1 = !hideType;
            }
            if(ColumnValue == "Column2")
            {
                hide2 = !hideType;
            }
            if(ColumnValue == "Column3")
            {
                hide3 = !hideType;
            }
        }
    }


</script>


you are just hiding the column and updating the variable hide1,hide2,hide3. so i just updated the your function .
1. added to more parameter into your function.
2. hidetype pass the current status of variable (true/false)
3. columnValue pass the you coumn id. fot that you were written diff function.
4. at last check the column value and on the basis of that set the variable status.

hope it will be help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900