2017-05-29 69 views
0

我想使用自己的API函數從數據表中獲取數據,而不是使用常規的html表格選擇器。我也使用編輯器版本(沒有在片段中顯示),我使用本地表編輯功能(我想批量發送所有的數據一次)在自定義映射函數中使用Jquery Datatables API

我試過下面的代碼,它訪問API數據但我不能跳過單元​​格。

var data = $('#PrtTbl').find('tr').map(function() { 
       return PrtTbl.row(this).data() 
      }).get() 
      console.log(JSON.stringify(data)) 

我想申請這個code它映射的HTML表格,讓我來選擇特定的細胞,但的問題是,我無法弄清楚如何訪問API存儲的數據,僅 API函數可以給我同時訪問存儲的數據和隱藏列

var tbl = $('#RegSrc tr:has(td)').map(function(i, v) { 
var $td = $('td', this); 
return { 
    RecID: $td.eq(0).text(), 
    PtFilenum: $td.eq(1).text(), 
    Status: $td.eq(5).text() 
} }).get(); 

我的目標了進來會是這樣

{ 
"data": { 
    "2383": { 
     "RecID": 2383, 
     "PtFilenum": "15090248", 
     "PrtFilenum": 13090701, 
     "PrtStatus": "" 
    }, 
    "3387": { 
     "RecID": 3387, 
     "PtFilenum": "15090248", 
     "PrtFilenum": 15120996, 
     "PrtStatus": "" 
    }, 
    "3388": { 
     "RecID": 3388, 
     "PtFilenum": "15090248", 
     "PrtFilenum": 170227111, 
     "PrtStatus": "" 
    } 
}} 

我有下面顯示的結果爲兩個代碼

var tablenest = $('#RegSrc').DataTable({ 
 
    select: true, 
 
    "bPaginate": false, 
 
    "bFilter": false, 
 
    responsive: true, 
 
    deferRender: true, 
 
    "processing": true, 
 
    "serverSide": false, 
 
    bAutoWidth: true, 
 
    data: [{ 
 
    "RecID": 2383, 
 
    "PtFilenum": 15090248, 
 
    "PrtFilenum": 13090701, 
 
    "Fullname": " sadden ", 
 
    "PrtStatus": 1 
 
    }, { 
 
    "RecID": 2384, 
 
    "PtFilenum": 15090248, 
 
    "PrtFilenum": 15120996, 
 
    "Fullname": "marwam mohmmad saleem", 
 
    "PrtStatus": 1 
 
    }, { 
 
    "RecID": 2385, 
 
    "PtFilenum": 15090248, 
 
    "PrtFilenum": 170227111, 
 
    "Fullname": "asd dsf a", 
 
    "PrtStatus": 1 
 
    }], 
 
    order: [2, 'asc'], 
 
    keys: { 
 
    columns: ':not(:first-child)', 
 
    keys: [9] 
 
    }, 
 
    columns: [{ // Checkbox select column 
 
     data: null, 
 
     defaultContent: '', 
 
     className: 'select-checkbox', 
 
     orderable: false, 
 
     "width": "1%" 
 
    }, 
 
    { 
 
     "width": "50%", 
 
     data: "RecID", 
 
     "visible": false 
 
    }, 
 
    { 
 
     "width": "50%", 
 
     data: "PtFilenum", 
 
     "visible": false 
 
    }, 
 
    { 
 
     "width": "10%", 
 
     data: "PrtFilenum" 
 
    }, 
 
    { 
 
     "width": "40%", 
 
     data: "Fullname" 
 
    }, 
 
    { 
 
     "width": "10%", 
 
     data: "PrtStatus", 
 
     render: function(data, type, row) { 
 
     if (type === 'display') { 
 
      if (data == 1) { 
 
      return 'Partners'; 
 
      } else { 
 
      return 'Not Partners'; 
 
      } 
 
     } 
 
     return data; 
 
     }, 
 
     className: "dt-body-center" 
 
    }, 
 
    ], 
 

 
}); 
 

 
$("#btn1").click(function() { 
 
    var tbl = $('#RegSrc tr:has(td)').map(function(i, v) { 
 
    var $td = $('td', this); 
 
    return { 
 
     RecID: $td.eq(0).text(), 
 
     PtFilenum: $td.eq(1).text(), 
 
     Status: $td.eq(5).text() 
 
    } 
 
    }).get(); 
 
    console.log(JSON.stringify(tbl)) 
 
    return false; 
 
}) 
 

 
$("#btn2").click(function() { 
 
    var data = $('#RegSrc').find('tr').map(function() { 
 
    return tablenest.row(this).data() 
 
    }).get() 
 
    console.log(JSON.stringify(data)) 
 
    return false; 
 
}) 
 
