2016-12-01 270 views
0

我在我的web應用程序中使用DataTable,我試圖修復水平滾動前3列。在數據表中的固定列

檢查DataTable文檔後,建議在啓動數據表時使用fixedColumns屬性。我嘗試了他們的解決方案,但最終得到了一個亂七八糟的非風格的數據表,看起來數據表被轉換成了兩個內表,其中固定表沒有與可移動表相同的風格。

下面的是我如何開始我的數據表:

table = $('#MailDataTable').DataTable({ 
      'bJQueryUI' :true, 
      "processing": true, 
      "serverSide": true, 
      "fixedColumns": true, 
      "ajax": { 
       "url": "GetCTSRecords?DBName="+"${DBName}"+"&TableName="+"${TableName}", 
       "type":"POST", 
       "data": function (d) { 
        return $.extend({}, d, { 
        "DBName"    : "${DBName}", 
        "TableName"    : "${TableName}", 
        "Where"     : Where, 
        "OrderBy"    : "${OrderBy}", 
        "JSONCTS"    : "true", 
        "Count"     : "no", 
        "FieldList"    : "min", 
        "totalDataTableRecords" : totalDataTableRecords, 
        "iconsToDisplay"  : "${iconsToDisplay}", 
        "additionalFilter"  : searchFields, 
        "additionalWhere"  : additionalWhere, 
        "DocCount"    : "${DocCount}", 
        "LinkType"    : "both", 
        "fromWhere"    :fromWhere 
        }); 
       } 
      }, 
      "scrollX": true, 
      //"bSort":true, 
      "scrollCollapse": true, 
      "iDisplayLength": 20, 
      "lengthMenu": [10,20, 25, 50, 75, 100], 
      "blength":true, 
      "pagingType": "input", 
      "language": { 
       "sSearch": " " + "${tr.Search}" + " ", 
       "zeroRecords": "${tr.NoMatchFound}", 
       "info": "_START_ "+"${tr.to}" + " _END_ " + "${tr.Items} " +"${tr.of}" +" _TOTAL_ ", 
       "infoFiltered": "${tr.of}" + " _TOTAL_ " + "${tr.Items}", 
       "sLengthMenu": " _MENU_ ", 
       "infoEmpty": "${tr.NoEntries}", 
       "sEmptyTable": "${tr.TableIsEmpty}", 
       "sProcessing": "<img src='apps/ctsc/images/Resources/Loading.gif' /><span class='GridLoadingMessage'>" + resources_Loading + " " + resources_PleaseWait + "</span>", 
       oPaginate: { 
        "sFirst": "", 
        "sLast": "", 
        "sNext": "", 
        "sPrevious": "" 
       } 
      }, 
      "bAutoWidth": false, 
      "initComplete":function(settings,json){ 
       $("#DataTable_wrapper .Header th > div > span").each(function(index){ 
        if($(this).parent().text()!="") 
         $(this).addClass("icon"); 
       }); 
      $('.paginate_page').text(resources_Page+" "); 
      $('.paginate_of').text($('.paginate_of').text().replace(/of/gi,resources_Of)); 

      addToolTips(); 
      totalDataTableRecords = json.totalDataTableRecords; 
      $("#dataTableHeader1").css("opacity","1"); 
      }, 
      columns: columnsCust, 
      columnDefs: columnDef 
      ,deferRender: true 
      , dom: '<"top">rt<"bottom">ilBp<"clear">' 
      ,buttons: [ 
        { 
         header: true, 
         text: "<img src='apps/ctsc/images/Resources/refresh.svg' style='width:16px;height:16px' title='"+ resources_Refresh+"'/>", 
         className: 'refreshbtn', 
         action: function(){ 
          if ($('#MailDataTable tbody tr').length > 0) 
          { 
           totalDataTableRecords = ""; 
           var table = $('#MailDataTable').DataTable(); 
           table.destroy(); 
          } 
          BuildDataTable(); 
         } 
        }, 
         { 
          extend: 'print', 
          header: true, 
          text: "<img src='apps/ctsc/images/Resources/Print.svg' style='width:16px;height:16px' title='"+resources_Print+"'/>" 
         }, 
         { 
          extend: 'excel', 
          header: true, 
          text:"<img src='apps/ctsc/images/Resources/Excel.svg' style='width:16px;height:16px' title='"+ resources_Excel+"'/>" 
         }, 
         { 
          extend: 'pdf', 
          header: true, 
          text: "<img src='apps/ctsc/images/Resources/PDF.svg' style='width:16px;height:16px' title='"+ resources_Pdf+"'/>", 
          orientation: 'landscape', 
          pageSize: 'LEGAL' 
         } 
        ], 


     }); 

下面是我最初的數據表,然後將新的應用fixedColumns屬性之後:

前:

enter image description here

後:

enter image description here

正如你所看到的,在第二張圖片水平滾動消失,DataTable的造型成爲損壞。

任何人都有關於如何修復DataTable中的列的想法。 請注意,我不想擺脫DataTable,我喜歡它,如果此屬性不起作用,我會接受任何css解決方案。

+0

我已經有了最好的運氣,將autoWidth設置爲false,爲除一列之外的所有列設置列寬,然後將表格放在div中並將div設置爲比我想要的表格大一點是。 – Bindrid

