英文:
Datatables get selected cell value from row select in PHP
问题
I apologize, but it seems like you'd like to exclude the code part from the translation. Here's the translated text without the code:
如何通过复选框获取单个或多个选择的行中的特定单元格的值,并将该行值传递到其他PHP表单以进行查询。基本上,我有一个具有选择扩展和搜索构建器的数据表。我能够实现多项选择,但似乎无法使获取选定的行起作用。
表格
在这里放置了HTML表格的内容,其中包括表头和数据行。
JavaScript
在这里放置了JavaScript代码,用于初始化数据表和处理行选择的操作。
我已尝试使用table.rows({selected:true})
,但无法弄清楚如何获取查询所需的特定或一组值(app_id)。
此外,如何在标题标签上的按钮单击时将app_id值传递到下一个PHP页面?
英文:
How do you get the value of certain a cell in a single or multiple selected row via checkboxes and pass that row value to other php form for query. Basically I have a datatable with select extension and searchbuilder. I was able to make the multiple item select but can't seem to make the get selected row to work.
Table
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>
<form method="post" action="query.php">
<button type="submit" name="send"></button>
</form>
</th>
<th>ID</th>
<th>Name</th>
</thead>
<tbody>
<?php
$sql = mysqli_query($db,"query inside here");
while ($row=mysqli_fetch_array($sql)){ ?>
<tr>
<td></td>
<td><?php echo $row['app_id'] ?></td>
<td><?php echo $row['app_nname']</td>
<?php }?>
</tbody>
<tfooter>
<tr>
<th></th>
<th>ID</th>
<th>Name</th>
</tr>
</tfooter>
</table>
Javascript
<script>
$(document).ready(function() {
var table = $('#example').DataTable( {
dom: 'QBfrtip',
select: true,
buttons: [
{
text: 'Select all',
action: function () {
table.rows({
page:'current',search: 'applied'} ).select();
}
},
{
text: 'Select none',
action: function () {
table.rows().deselect();
}
}
],
columnDefs: [{
searchBuilder: {
defaultCondition: "=",
},
targets: [1]
}],
} );
} );
</script>
I have tried using the table.rows({selected:true})
but can't figure out how to get the specific or group of values necessary for query (app_id).
In addition, how do I make the app_id value be passed to the next php page via button click in the header tag?
答案1
得分: 0
"如何获取单个或多个选定行中某个单元格的值"
一个建议:既然您已经在使用DataTables按钮,为什么不创建一个DataTables按钮来处理您想要捕获的选定ID呢。
对于这个问题,我忽略了问题表中的第一列(复选框列),因为对于我的演示来说,它是不需要的。
您可以将它添加回到您的演示中 - 如果您愿意,它并不会改变总体方法。
我添加了一个额外的按钮:
{
text: '获取选定数据',
action: function () {
var rows = table.rows( { selected: true } ).data().toArray();
console.log( rows ); // 选定行的数组(每行都是数据数组)
var ids = rows.map(function(x) {
return x[0];
});
console.log( ids ); // 选定的ID数组
}
}
这个逻辑被分解成小步骤,仅供演示目的 - 但您可以简化它。最终的console.log( ids )
将记录来自列0(ID列)的值数组。
关键步骤是使用这个:
rows( { selected: true } )
来访问选定的行。
一旦您获得了需要POST到服务器的值数组,那就是一个单独的步骤。这个答案只处理从DataTables获取选定数据的部分。
如果您仍然喜欢使用复选框(这是一个完全合理的方法),那么您可能还想保留您的标准HTML按钮。
完整的演示:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>演示</title>
<link href="https://cdn.datatables.net/1.13.3/css/jquery.dataTables.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/buttons/2.3.5/css/buttons.dataTables.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/searchbuilder/1.4.0/css/searchBuilder.dataTables.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/select/1.6.1/css/select.dataTables.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.3/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.5/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/searchbuilder/1.4.0/js/dataTables.searchBuilder.min.js"></script>
<script src="https://cdn.datatables.net/select/1.6.1/js/dataTables.select.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">
</head>
<body>
<div style="margin: 20px;">
<table id="example" class="display dataTable cell-border" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Tiger Nixon</td>
</tr>
<tr>
<td>2</td>
<td>Garrett Winters</td>
</tr>
<tr>
<td>3</td>
<td>Ashton Cox</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
英文:
"How do you get the value of certain a cell in a single or multiple selected row"
One suggestion: Given you are using DataTables buttons already, why not create a DataTable button to handle the capturing of the selected IDs you want.
For this, I ignored the first column (the checkboxes column) in your question's table since it is not needed for my demo.
You can add that back into your demo - it does not change the overall approach, if you prefer.
I add one more button:
{
text: 'Get selected data',
action: function () {
var rows = table.rows( { selected: true } ).data().toArray();
console.log( rows ); // array of selected rows (each row is an array of data)
var ids = rows.map(function(x) {
return x[0];
});
console.log( ids ); // array of selected IDs
}
}
The logic is broken down into small steps, just for demo purposes - but you could streamline that. The final console.log( ids )
will log an array of values from column 0 (the ID column).
The key step is to use this:
rows( { selected: true } )
to access the selected rows.
Once you have the array of values you need to POST to your server, then that is a separate step. This answer only deals with getting the selected data from the DataTable.
If you still prefer to use checkboxes (a totally reasonable approach) then you may also want to keep your standard HTML button.
A full demo:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
var table = $('#example').DataTable( {
dom: 'QBfrtip',
select: true,
buttons: [
{
text: 'Select all',
action: function () {
table.rows( {page:'current',search: 'applied'} ).select();
}
},
{
text: 'Select none',
action: function () {
table.rows().deselect();
}
},
{
text: 'Get selected data',
action: function () {
var rows = table.rows( { selected: true } ).data().toArray();
//console.log( rows ); // array of selected rows (each row is an array of data)
var ids = rows.map(function(x) {
return x[0];
});
console.log( ids ); // array of selected IDs
}
}
],
columnDefs: [{
searchBuilder: {
defaultCondition: "=",
},
targets: [1]
}]
} );
} );
<!-- language: lang-html -->
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
<link href="https://cdn.datatables.net/1.13.3/css/jquery.dataTables.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/buttons/2.3.5/css/buttons.dataTables.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/searchbuilder/1.4.0/css/searchBuilder.dataTables.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/select/1.6.1/css/select.dataTables.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.3/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.5/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/searchbuilder/1.4.0/js/dataTables.searchBuilder.min.js"></script>
<script src="https://cdn.datatables.net/select/1.6.1/js/dataTables.select.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">
</head>
<body>
<div style="margin: 20px;">
<table id="example" class="display dataTable cell-border" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Tiger Nixon</td>
</tr>
<tr>
<td>2</td>
<td>Garrett Winters</td>
</tr>
<tr>
<td>3</td>
<td>Ashton Cox</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论