$("#btn3").click(function() { 
 
    var tbl = $('#RegSrc tr:has(td)').map(function(i, v) { 
 
    return { 
 
     RecID: tablenest.cell(this, 1).data(), 
 
     PtFilenum: tablenest.cell(this, 2).data(), 
 
     PrtFilenum: tablenest.cell(this, 3).data(), 
 
     PrtStatus: tablenest.cell(this, 5).data() 
 

 
    } 
 
    }).get(); 
 
    console.log(JSON.stringify(tbl)) 
 
    return false; 
 
})
<link href="https://cdn.datatables.net/1.10.15/css/dataTables.bootstrap.min.css" rel="stylesheet" /> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script> 
 

 
<script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap.min.js"></script> 
 

 

 

 
<script type="text/javascript" charset="utf-8" src="https://cdn.datatables.net/v/dt/jqc-1.12.4/moment-2.18.1/dt-1.10.15/b-1.3.1/se-1.2.2/datatables.min.js"></script> 
 

 

 

 
<button id="btn1" class="btn btn-primary">Code1</button> 
 
<button id="btn2" class="btn btn-primary">Code2</button> 
 
<button id="btn3" class="btn btn-primary">Code3</button> 
 
<table id="RegSrc" class="table table-bordered table-striped table-condensed mb-none display responsive nowrap" cellspacing="0" width="100%"> 
 
    <thead> 
 
    <tr> 
 
     <th></th> 
 
     <th><b>RecID</b></th> 
 
     <th><b>Patient File Number</b></th> 
 
     <th><b>Partner File Number</b></th> 
 
     <th><b>Patient Name</b></th> 
 
     <th><b>Status</b></th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    </tbody> 
 
</table>

更新 我加入這個代碼這個片段,並似乎它的工作,

var tbl = $('#RegSrc tr:has(td)').map(function(i, v) { 
return { 
    RecID: tablenest.cell(this, 1).data(), 
    PtFilenum: tablenest.cell(this, 2).data(), 
    PrtFilenum: tablenest.cell(this, 3).data(), 
    PrtStatus: tablenest.cell(this, 5).data() 

} }).get(); 

但我仍然無法達到預期的效果

請幫忙嗎?

回答

0

於是,我從here

解決我張貼兩個代碼, 他們中的一個「代碼1」,與該表的作品,但在頁面通過記錄不循環,而代碼2遍歷所有的頁面(純數據表API利用)

