Hi i am new. i didn't understand howto insert data in a Datable from a database. Could someone provi

Hi i am new. i didn't understand howto insert data in a Datable from a database. Could someone provi

lupomeolupomeo Posts: 6Questions: 2Answers: 0

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Answers

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    There are many ways this can be done. A common way is to load the table data from the server using Ajax. See the Ajax docs for details. There are Ajax examples you can look at.

    Its best to ask more specific questions and to provide basic information of your environment.

    Kevin

  • lupomeolupomeo Posts: 6Questions: 2Answers: 0

    Yes i have seen the examples but my problem is that when i use ajax with this code the data are succesfully inserted in the table BUT the table don't display the pages, display the wrong nuber of items, and lose the search input.

    function view_data(){
    $con = connection();
    $query = "SELECT * FROM tipologie";
    $result = mysqli_query($con, $query);

    if(mysqli_num_rows($result)){
        while($row = mysqli_fetch_assoc($result)){
        $table .= '<tr>';
        $table .= '<td>' . $row['descrizione'] . '</td>';
        $table .= '<td>' . $row['ore'] . '</td>';
        $table .= '<td>' . $row['validita'] . '</td>';
        $table .= "<td style='text-align:center'><a data-id='" . $row['id'] . "' data-toggle='modal' data-target='#editModal' href='' id='update' class='edit'>
        <i class='fa fa-edit'></i></a>  <a data-id='" . $row['id'] . "' class='delete' id='delete' href='' data-toggle='modal' data-target='#deleteModal'><i class='fa fa-trash'></i></a> </td>";
        $table .= '</tr>';
        }    
        echo json_encode(['status'=>'success', 'table' => $table]);
    } else {
        $table .= '<tr colspan="8"';
        $table .= '<td>NO DATA!</td>';
        echo json_encode(['status' => 'success', 'table' => $table]);
    }
    

    }

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    If you are populating the HTML table data this way then don't use ajax. Read about the supported Data Sources. Looks like you will want to initialize using DOM sourced data. See this example.

    Kevin

  • lupomeolupomeo Posts: 6Questions: 2Answers: 0

    Anyone else that can really help ?

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    I think Kevin's answer is spot on (as they always are).

    Maybe we should take a step back - first of all: how do you want to populate the table's data? Via Ajax? If so, don't have the Ajax response return HTML for the table rows. Let DataTables do that for you! Reply to the Ajax request with JSON - e.g.:

    $data[] = [
      'descrizione' => $row['descrizione'],
      'ore' => $row['ore'],
      ...
    ];
    

    Then:

    echo json_encode([
      'data' => $data
    ]);
    

    Then you can populate your DataTable in the say way as this example.

    See this section of the manual for more details on this.

    Allan

Sign In or Register to comment.