0

I'm trying to add an extra default columns (edit and remove button), and the rest of rows, dinamics.

I get this error when I try to pass my $resultados and I get it with datos in my JS code.

DataTables warning: table id=tablaUsuarios - Requested unknown parameter 'ACCIÓN' for row 0, column 6. For more information about this error, please see http://datatables.net/tn/4

My $resultados already contains the data correctly (even with the extra column):

enter image description here

And I see my table with my data but not with the buttons column (and I have this data inside of $resultados variable, as you can see in the previous screenshoot).

enter image description here

This is my full code:

<?php

include_once(DIR_PLUGINS.'/alexcrudgenerator/main.php');

    $test = new GenerateCrud($_POST['tableName'], $_POST['id'], $_POST['tableFields']);

    switch($_POST['action']){
        
        case 'datosTabla': // OK.
            
            $res = json_decode($_POST['datos']);
            echo json_encode($res, JSON_UNESCAPED_UNICODE);
            
            break;
            
        case 'showtable': // OK.

            $res = getEntireTable($_POST['tableName'], $_POST['id'], $_POST['tableFields']);
            
            foreach ($res as $data){
                
                $data->botones = "<div class='text-center'><div class='btn-group'><button id='modificar_$data->id' class='btn btn-primary btn-sm btnEditar' value='edit'><i class='material-icons'>edit</i></button><button onclick='Delete($data->id)' class='btn btn-danger btn-sm btnBorrar'><i class='material-icons' value='delete'>delete</i></button></div></div>"; 
                $resultados['data'][] = $data;
            }           
            
            $resultados = json_encode($resultados); // 7 PROPIEDADES
            
            foreach(json_decode($_POST['tableFields']) as $columnsDB){
                $fields[] = array('data'=>$columnsDB);
            }

            $fields[]['data'] = 'ACCIÓN';
            $fields = json_encode($fields);
            
?>
            <head>
                <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
            </head>
            
            <div class="container caja">
                <div class="row">
                    <div class="col-lg-12 col-sm-12">
                        <div>
                            <table id="tablaUsuarios" class="table table-striped table-bordered table-condensed hover" style="width:100%" >
                                <thead class="text-center">
                                    <tr>
                                        <?php
                                            foreach (json_decode($_POST['tableFields']) as $columnsTH){
                                                echo '<th>' . strtoupper($columnsTH) . '</th>';
                                            }
                                            echo '<th>ACCIÓN</th>';
                                        ?>
                                    </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <script>
                
                $(document).ready(function() {
                    var datos= <?=$resultados?>;
                    var dynamicColumns = <?=$fields?>;
                    datos = JSON.stringify(datos); // I convert to JSON AGAIN because if not, my data is not showed

                    $('#tablaUsuarios').DataTable({
                        "language": {"url": "https://cdn.datatables.net/plug-ins/1.10.25/i18n/Spanish.json"},
                        "paging": true,
                        "lengthChange": true,
                        "searching": true,
                        "info": true,
                        "autoWidth": true,
                        "scrollX": true,

                        "ajax":{
                            "url": '<?=SITE_URL_ADMIN?>/alexcrudgenerator/crud/res/',
                            "method": 'POST',
                            "data":{action: "datosTabla", datos: datos}
                        },

                        "columns": dynamicColumns
                    });
                })
            </script>
<?php
        break;      
}
?>

Can someone help me? Thank you in advance!

5
  • 1
    Hey! may the accent on ACCIÓN do something wrong? Commented Jul 2, 2021 at 13:34
  • 1
    It is difficult to read your screenshot of the $resultados variable. This is one of the reasons why it is almost always better to provide data as text, not as images of text. Commented Jul 2, 2021 at 13:39
  • 1
    Having said that, it looks as if your $resultados buttons data value is called botones - but you appear to be using the heading name here: $fields[]['data'] = 'ACCIÓN'; instead of the data value name. So, what happens if you change that to $fields[]['data'] = 'botones';? Commented Jul 2, 2021 at 13:39
  • @DafuQi Thank you for your reply, unfortunatelly, no, the accent is not giving problems :D Commented Jul 5, 2021 at 8:04
  • @andrewjamesOhhh, yes, you are right ... I had to change $fields[]['data'] = 'ACCIÓN'; to $fields[]['data'] = 'botones';. Thank you very much. You can reply my question and I will accept your answer! Have a good day Commented Jul 5, 2021 at 8:06

1 Answer 1

1

Your $resultados buttons data value is called botones - but you appear to be using the heading name here: $fields[]['data'] = 'ACCIÓN'; instead of the data value name.

You can change that to $fields[]['data'] = 'botones';, instead.

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

Comments

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.