Having Problem in fetching data to DataTable based on DropDown selected Value in CodeIgniter

Having Problem in fetching data to DataTable based on DropDown selected Value in CodeIgniter

nyt1972nyt1972 Posts: 3Questions: 2Answers: 0
$("#classid").change(function(){
        var sessionid = $("#sessionid").val();
        var classid = $(this).val();
    $('#example').DataTable({
        ajax: {
            url: "<?php echo site_url('Ajax/getStudents'); ?>",
            type: "POST",
            data: function(d){
                d.sessionid = $("#sessionid").val();
                d.classid = $("#classid").val();
            }
        },
        dom: 'Bfrtip',
              iDisplayLength: 15,
              buttons: [
                  'copyHtml5',
                  'excelHtml5',
                  'csvHtml5',
                  'pdfHtml5',
                  'pageLength'
              ],
              search: true
         });  
        });

In Chrome Network its show the response as below:

[{"studentname":"Ali","fathername":"Ajmal","rollno":"101","mobile1":"222111"},{"studentname":"jamal","fathername":"kamal","rollno":"102","mobile1":"545454"}]

but not fetched to dataTable

Please help.

Answers

  • nyt1972nyt1972 Posts: 3Questions: 2Answers: 0

    I resolved the problem, below is my solution:

    $("#classid").change(function(){
            var sessionid = $("#sessionid").val();
            var classid = $(this).val();
            $.ajax({
                    url : "<?php echo site_url('Ajax/getStudents'); ?>",
                    method : "POST",             
                    data : {sessionid: sessionid, classid: classid},
                    async : true,
                    dataType : 'json',
                    success: function(data)
                    {
    
                        $('#example').DataTable({  
                destroy: true,    
                data : data, 
                columns: [
                         { data: 'studentname', title: "StudentName" },
                         { data: 'fathername', title: "Father Name" },
                         { data: 'rollno', title: "RollNo" },
                         { data: 'mobile1', title: "Mobile" }
                    ],   
                dom: 'Bfrtip',
                    iDisplayLength: 15,
                    buttons: [
                        'copyHtml5',
                        'excelHtml5',
                        'csvHtml5',
                        'pdfHtml5',
                        'pageLength'
                    ],
                    search: true
             });
    
                    }
                                    
                });
                return false;   
        });
    
Sign In or Register to comment.