renderizar botones, serverside.

renderizar botones, serverside.

cris19ncris19n Posts: 55Questions: 18Answers: 0
edited December 2020 in Free community support

I am painting edit button if the user is administrator type.

It works correctly if it is administrator type, but how can I do so that it does not paint anything if the condition is not met, because I get this error:

{ data: 1},
                        { data: 2},
                        { data: 3},
                        { data: 4},
    {

                                "render": function ( data, type, row ) {

                                    var idus = $('#variable_sesi').val();
                                    var user = $('#variable_sesi2').val();



                                        if(user==="ADMIN"){

                                           return '<div class="btn-group" role="group" aria-label="Third group"><button class="btn btn-warning btnEditM  manito-clic" edit='+row['0']+' idUse='+idus+' User='+user+' data-toggle="modal" data-target="#modalEditarM"><i class="fa fa-pencil"></i></button> </div>';
                                        }
                                    }
                                }

it works correctly if it is administrator type.

if it is not of type admin I receive this error: jquery.dataTables.min.js:76 Uncaught TypeError: Cannot read property 'style' of undefined

I only need to paint the button when it is of type admin, but then not paint anything, as if that column does not exist

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    You always need to return something in columns.render. What you return is up to your requirements but here is an example:

                                            if(user==="ADMIN"){
     
                                               return '<div class="btn-group" role="group" aria-label="Third group"><button class="btn btn-warning btnEditM  manito-clic" edit='+row['0']+' idUse='+idus+' User='+user+' data-toggle="modal" data-target="#modalEditarM"><i class="fa fa-pencil"></i></button> </div>';
                                            }
                                            return data; // or whatever you want if not admin
    

    Kevin

  • cris19ncris19n Posts: 55Questions: 18Answers: 0
    edited December 2020

    It doesn't work because I am validating from html, if the user is of type admin.
    html:

            if($user["tipo_user"]=="ADMIN"){
    
                                                echo
                                                    '<th style="width:85px; color: #FEB100;">ACCION</th>'
                                                ;
    
                                            }
    

    when not admin i keep getting this error.

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    when not admin i keep getting this error.

    That is because you aren't returning anything. You can simple use return ''; if you don't want to show anything. If you still need help post a link to your page or a test case showing the issue so we can see what you have and help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • cris19ncris19n Posts: 55Questions: 18Answers: 0

    how to make a test case, using server side?

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    The data source isn't part of the problem. You can simulate the data using Javascript and use data instead of ajax to apply the data to the table. See this example.

    Kevin

  • cris19ncris19n Posts: 55Questions: 18Answers: 0
    edited December 2020
  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Thanks for the test case. The test case has the original HTML table with 6 columns. But your Datatables config has 7 columns resulting in the style of undefined error. I removed the HTML table and added columns.title to the 7th column (button column). Now the table is rendering:
    http://live.datatables.net/munegufo/2/edit

    Kevin

  • cris19ncris19n Posts: 55Questions: 18Answers: 0
    edited December 2020

    I appreciate your help,
    but I don't think I made myself understood.

    the html table has 7 columns when the user is an administrator, that is, the missing th is painted when the user is an administrator.

    but when the user is not admin then I don't paint the seventh column

    that's why I keep getting the error when the user is not an admin

    and I don't know how I can handle it

    my code html:

    <table class="table table-bordered" id="example">
    
        <thead>
            <tr class="warning">
            <th colspan="29" class="text-center" style="width:10px; color: #FEB100;">TABLA</th>
            </tr>
            <tr class="warning">
    
                <th style="width:130px; color: #FEB100;">DATA 1</th>
                <th style="width:153px; color: #FEB100;">DATA 2</th>
                <th style="width:65px; color: #FEB100;">DATA 3</th>
                <th style="width:150px; color: #FEB100;">DATA 4</th>
                <th style="width:30px; color: #FEB100;">DATA 5</th>
                <th style="width:130px; color: #FEB100;">DATA 6</th>
    
                <!-- condition that does or does not paint the data 7-->
                <?php
                    $item = "id_user";
    
                    $valor= $_SESSION["user"];
    
                    $respUser = controllerUser::ctrSHOWUSERS($item, $valor);
    
                    if($respUser["user_type"]=="ADMIN"){
    
                        echo
                            '<th style="width:85px; color: #FEB100;">ACTION</th>'
                        ;
    
                    }
                ?>
    
            </tr>
        </thead>
    
    
    </table>
    
  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    You can use column visibility for this. See this updated example:
    http://live.datatables.net/munegufo/3/edit

    It uses columns.visible to hide the column by default. In initComplete it uses column().visible() to make the column visible if the user is ADMIN. Change the global var user to ADMIN to see the change.

    Kevin

  • cris19ncris19n Posts: 55Questions: 18Answers: 0

    now i have this error and ajax data keeps loading

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Its hard to say what the problem is without seeing it. Please update the test case or provide a link to your page so we can see what you have to help debug.

    My guess is you still have a problem with the number of HTML table columns being different than the number of columns defined in Datatables. They need to match. Or you can use columns.title to have Datatables create the columns like the example we are working with.

    Kevin

This discussion has been closed.