【问题标题】:How to pass the ajax result data into jquery datatable如何将ajax结果数据传递到jquery数据表中
【发布时间】:2014-08-14 12:42:06
【问题描述】:

我想将ajax页面的结果数据传递给jquery datatable函数,请建议我如何将结果传递给datatable函数的正确语法:

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 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页面将结果显示在datatable中,这个结果是json格式的:

  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);

【问题讨论】:

  • 遵循 dataTables 网站上的示例。您需要优化初始化选项

标签: php jquery ajax datatable jquery-datatables


【解决方案1】:

你可以这样用

变量选择 = { “阿贾克斯”: { 'url': 'serverSideTableProviderPage', “类型”:“发布”, 'contentType': '应用程序/json;字符集=utf-8', “数据”:函数(数据) { 返回数据 = JSON.stringify(data); } }, “分页类型”:“简单”, [更多选择 ...] } $('table').dataTable(opts);

欲了解更多信息visit

【讨论】:

  • 我必须将数据参数传递给 ajax 调用: data: { formName:'afscpMsr', action:'search', field_nm: document.getElementById('msrdet').value, field_value:document .getElementById('srhmsr').value }
  • 如何将数据参数传递给ajax调用示例action:search,field_nm: document.getElementById('msrdet').value
  • 请告诉我答案
【解决方案2】:

您是否尝试过使用 aaData 属性:

您可以在此处找到有关数据表的各种数据源的信息:

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
                });

                });

【讨论】:

  • 我已经检查了上面的这些链接,但他们没有提到在 ajax 调用中传递的数据参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 2011-06-19
  • 2015-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多