How to Hide a column in DataTable based on value in other column in same row ?

How to Hide a column in DataTable based on value in other column in same row ?

How to Hide a column in DataTable based on value in other column in same row ?

Answers

  • jpobleyjpobley Posts: 4Questions: 0Answers: 0

    Something like this would likely work. Here is a simple example

    function hideColumn( col, row, testVal, testCol ) {
        var testCell = oTable.cell( row, testCol );
        if ( testCell.data() === testVal ) {
            oTable.column( col ).visible( false );        
        }
    }
    

    However, what happens if another row satisfies the opposite condition, and the column should be shown?

  • hchaudhary@relevantads.com hchaudhary@relevantads.com Posts: 3Questions: 1Answers: 0

    Yes, if opposite condition is true , then I need it to be visible , so I am looking for a function that will handle it per row basis .. maybe I should Disable or Gray out the cell if condition is met instead of making it invisible :

    My scenario is I have a dropdown in one column ,when a certain drop down value is selected , I want a cell in same row to be visible , on other dropdown selections I want it to be Hidden or disabled/grayed out.

  • jpobleyjpobley Posts: 4Questions: 0Answers: 0

    Ah, so you have a <select> in each cell of a column, and when the value of one changes, you want to enable/disable a cell in the same row. Is that correct? I thought you meant to hide the entire column :)

    I'd suggest listening for the change event from the <select> elements at the <table> node so that you only have to attach one event and rows can come and go as needed. Then you can apply whatever styles you want to the cell in question when the value of any given dropdown changes.

    How you work out the styles and initialization state is up to you, but here's an example of what I mean.

    You can also use the columns.render API to do things like sorting by enabled/disabled and other cool things. That is, If you're into it.

  • hchaudhary@relevantads.com hchaudhary@relevantads.com Posts: 3Questions: 1Answers: 0

    yes , you are right, I did something like that on fnrow callback for display , for editor I found this http://editor.datatables.net/examples/api/dependentFields.html
    Thanks for your help on this..

  • jpobleyjpobley Posts: 4Questions: 0Answers: 0

    Totally :) Good luck!

This discussion has been closed.