var tablenest = $('#RegSrc').DataTable({ 
 
    select: true, 
 
    "bPaginate": false, 
 
    "bFilter": false, 
 
    responsive: true, 
 
    deferRender: true, 
 
    "processing": true, 
 
    "serverSide": false, 
 
    bAutoWidth: true, 
 
    data: [{ 
 
    "RecID": 2383, 
 
    "PtFilenum": 15090248, 
 
    "PrtFilenum": 13090701, 
 
    "Fullname": " sadden ", 
 
    "PrtStatus": 1 
 
    }, { 
 
    "RecID": 2384, 
 
    "PtFilenum": 15090248, 
 
    "PrtFilenum": 15120996, 
 
    "Fullname": "marwam mohmmad saleem", 
 
    "PrtStatus": 1 
 
    }, { 
 
    "RecID": 2385, 
 
    "PtFilenum": 15090248, 
 
    "PrtFilenum": 170227111, 
 
    "Fullname": "asd dsf a", 
 
    "PrtStatus": 1 
 
    }], 
 
    order: [2, 'asc'], 
 
    keys: { 
 
    columns: ':not(:first-child)', 
 
    keys: [9] 
 
    }, 
 
    columns: [{ // Checkbox select column 
 
     data: null, 
 
     defaultContent: '', 
 
     className: 'select-checkbox', 
 
     orderable: false, 
 
     "width": "1%" 
 
    }, 
 
    { 
 
     "width": "50%", 
 
     data: "RecID", 
 
     "visible": false 
 
    }, 
 
    { 
 
     "width": "50%", 
 
     data: "PtFilenum", 
 
     "visible": false 
 
    }, 
 
    { 
 
     "width": "10%", 
 
     data: "PrtFilenum" 
 
    }, 
 
    { 
 
     "width": "40%", 
 
     data: "Fullname" 
 
    }, 
 
    { 
 
     "width": "10%", 
 
     data: "PrtStatus", 
 
     render: function(data, type, row) { 
 
     if (type === 'display') { 
 
      if (data == 1) { 
 
      return 'Partners'; 
 
      } else { 
 
      return 'Not Partners'; 
 
      } 
 
     } 
 
     return data; 
 
     }, 
 
     className: "dt-body-center" 
 
    }, 
 
    ], 
 

 
}); 
 

 
$("#btn1").click(function() { 
 
    var tbl = $('#RegSrc tr:has(td)').map(function(i, v) { 
 
    var myObject = new Object(); 
 
    var Data = tablenest.cell(this, 1).data() 
 
    myObject[Data] = { 
 
     RecID: tablenest.cell(this, 1).data(), 
 
     Status: tablenest.cell(this, 5).data(), 
 
    } 
 
    return myObject 
 
    }).get(); 
 

 
    var dt = {} 
 
    dt.data = tbl 
 
    console.log(JSON.stringify(dt)) 
 
    return false; 
 
}) 
 

 
$("#btn2").click(function() { 
 
    var tbldta = $.map(tablenest.rows().data(), function(d, i) { 
 
    var myObject = new Object(); 
 
    var Data = d.RecID 
 
    myObject[Data] = { 
 
     RecID: d.RecID, 
 
     PrtStatus: d.PrtStatus 
 
    } 
 
    return myObject 
 
    }); 
 
    var DtaObj = {} 
 
    DtaObj.data = tbldta 
 
    console.log(JSON.stringify(DtaObj)) 
 
    return false; 
 
})
<link href="https://cdn.datatables.net/1.10.15/css/dataTables.bootstrap.min.css" rel="stylesheet" /> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script> 
 

 
<script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap.min.js"></script> 
 

 

 

 
<script type="text/javascript" charset="utf-8" src="https://cdn.datatables.net/v/dt/jqc-1.12.4/moment-2.18.1/dt-1.10.15/b-1.3.1/se-1.2.2/datatables.min.js"></script> 
 

 

 

 
<button id="btn1" class="btn btn-primary">Code1</button> 
 
<button id="btn2" class="btn btn-primary">Code2</button> 
 

 
<table id="RegSrc" class="table table-bordered table-striped table-condensed mb-none display responsive nowrap" cellspacing="0" width="100%"> 
 
    <thead> 
 
    <tr> 
 
     <th></th> 
 
     <th><b>RecID</b></th> 
 
     <th><b>Patient File Number</b></th> 
 
     <th><b>Partner File Number</b></th> 
 
     <th><b>Patient Name</b></th> 
 
     <th><b>Status</b></th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    </tbody> 
 
</table>

相關問題