+0

我沒有收到您的評論,您之前是否遇到過這樣的問題?你是如何解決它的? –

+0

我遇到的問題是我需要從單獨的表中排列排列。但由於自動調整尺寸的原因,列寬被忽略,不能排列。我會稍後發佈我的解決方案,以便您能看到我所做的並看看它是否有幫助。 – Bindrid

回答

0

enter image description here

你,從我所需要的正確的4列排隊的圖片中看到。

這裏是略表定義我用來設置所有三個表:

 var commonDefs = { 
      autoWidth: false, 
      columnDefs: [{ 
       targets: [-1, -2, -3], 
       width: "100px", 
       type: "$money", 
       className: "dt-head-center dt-body-right", 
       render: function (data, type, full, meta) { return onColumnRender(data, type, full, meta); } 
      }, 
      { targets: [-4], type: "string", width: "75px", className: "dt-center" } 

      ] 
     }; 
     // show revision level table 
     if ($("#tblRevTotal").length > 0) { 
      $("#tblRevTotal").DataTable(commonDefs); 
     } 
     // append definital for the remaining tables. (all but revision summary) 
     commonDefs.columnDefs[commonDefs.columnDefs.length] = { targets: [0], width: "100px" }; 
     commonDefs.columnDefs[commonDefs.columnDefs.length] = { targets: [1], width: "325px" }; 

     // footer not used. 
     $(".TaskDataTable").each(function() { 
      $(this).find("tfoot").children().remove(); 
      $(this).DataTable(commonDefs); 
     }) 

和每個樹表的HTML。 DataTable base css的寬度爲100%,因此我將外部div設置爲我想要的較大表格的寬度。浮動權被添加到內部div。

 <div id="TPSRollUpContainer" style="width: 1050px;"> 

       <fieldset id="fsRevisionRollUp"> 
        <legend>Overview Total</legend> 
        <div style="float: right;"> 
         <table class="display" id="tblRevTotal"> 
          <thead> 
           <tr> 
            <th></th> 
            <th>Money One</th> 
            <th>Money Two</th> 
            <th>Total</th> 
           </tr> 

          </thead> 
          <tbody> 
          <tr> 
           <th class="dt-subheader">Funded</th> 
           <td>400000</td> 
           <td>400000</td> 
           <td>800000</td> 
          </tr> 
          <tr> 
           <th class="dt-subheader">Unfunded</th> 
           <td>0</td> 
           <td>0</td> 
           <td>0</td> 
          </tr> 
          <tr> 
           <th class="dt-subheader">Total</th> 
           <td>40000</td> 
           <td>400000</td> 
           <td>800000</td> 
          </tr> 
           </tbody> 
          <tfoot></tfoot> 
         </table> 
        </div> 
       </fieldset> 
      <div> 
       <fieldset> 
        <legend> 
         Task 1 
        </legend><div class="TableContainer" id="divTaskNo_1"> 
         <table class="TaskDataTable display" id="tblTaskNo_1"> 
          <thead> 
           <tr> 
            <th>Number</th><th>Title</th><th>Funded</th><th></th><th>Money Two</th><th>Total</th> 
           </tr> 
          </thead><tbody> 
           <tr class="dt-subheader"> 
            <td>Task 1</td><td>Title123456789</td><td>Yes</td><td>4000000</td><td>400000</td><td>80000</td> 
           </tr><tr class="dt-subheader"> 
            <td></td><td></td><td>No</td><td>0</td><td>0</td><td>0</td> 
           </tr><tr> 
            <td>Step 1.1</td><td>Title123456789</td><td>Yes</td><td>40000</td><td>400000</td><td>800000</td> 
           </tr> 
          </tbody><tfoot> 
          </tfoot> 
         </table> 
        </div> 
       </fieldset> 
      </div><div> 
       <fieldset> 
        <legend> 
         Task 2 
        </legend><div class="TableContainer" id="divTaskNo_2"> 
         <table class="TaskDataTable display" id="tblTaskNo_2"> 
          <thead> 
           <tr> 
            <th>Number</th><th>Title</th><th>Funded</th><th>Money One</th><th>Money Two</th><th>Total</th> 
           </tr> 
          </thead><tbody> 
           <tr class="dt-subheader"> 
            <td>Task 2</td><td>Task # 2</td><td>Yes</td><td>32.00</td><td>40.00</td><td>72.00</td> 
           </tr><tr class="dt-subheader"> 
            <td></td><td></td><td>No</td><td>0</td><td>0</td><td>0</td> 
           </tr><tr> 
            <td>Step 2.1</td><td>Step Number # 1</td><td>Yes</td><td>16.00</td><td>20.00</td><td>36.00</td> 
           </tr><tr> 
            <td>Step 2.3</td><td>Step Number # 1</td><td>Yes</td><td>0.00</td><td>0.00</td><td>0.00</td> 
           </tr><tr> 
            <td>Step 2.4</td><td>Step Number # 1</td><td>Yes</td><td>16.00</td><td>20.00</td><td>36.00</td> 
           </tr> 
          </tbody><tfoot> 

          </tfoot> 
         </table> 
        </div> 
       </fieldset> 
      </div> 
     </div> 
+0

那裏有很多東西。我希望它有一些用處。 – Bindrid