2011-03-27 448 views
3

我一直在抓我的頭,我一直在使用jQuery數據表http://www.datatables.net來顯示數據庫中的記錄。jquery datatable生成上自定義查詢

我一直在使用內置的搜索字段,但它的單一字段。

我想進行報告類型搜索,例如,顯示給定開始日期和結束日期之間的所有訂單,訂單由誰分配,訂單分配給的工廠等等。我想根據多個輸入來顯示錶格,這些輸入可用於構建生成報表的查詢。

所以,當時我在搜索字段中輸入搜索參數並根據返回的結果重繪表格時,我的想法是使用少量表單域的表單。

我想知道如何從表單字段發送數據,以便生成查詢並返回響應並重繪表格。

下面

是一個代碼示例中,我一直在使用

`/ 初始化數據表/

 $('#second_tab_table').dataTable({ 
      "bProcessing": true, 
      "bServerSide": true, 
      "sAjaxSource": "components/report/report_processing.php?status=displayOrderReport", 
      "bJQueryUI": true, 
      "bStateSave": true, 
      "bAutoWidth": false, 
      "sPaginationType": "full_numbers" 
     });` 

我也看了一下自定義過濾http://www.datatables.net/examples/plug-ins/range_filtering.html,但這並不能成爲目的。

我將不勝感激任何幫助。

這種方式解決了這個問題:


`$(文件)。就緒(函數(){

 /*Initialize the data table*/ 
     $('#second_tab_table2').dataTable({ 
      "bProcessing": true, 
      "bServerSide": true, 
      "sAjaxSource": "components/report/report_processing.php?status=displayOrderReport&sid=<?php echo $_REQUEST['sid']; ?>", 
      "bJQueryUI": true, 
      "bStateSave": true, 
      "bAutoWidth": false, 
      "sPaginationType": "full_numbers" 
     }); 


     $("#oc_id").live("change", function() { 

      var data = $("#formulario_personal").serialize(); 
      var dataString = 'oc_id=8&status=displayOrderReport&'+ data; 
      $.ajax({       
        type: "POST", 
        url: "components/report/report_processing.php", 
        data: dataString, 
        cache:false, 
        success: function(html){; 
          var oTable = $('#second_tab_table').dataTable(); 
          oTable.fnDraw(); 
        } 
       }); 
     }); 


});` 

看來,這將工作: http://www.datatables.net/examples/server_side/custom_vars.html

+4

你應該把你的解決方案放在你自己的問題的答案,並將其標記爲正確的,以便這個問題從「未答覆」列表中消失:)謝謝。 – Challe 2012-06-27 16:43:10

回答

0

正確的方法是覆蓋fnServerDatafnServerParams

雖然這是版本,所以您需要確保您的版本正確。我認爲fnServerData更好地支持舊版本。所以我會展示它的例子。

此功能是well documented。這是一個usage example

從他們的例子中,看看他們如何使用aoData.push語句向查詢添加數據。 我正在使用它,它適用於我。所以如果你有更多的問題,請讓我知道。

$(document).ready(function() { 
    $('#example').dataTable({ 
     "bProcessing": true, 
     "bServerSide": true, 
     "sAjaxSource": "../examples_support/server_processing.php", 
     "fnServerData": function (sSource, aoData, fnCallback) { 
      /* Add some extra data to the sender */ 
      aoData.push({ "name": "more_data", "value": "my_value" }); 
      $.getJSON(sSource, aoData, function (json) { 
       /* Do whatever additional processing you want on the callback, then tell DataTables */ 
       fnCallback(json) 
      }); 
     } 
    }); 
});