简体   繁体   English

如何在click事件中附加数据来自dataTable

[英]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 我使用了以下脚本,数据没有加载到dataTable,我收到错误点击这里显示错误图片这里我的jquery代码

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 //文档关闭在这里,我的PHP代码是

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 我的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 . 在这里,您为标题添加总计6 td ,然后为内部tr添加7 td So there is mismatch in datatable td for each row. 因此每行的数据表td不匹配。

What you can do is: 您可以做的是:

Just add one more td to your datatable initialization preceding with , (comma): 只需再添加一个td到之前的数据表初始化, (逗号):

{ "data": "Actions" }

Note: Action is a title for td of edit & other icons column. 注意: Action是“编辑”和“其他图标”列的td的标题。

Use xhr.dt and reload data using the datatables load() api . 使用xhr.dt并使用datatables load()api重新加载数据。 xhr.dt will listen on the ajax call data when it gets loaded. xhr.dt将在加载时监听ajax调用数据。 You can clear and draw ie append data in the xhr callback function. 您可以清除并绘制,即在xhr回调函数中附加数据。 Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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