Datatables get selected cell value from row select in PHP

huangapple go评论131阅读模式
英文:

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: &#39;Get selected data&#39;,
  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 = $(&#39;#example&#39;).DataTable( {
dom: &#39;QBfrtip&#39;,
select: true,
buttons: [
{
text: &#39;Select all&#39;,
action: function () {
table.rows( {page:&#39;current&#39;,search: &#39;applied&#39;} ).select();
}
},
{
text: &#39;Select none&#39;,
action: function () {
table.rows().deselect();
}
},
{
text: &#39;Get selected data&#39;,
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: &quot;=&quot;,
},
targets: [1]
}]
} );
} );

<!-- language: lang-html -->

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Demo&lt;/title&gt;
&lt;link href=&quot;https://cdn.datatables.net/1.13.3/css/jquery.dataTables.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;link href=&quot;https://cdn.datatables.net/buttons/2.3.5/css/buttons.dataTables.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;link href=&quot;https://cdn.datatables.net/searchbuilder/1.4.0/css/searchBuilder.dataTables.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;link href=&quot;https://cdn.datatables.net/select/1.6.1/css/select.dataTables.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.datatables.net/1.13.3/js/jquery.dataTables.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.datatables.net/buttons/2.3.5/js/dataTables.buttons.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.datatables.net/searchbuilder/1.4.0/js/dataTables.searchBuilder.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.datatables.net/select/1.6.1/js/dataTables.select.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://datatables.net/media/css/site-examples.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div style=&quot;margin: 20px;&quot;&gt;
&lt;table id=&quot;example&quot; class=&quot;display dataTable cell-border&quot; style=&quot;width:100%&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Tiger Nixon&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Garrett Winters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Ashton Cox&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月12日 19:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定