checkbox - Datatable 无法将 excel 中更新的复选框值导出为 0 或 1,始终仅显示最初选中的值

标签 checkbox datatable datatables datatables-1.10 tabletools

尝试下载数据表,最初能够为复选框获取 0 或 1 个值(对于列字段之一“已验证”),但是当我切换复选框并导出为 excel 时,无法检索最新的复选框值. 最初我只有in the first excel image在数据库中没有勾选第二个复选框,因此它在 Excel 工作表中正确地将其值导出为 0 但是当我更改值并再次导出它时它没有采用 updated checkbox ticks如您在随附的快照中所见,更新了最新值。

   var j=0;
    $(document).ready(function(){

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>    var table;
    function Xhr(id)
    {
    return $.ajax(
    {type:"GET",
    url:'http://localhost:8080/SSRSReports/v1/test',
    dataType:'json',
    cache:false,
    async:true,

    beforeSend:
    function(){$(id).append('<img src="images/spinner.gif" class="spin"/>');
    }})
    .always(function(){
    $(id).find('.spin').fadeOut(4000,function(){$(this).remove();
    });
    })
    .fail(function()
    {console.log("There was a problem while trying to get data at "+d);});
    }
    var radio=[];
    var j=0;
    $(document).ready(function(){
    	Xhr('#report').done(function (json) {
    		console.log(json);
    		var x;
    	
    			jQuery_2_2_2.each(json, function (i, report) {
    		
    				radio.fill(0);
    				if(report.testSampleOutOfStabilityIndt==1)
    					{
    						x += '<tr><td>' + report.studyId + '</td>';
    					    x += '<td>' + report.country + '</td>';
    					    x += '<td>' + report.investigatorName + '</td>';
    					    x += '<td>' + report.screenId + '</td>';
    						x += '<td>' + report.subjectId + '</td>';
    						x += '<td>' + report.subjectInitials + '</td>';
    						x += '<td>' + report.requisitionNumber + '</td>';
    						x += '<td>' + report.testName + '</td>';
    						x += '<td>' + report.visitName + '</td>';
    						x += '<td>' + report.collectionDate + '</td>';
    						x += '<td>' + report.collectionTime + '</td>';
    						x += '<td>' + report.resultDate + '</td>';
    						x += '<td>' + report.resultTime + '</td>';
    						x += '<td>' + report.minutesToResult + '</td>';
    						x += '<td>' + report.minutesSampleStabilityLimit + '</td>';
    						x += '<td>' + report.testResult + '</td>';
    						x += '<td>' + report.daysSampleStabilityLimit + '</td>';
    						x += '<td>' + report.daysToResult + '</td>';
    						x += '<td>' + report.testSampleOutOfStabilityIndt + '</td>';
    					//console.log(i);
    						if(j==1)
    							{
    							x += '<td id="verified'+j+'"><input id="verified'+j+'" style="font-size:xx-small;" type="checkbox" onclick="handleChange(this)"></td></tr>';
    							}
    						else{
    							x += '<td id="verified'+j+'"><input id="verified'+j+'" style="font-size:xx-small;" type="checkbox" checked onclick="handleChange(this)"></td></tr>';
    						}
    						j++;
    					
    					}
    				
    			});//.each
    			jQuery_2_2_2('#reportbody').append(x);
    			
    			
    			table=jQuery_2_2_2('#reporttable').DataTable({
    				"stateSave": true,
    				checkboxRefresh:true,
    				dom: 'Bfrtip',
    			    buttons: [
    			    	{
    			    	extend: 'excel',
    		            exportOptions: {
    		                orthogonal: 'sort'
    		            }
    		        }        
    		    ],
    		    columnDefs: [{
    		       targets:[19],
    		       render: function(data, type, row, meta){
    		          if(type === 'sort'){
    		             var $input = $(data).find('input[type="checkbox"]').addBack();
    		             data = ($input.prop('checked')) ? "1" : "0";
    		          }

    		          return data;    
    		       }
    		    }],
    			     
    			    
    			    scrollY:        true,
    			    scrollX:        true,
    			    scrollCollapse: true,
    			    fixedColumns:   {
    			        leftColumns: 1,
    			        rightColumns: 1
    			    }
    			
    			    });//.datatable

    		});//.done
    		
    	
    		
    });//document.ready
    var k;

    function handleChange(e){
[enter image description here][1]
       $(e).attr('checked', !($(e).attr('checked')));  
    	console.log(e.id);
    	
    	

    	
    }</code></pre>
<pre class="snippet-code-css lang-css prettyprint-override"><code>    th, td {
    	white-space: nowrap;
    }

    div.dataTables_wrapper {
    	width: 1600px;
    	margin: 0 auto;
    }</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Reports</title>
    <script type="text/javascript"
    	src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <link
    	href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"
    	type="text/css" rel="stylesheet" />
    <link
    	href="https://cdn.datatables.net/fixedcolumns/3.2.6/css/fixedColumns.dataTables.min.css"
    	type="text/css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-2.2.2.min.js"
    	integrity="sha256-36cp2Co+/62rEAAYHLmRCPIych47CvdM+uTBJwSzWjI="
    	crossorigin="anonymous"></script>
    <script

    	src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript"
    	src="https://cdn.datatables.net/fixedcolumns/3.2.6/js/dataTables.fixedColumns.min.js"></script>
    <script
    	src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script>
    	<link
    	href="https://cdn.datatables.net/buttons/1.5.2/css/buttons.dataTables.min.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript"
    	src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
    <script type="text/javascript"
    	src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
    <script type="text/javascript"
    	src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
    <script type="text/javascript"
    	src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.html5.min.js"></script>
    <script type="text/javascript">var jQuery_2_2_2=$.noConflict(true);</script>

    </head>
    <body>
    	<form id="myform">
    		<div id="report" style="padding: 1% 1%">
    			<h2 class="section_header">
    				<span style="color: #18d1f2">RE</span>PORT
    			</h2>
    			</br>

    			<table id="reporttable" class="display" cellspacing="0" width="100%"
    				border="1px solid black" ; border-collapse="collapse";>
    				<thead>
    					<tr>
    						<th>StudyID</th>
    						<th>Country</th>
    						<th>Investigator_Name</th>
    						<th>ScreenID</th>
    						<th>SubjectID</th>
    						<th>Subject_Initials</th>
    						<th>Requisition_Number</th>
    						<th>Test_Name</th>
    						<th>Vist_Name</th>
    						<th>Collection_Date</th>
    						<th>Collection_Time</th>
    						<th>Result_Date</th>
    						<th>Result_Time</th>
    						<th>MinutesToResult</th>
    						<th>Minutes_Sample_Stability_Limit</th>
    						<th>Test_Result</th>
    						<th>Days_Sample_Stability_Limit</th>
    						<th>DaysToResult</th>
    						<th>Taste_Sample_Out_of_Stability_Ind</th>
    						<th>Verified ?</th>


    					</tr>
    				</thead>
    				<tbody id="reportbody">

    				</tbody>
    				<tfoot>
    					<tr>
    						<th>StudyID</th>
    						<th>Country</th>
    						<th>Investigator_Name</th>
    						<th>ScreenID</th>
    						<th>SubjectID</th>
    						<th>Subject_Initials</th>
    						<th>Requisition_Number</th>
    						<th>Test_Name</th>
    						<th>Vist_Name</th>
    						<th>Collection_Date</th>
    						<th>Collection_Time</th>
    						<th>Result_Date</th>
    						<th>Result_Time</th>
    						<th>MinutesToResult</th>
    						<th>Minutes_Sample_Stability_Limit</th>
    						<th>Test_Result</th>
    						<th>Days_Sample_Stability_Limit</th>
    						<th>DaysToResult</th>
    						<th>Taste_Sample_Out_of_Stability_Ind</th>
    						<th>Verified?</th>


    					</tr>
    				</tfoot>
    			</table>

    		</div>
    		<input
    			style="color: white; font-weight: bold; font-size: larger; width: 80px; height: 30px; background-color: deepskyblue; border: 1px solid #223445; border-radius: 18px; -webkit-border-radius: 5px; -moz-border-radius: 5px"
    			type="submit">
    	</form>
    </body>
    <script></code></pre>
</div>
</div>


        Xhr('#report').done(function (json) {
            console.log(json);
            var x;

                jQuery_2_2_2.each(json, function (i, report) {

                    radio.fill(0);
                    if(report.testSampleOutOfStabilityIndt==1)
                        {
                            x += '<tr><td>' + report.studyId + '</td>';
                            x += '<td>' + report.country + '</td>';
                            x += '<td>' + report.investigatorName + '</td>';
                            x += '<td>' + report.screenId + '</td>';
                            x += '<td>' + report.subjectId + '</td>';
                            x += '<td>' + report.subjectInitials + '</td>';
                            x += '<td>' + report.requisitionNumber + '</td>';
                            x += '<td>' + report.testName + '</td>';
                            x += '<td>' + report.visitName + '</td>';
                            x += '<td>' + report.collectionDate + '</td>';
                            x += '<td>' + report.collectionTime + '</td>';
                            x += '<td>' + report.resultDate + '</td>';
                            x += '<td>' + report.resultTime + '</td>';
                            x += '<td>' + report.minutesToResult + '</td>';
                            x += '<td>' + report.minutesSampleStabilityLimit + '</td>';
                            x += '<td>' + report.testResult + '</td>';
                            x += '<td>' + report.daysSampleStabilityLimit + '</td>';
                            x += '<td>' + report.daysToResult + '</td>';
                            x += '<td>' + report.testSampleOutOfStabilityIndt + '</td>';
                        //console.log(i);
                            if(j==1)
                                {
                                x += '<td id="verified'+j+'"><input id="verified'+j+'" style="font-size:xx-small;" type="checkbox" onclick="handleChange(this)"></td></tr>';
                                }
                            else{
                                x += '<td id="verified'+j+'"><input id="verified'+j+'" style="font-size:xx-small;" type="checkbox" checked onclick="handleChange(this)"></td></tr>';
                            }
                            j++;

                        }

                });//.each
                jQuery_2_2_2('#reportbody').append(x);


                table=jQuery_2_2_2('#reporttable').DataTable({
                    "stateSave": true,
                    checkboxRefresh:true,
                    dom: 'Bfrtip',
                    buttons: [
                        {
                        extend: 'excel',
                        exportOptions: {
                            orthogonal: 'sort'
                        }
                    }        
                ],
                columnDefs: [{
                   targets:[19],
                   render: function(data, type, row, meta){
                      if(type === 'sort'){
                         var $input = $(data).find('input[type="checkbox"]').addBack();
                         data = ($input.prop('checked')) ? "1" : "0";
                      }

                      return data;    
                   }
                }], 
                    scrollY:        true,
                    scrollX:        true,
                    scrollCollapse: true,
                    fixedColumns:   {
                        leftColumns: 1,
                        rightColumns: 1
                    }

                    });//.datatable

            });//.done
    });/
    function handleChange(e){
        $(e).attr('checked', !($(e).attr('checked'))); 
    }

