简体   繁体   中英

How to append the data From dataTable in click event

i Have used following script, data doesn't load to dataTable, i got error click here to show error image Here my jquery code

Table = $("#example").DataTable({
            data:[],
            columns: [
            { "data": "Course" },
            { "data": "Batch" },
            { "data": "Admission No" },
            { "data": "Rollno" },
            { "data": "Student Name" },
            { "data": "Email" }
            ],
            rowCallback: function (row, data) {},
            filter: false,
            info: false,
            ordering: false,
            processing: true,
            retrieve: true
            });
        $('.view_search_btn').click(function(){
            //alert("clicked");
            var search_key = $('input[name=view_stu_search]').val();
            $('.box-body').show();
             $.ajax({
                    url: "../live_search.php",
                    type: "post",
                    data: "key="+'view_student_search_key'+"&search_keyword="+search_key
                }).done(function (result) {
                    Table.clear().draw();
                    Table.rows.add(result).draw();
                }).fail(function (jqXHR, textStatus, errorThrown) { 
                      // needs to implement if it fails
                });

});// Click event close here }); // document close here and my php code is

if(!ctype_digit($_POST['search_keyword'])){
            //echo "given value is string".$_POST['search_keyword'];
            $rows = search_keyword($view_student_search_key);
            //print_r($rows);
            foreach($rows as $row)
            {
                $query = "SELECT a.stu_rollno, c.stu_course,c.stu_batch,a.admission_no,p.stu_firstname,p.stu_lastname,co.stu_email FROM current_course c, admission_details a,stu_personal_details p, stu_contact_details co WHERE a.stu_rollno = p.stu_rollno AND c.stu_rollno = co.stu_rollno AND a.stu_rollno = c.stu_rollno AND (c.stu_degree = ".$row['degree_id']." AND c.stu_course = ".$row['course_id']." AND c.stu_branch = ".$row['branch_id'].");";
                //echo "<br><br>";
                $run_query = mysqli_query($con, $query);
                while($result = mysqli_fetch_array($run_query))
                {
                    echo "
                        <tr>
                                <td>".$row['course_name']."</td>
                                <td>".$result['stu_batch']."</td>
                                <td>".$result['admission_no']."</td>
                                <td>".$result['stu_rollno']."</td>
                                <td>".$result['stu_firstname'].$result['stu_lastname']."</td>
                                <td>".$result['stu_email']."</td>
                                <td align='center'><a href='edit.php?rollno=".$result['stu_rollno']."&degree=".$row['degree_name']."&course=".$row['course_name']."&branch=".$row['branch_name']."' class='btn btn-info btn-sm btn-flat'><i class='fa fa-edit'></i> Edit</a>
                                    <button type='button' class='btn btn-danger btn-sm btn-flat' name='remove_levels' data-toggle='modal' data-target='.bs-example-modal-sm'><i class='fa fa-close'></i> Delete</button>
                                </td>
                            </tr>
                    ";

                }
            }

        }`

this following function inside my php

function search_keyword($view_student_search_key)
{
    $query = "SELECT d.degree_id,d.degree_name,c.course_id,c.course_name,b.branch_id,b.branch_name FROM degree d,courses c,branch b WHERE d.degree_id = c.degree_id AND b.course_id = c.course_id AND (d.degree_name like '%$view_student_search_key%' OR c.course_name like '%$view_student_search_key%' OR b.branch_name like '%$view_student_search_key%')";
    global $con;
    $run_query = mysqli_query($con, $query);

    //Declare the rows to an array
    $rows = array();
    // Insert Each row to an array
    while($row = mysqli_fetch_array($run_query))
    {
        $rows[] = $row;
    }
    // Return the array
    return $rows;
}`

Here, You are adding total 6 td for header and Then for inner tr You are adding 7 td . So there is mismatch in datatable td for each row.

What you can do is:

Just add one more td to your datatable initialization preceding with , (comma):

{ "data": "Actions" }

Note: Action is a title for td of edit & other icons column.

Use xhr.dt and reload data using the datatables load() api . xhr.dt will listen on the ajax call data when it gets loaded. You can clear and draw ie append data in the xhr callback function. Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM