0

I want to pass the result data of ajax page to jquery datatable function,Please suggest me the correct syntax how to pass the result into the datatable function:

function getResultsMsr(action, msrDel) {
            if(action == 'search') {
            $.ajax({
                     url: 'getResult.php',
                       type: 'POST',
                       data: {
                             formName:'afscpMsr',
                            action:'search',
                            field_nm: document.getElementById('msrdet').value,
                             field_value:document.getElementById('srhmsr').value    
                         }
                      }).done(function(result_data){

                          var  data= result_data;
                          $('#example').dataTable( {
                            "processing": true,
                            "serverSide": true
                    });

                    });

Table which is diplay the data and column name is:

   <table id="example" class="display" cellspacing="0" width="100%" >
                    <thead>
                         <tr>
                           <th>Customer Name</th>
                           <th>Feature Order No</th>
                           <th>NCP Account Number</th>
                           <th>Mcn Code</th>
                           <th>Sales Person</th>
                          <!--  <th>Due Date</th> --> 
                           <th>Status</th>
                            <th>MSR Id</th>
                            <th>Action </th>

                        </tr>
                    </thead>
                 </table>

Ajax page which passes the result to be displayed in datatable ,this result is in json format :

  if($_POST['action'] == 'search')
  {
    $col_nm = $_POST['field_nm'];
    $srch_val = $_POST['field_value'];
     if($srch_val == 'Yes') {
         $srch_val = 'Y';
     } elseif ($srch_val == 'No'){
        $srch_val = 'N';
    }
    $result = $afscpMsrMod->getMsrDetails($col_nm, $srch_val,$page,$start);
    $totalCont = $afscpMsrMod->getTotalCountOfMsrDetails($col_nm, $srch_val);
    $totalCont= ceil($totalCont/10);
    $newarray = array(
        "draw"            => intval( ""),
        "recordsTotal"    => intval($totalCont ),
        "recordsFiltered" => intval( $totalCont ),
        "data"            => json_encode($result[0])
    );
    echo json_encode($newarray);
chaya
  • 423
  • 3
  • 11
  • 19

2 Answers2

0

Did you try using aaData property:

Here you can find information abour various data sources for datatables:

JS array as datasource in datatables

Server side data processing

Ajax source

DOM zero configuration

 function getResultsMsr(action, msrDel) {
        if(action == 'search') {
        $.ajax({
                 url: 'getResult.php',
                   type: 'POST',
                   data: {
                         formName:'afscpMsr',
              function getResultsMsr(action, msrDel) {
        if(action == 'search') {
        $.ajax({
                 url: 'getResult.php',
                   type: 'POST',
                   data: {
                         formName:'afscpMsr',
                        action:'search',
                        field_nm: document.getElementById('msrdet').value,
                         field_value:document.getElementById('srhmsr').value    
                     }
                  }).done(function(result_data){

                      var  data= result_data;
                      $('#example').dataTable( {
                        "processing": true,
                        "serverSide": true,
                        "aaData":data
                });

                });          action:'search',
                        field_nm: document.getElementById('msrdet').value,
                         field_value:document.getElementById('srhmsr').value    
                     }
                  }).done(function(result_data){

                      var  data= result_data;
                      $('#example').dataTable( {
                        "processing": true,
                        "serverSide": true
                });

                });
SSA
  • 5,433
  • 4
  • 36
  • 50
  • I have check these above links , but they havent mention the data parameter passing in ajax call. – chaya Aug 14 '14 at 13:51
0

Hey you can use in this way


    var opts =
        {
            'ajax'    :
            {
                'url': 'serverSideTableProviderPage',
                'type': 'POST',
                'contentType': 'application/json; charset=utf-8',
                'data':function(data)
                 {
                       return data = JSON.stringify(data);
                 }
            },
            'pagingType': 'simple',
            [more options ...]
        }
    $('table').dataTable(opts);

For more info visit

Community
  • 1
  • 1
khanz
  • 205
  • 1
  • 9
  • I have to pass the data parameters to ajax call : data: { formName:'afscpMsr', action:'search', field_nm: document.getElementById('msrdet').value, field_value:document.getElementById('srhmsr').value } – chaya Aug 14 '14 at 13:41
  • How to pass the data parameter to ajax call example action:search,field_nm: document.getElementById('msrdet').value – chaya Aug 14 '14 at 13:50
  • Please suggest me the answer – chaya Aug 14 '14 at 14:16