最佳答案

又试了下,导出的时候基本上改了格式。还更改了复选框数据以手动更改。

$(document).ready(function() {

$('.checkbox').click(function() {
    debugger
    if ($(this).data("checked") === 0) {
        $(this).data("checked", "1")
    } else {
        $(this).data("checked", "0")
    }
});
$('#example').DataTable({
    dom: 'Bfrtip',
    buttons: [{
            extend: 'copyHtml5',
            exportOptions: {
                orthogonal: 'export'
            }
        },
        {
            extend: 'excelHtml5',
            exportOptions: {
                orthogonal: 'export',
                format: {
                    body: function(data, row, column, node) {
                        // Strip $ from salary column to make it numeric
                        debugger;
                        if (column === 0) {
                            data = $(node).children().data("checked");
                        }
                        return data;
                    }
                }
            }
        },
        {
            extend: 'pdfHtml5',
            exportOptions: {
                orthogonal: 'export'
            }
        }
    ]

});

});

这是相同的 https://jsfiddle.net/raheelshah31/fm6a74yu/2/ 的 fiddle

关于checkbox - Datatable 无法将 excel 中更新的复选框值导出为 0 或 1,始终仅显示最初选中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52894619/

相关文章:

javascript - jQuery DataTable 多行表头

javascript - 默认更改为DataTable

vb.net - 从数据表中获取值(value)

javascript - 在 colorbox 内提交表单后重新加载 dataTable

javascript - 数据表扩展 excel 选项不适用于分页

javascript - DataTables - 默认禁用所有功能

java - 如何动态获取页面上显示的复选框的id

javascript - 使用 JavaScript 隐藏复选框时取消选中该复选框

JavaScript 不检查复选框是否被选中

javascript - jquery - 复选框 oncheck 总计将更新