2017-07-20 40 views
1

我開始學習如何使用dataTables,我用它來顯示一些JSON數據,我從Excel文件導入 我有表顯示,但它不是與標題居中。 我一直在尋找一個解決方案很長一段時間,但我還沒有找到一個。 代碼包含在代碼片段中,並且是輸出的屏幕截圖。 如果我做錯了什麼,請幫助我。jquery datatable不居中

謝謝! :)

/* set up XMLHttpRequest */ 
 

 
var url = "LICENCIAMENTOS.xlsx"; 
 
var oReq = new XMLHttpRequest(); 
 
oReq.open("GET", url, true); 
 
oReq.responseType = "arraybuffer"; 
 

 
oReq.send(); 
 
oReq.onload = function(e) { 
 
    var arraybuffer = oReq.response; 
 

 
    /* convert data to binary string */ 
 
    var data = new Uint8Array(arraybuffer); 
 
    var arr = new Array(); 
 
    for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]); 
 
    var bstr = arr.join(""); 
 

 
    /* Call XLSX */ 
 
    var workbook = XLSX.read(bstr, { 
 
    type: "binary" 
 
    }); 
 

 
    /* DO SOMETHING WITH workbook HERE */ 
 

 
    var first_sheet_name = workbook.SheetNames[5]; 
 
    //var address_of_cell='A5'; 
 

 
    /* Get worksheet */ 
 

 
    var worksheet = workbook.Sheets[first_sheet_name]; 
 
    v = XLSX.utils.sheet_to_json(worksheet); 
 

 
    //DataTable \t \t \t \t \t \t \t 
 
    $('#data-table').DataTable({ 
 
    "aaData": v, 
 
    "aoColumns": [{ 
 
     "mData": "Cenas", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "PT", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "kV", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "S/A", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "NOME", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "SGD", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Comprimento (m)", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "EDIS", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Licenciamento Externo", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Data Execucao", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "APE", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "DREC/DRELVT", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "A-P-E-", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Editos - GBCI", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "GBCI-Recortes", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Editos - ME", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Estradas", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Refer", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "ICN", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Txas-NI", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Taxas-Pag.", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Licenciamento", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Pedido de Vistoria", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Licenca de Exploracao", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "DRE 161.10.03.----", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Avisos", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "Observacoes", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "IF", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "SGD", 
 
     sDefaultContent: '' 
 
     }, 
 
     { 
 
     "mDataProp": "DTA PGT", 
 
     sDefaultContent: '' 
 
     } 
 
    ] 
 
    }); 
 
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.10.8/xlsx.full.min.js"></script> 
 
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.css" /> 
 
<script type="text/javascript" src="https://cdn.datatables.net/r/bs-3.3.5/jqc-1.11.3,dt-1.10.8/datatables.min.js"></script> 
 

 

 
    <div class="container"> 
 
    <h1 align="center">LICENCIAMENTOS</h3> 
 
     <h3 align="center">Ansiao</h3> 
 
     <table id="data-table" style="text-align:center;"> 
 
     <thead> 
 
      <tr> 
 
      <th>X</th> 
 
      <th>PT</th> 
 
      <th>kV</th> 
 
      <th>S/A</th> 
 
      <th>NOME</th> 
 
      <th>SGD</th> 
 
      <th>Comprimento</th> 
 
      <th>EDIS</th> 
 
      <th>Licenciamento externo</th> 
 
      <th>Data Execucao</th> 
 
      <th>APE</th> 
 
      <th>DREC/DRELVT</th> 
 
      <th>A-P-E-</th> 
 
      <th>Editos</th> 
 
      <th>GBCI-Recortes</th> 
 
      <th>Editos-ME</th> 
 
      <th>Estradas</th> 
 
      <th>Refer</th> 
 
      <th>ICN</th> 
 
      <th>Txas-NI</th> 
 
      <th>Taxas-Pag.</th> 
 
      <th>Licenciamento</th> 
 
      <th>Pedido de vistoria</th> 
 
      <th>Licença de exploracao</th> 
 
      <th>DRE 161.10.03----</th> 
 
      <th>Avisos</th> 
 
      <th>Obeservacoes</th> 
 
      <th>if</th> 
 
      <th>SGD</th> 
 
      <th>DTA PGT</th> 
 

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

 
    </div>

DataTable

回答

1

添加margin: 0 auto;到表style

<table id="data-table" style="text-align:center; margin: 0 auto;" > 

它會自動調整水平邊距,以便您的表將被集中

+0

非常感謝!這工作得很好:) –