我有一个数据表,其中我显示子行扩展崩溃功能。它运行良好,但我想得到一个表的最后一个td的内容。现在我已经创建了一个函数,它放置了一些硬编码值在数据表扩展的地方。在那个地方我想获得那些td值。 !
这是我发布的代码
<html>
<head>
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.5/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/responsive/1.0.4/css/dataTables.responsive.css">
<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/responsive/1.0.4/js/dataTables.responsive.min.js"></script>
</head>
<body>
<style type="text/css">
td.details-control {
background: url('http://www.datatables.net/examples/resources/details_open.png') no-repeat center center;
cursor: pointer;
}
tr.shown td.details-control {
background: url('http://www.datatables.net/examples/resources/details_close.png') no-repeat center center;
}
</style>
<script type="text/javascript" class="init">
/* Formatting function for row details - modify as you need */
function format ( d ) {
var v;
$("#example tbody tr").each(function() {
// Within tr we find the last td child element and get content
v = $(this).find("td:last-child").html();
return v;
});
// Within tr we find the last td child element and get content
//alert($(this).find("td:last-child").html());
return '<fieldset> <legend> </legend> <table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
'<tr>'+
'<td>'+v+'</td>'
'</tr>'
'</table> </fieldset>'
}
$(document).ready(function() {
var table = $('#example').DataTable();
// Add event listener for opening and closing details
$('#example tbody ').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
console.log(row);
row.child( format(row.data()) ).show();
tr.addClass('shown');
}
} );
// End add event
$("#divPopUp").dialog({
resizable: true,
autoOpen: false,
width: 550,
modal: true,
buttons: {
"Save": function() {
var text = $(this).find( ":checkbox:checked" ).map(function() {
return this.value+' ';
}).get().join();
var obj = $(this).data("opener");
$(obj).parents('td:first').siblings(':eq(2)').find(':text').val(text);
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close:function(){
$(this).find( ":checkbox" ).removeAttr('checked');
$( this ).dialog( "close" );
}
});
$('button.btn').on('click', function(){
var title = $(this).parents('td:first').siblings(':eq(0)').text();
console.log("title is : " + title);
$( "#divPopUp" ).data('opener', this).dialog( "option", "title", title ).dialog( "open" );
var text = $(this).parents('td:first').siblings(':eq(2)').find(':input').val();
if($.trim(text) != ''){
var texts = text.split(" ,");
$.each(texts, function(i, value){ $("#divPopUp").find(':checkbox[value="'+$.trim(value)+'"]').prop('checked', true);
});
}
});
} );
</script>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td class="details-control" ></td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</td>
</tr>
<tr>
<td class="details-control" ></td>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</td>
</tr>
<tr>
<td class="details-control" ></td>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</td>
</tr>
</tbody>
</table>
在该格式函数中,我想获得datatable的所有td值。有人请帮助
答案 0 :(得分:3)
我是在jQuery中完成的,因为你有每行的最后一个td
,因为我没有删除它们以显示它使用salary列工作。
这将获取每行的最后td
的值,在函数中,您可以对值进行任何操作。
// Run function for each tbody tr
$("#example tbody tr").each(function() {
// Within tr we find the last td child element and get content
alert($(this).find("td:last-child").html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td class="details-control"></td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td class="details-control"></td>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td class="details-control"></td>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
</tbody>
</table>
答案 1 :(得分:0)
我试过jquery。
代码:
$('#example tr td:last-child').each(function () {
console.log($(this).html());
});
请尝试使用此代码,希望此代码适用于您。
答案 2 :(得分:0)
获取匹配元素的集合:last&lt; td&gt;在所有行中:
var last_col = $("#example tbody tr td:last-child");
现在你可以得到&lt; td&gt;值作为数字数组(jQuery.map()或Array.prototype.map()):
var values = $.map(last_col.get(), function (td) {
return +$(td).text().replace(/[\$\,\s]/g, "") || 0;
});
奖励:如果你想要对数组中的每个值求和(或操作)(从左到右),你可以使用Array.prototype.reduce()
var total = values.reduce(function (prev, current) {
return prev + current;
}, 0 /*starts prev*/);
您可以在此处运行此代码: https://jsfiddle.net/4kw6yco7/
答案 3 :(得分:0)
说你想要一行的整个数据
var row = '<tr>' + '<td id="tduid">' + value['uid'] + '</td>' + '<td>' + value['name'] + '</td>' + '<td>' + value['address'] + '</td>' + '<td>' + tags + '</td>' + '<td>' + '<button class="deleteUser btn btn-danger" type="submit" id="del">Edit</button>' + '</td></tr>';
您需要此行的值。接下来,
$('#del').click(function(e) {
var tuid = $(this).closest('tr').find('#tduid').text();
alert(tuid);
});