Show no. of columns according to user rights

Show no. of columns according to user rights

pratsprats Posts: 45Questions: 21Answers: 0

I want use datatable feature for following scenario.
1)It contains 1 single report but different visible columns for different users.
2)when user clicks on link to open report, report should show only those columns that are assigned to him i.e only visible columns
3) and when user log out to account it should save previous state .Is it possible as I am new to jquery n javascript

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 3,036Questions: 88Answers: 423

    Most or all of it should be possible. I did something similar: depending on what page you are on some columns are shown or hidden. Same logic if you do this based on user rights. Here is my little example.
    - Outside datatables set classes depending on what page you are on:

    if (expPage) {
            $('.hideForContract').removeClass('hiddenForThis');
        } else {
            $('.hideForContract').addClass('hiddenForThis');
        }
    

    This is the HTML for the datatable:

    <thead>
                <tr>
                    <th class="noSelectCols all">Remarks From</th>
                    <th>Instrument</th>
                    <th>Type</th>
                    <th>Contract Number</th>
                    <th>IBAN</th>
                    <th>Expiration Follow Up</th>
                    <th class="hideForContract">Expires on</th>
                    <th class="noSelectCols all">Info</th> <!--all = column never hidden-->
                    <th class="noSelectCols all">Documentation</th> <!--all = column never hidden---->
                    <th class="all">done</th>
                    <th class="hiddenCols" id="updateTime">Update Time</th>                
                </tr>
            </thead>
    

    In column definitions of the respective table I have this. As you can see some columns are always hidden others only depending on what page you are on.

    columnDefs: [
                // targets may be classes
                {   targets: "hiddenCols", visible: false },
                {   targets: "hiddenForThis", visible: false }
            ]
    

    I also make sure that the "hiddenForThis" columns cannnot be made visible with the colvis button (whereas the "hiddenCols" can be made visible with that button.)

    buttons: [
                {   extend: "colvis", columns: ':not(.hiddenForThis)' }
    

    You can also add classes through the datatables API but there is a bug in the current official release, see below: https://datatables.net/forums/discussion/comment/107810/#Comment_107810

  • pratsprats Posts: 45Questions: 21Answers: 0

    @rf1234 thanks for response but I am getting data from web service so is it possible for this?

  • rf1234rf1234 Posts: 3,036Questions: 88Answers: 423
    Answer ✓

    why should the data source matter? This is purely Javascript and HTML ...
    Please see this on data sources: https://datatables.net/examples/data_sources/index.html

  • pratsprats Posts: 45Questions: 21Answers: 0

    So I used this solution that works fine for me.

    if(adminFlag === "false"){
                        
                        oTable.column( 0 ).visible( false ); // For hiding of column
                        oTable.button( '3-1' ).disable(); // For disabling of export option
                        }
    

    Thanks

This discussion has been closed.