简体   繁体   English

如何使用jquery遍历php回显的表行并选择满足属性的特定行

[英]How to use jquery to loop through table rows echoed by php and select a specific row where a property is meet

I have a table dynamically created from a Mysql database. 我有一个从Mysql数据库动态创建的表。 I am trying to create a modal dialog whereby if a user clicks on the view button a pop-up is displayed showing the values of that specific row. 我正在尝试创建一个模式对话框,如果用户单击查看按钮,则会显示一个弹出窗口,显示该特定行的值。 How do I loop through the table and select a specific row using jquery? 如何遍历表并使用jquery选择特定行? I am using a Javascript library called bootbox.js I am able to display the pop-up however I cant get it to display the relevant row, it instead displays only the first row. 我正在使用一个名为bootbox.js的Javascript库,我能够显示弹出窗口,但无法显示相关行,而是仅显示第一行。 The relevant PHP and the Javascript code I tried is shown below 我尝试过的相关PHP和Javascript代码如下所示

echo "<table class='table table-hover table-responsive table-bordered'>";
    echo "<tr>";
        echo "<th>Payment Supplier</th>";
        echo "<th>Payment Reference</th>";
        echo "<th>Payment Cost rating</th>";
        echo "<th>Payment Amount</th>";
        echo "<th>Actions</th>";
    echo "</tr>";

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){

        extract($row);

        echo "<tr>";

            echo "<td>{$payment_supplier}</td>";
            echo "<td>{$payment_ref}</td>";
            echo "<td>{$payment_cost_rating}</td>";
            echo "<td>{$payment_amount}</td>";
            ?>
            <div id="dom-target" style="display: none;">
                <?php 
                $output = $payment_supplier; //Again, do some operation, get the output.
                echo htmlspecialchars($output); /* You have to escape because the result
                                       will not be valid HTML otherwise. */
                ?>

                </div>
                <?php
            echo "<td>"; 

                echo "<a view-id='{$payment_id}' class='btn btn-default view-object'>View</a>";
                echo "<a href='lib/Local/update_payment.php?id={$payment_id}' class='btn btn-default left-margin'>Edit</a>";
                echo "<a delete-id='{$payment_id}' class='btn btn-default delete-object'>Delete</a></div>";
            echo "</td>";

        echo "</tr>";

        $x++;
    }

echo "</table>";

The Javascript code is Javascript代码是

<script>
$(document).on('click', '.view-object', function(e) {

        var div = document.getElementById("dom-target");
        var myData = div.textContent;

        bootbox.alert(myData, function() {
            console.log("Alert Callback");
        });
    });
</script>

What am I missing here or is there a better way to achieve this? 我在这里想念的还是有更好的方法来实现这一目标?

As you had told , your code displays only the first row. 如您所知,您的代码仅显示第一行。

This is because you have not set different unique id to each row div to show on click of view. 这是因为您没有为单击视图时显示的每个行div设置不同的唯一ID。

Try using the code as : 尝试将代码用作:

    echo "<table class='table table-hover table-responsive table-bordered'>";
            echo "<tr>";
                echo "<th>Payment Supplier</th>";
                echo "<th>Payment Reference</th>";
                echo "<th>Payment Cost rating</th>";
                echo "<th>Payment Amount</th>";
                echo "<th>Actions</th>";
            echo "</tr>";

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){

                extract($row);

                echo "<tr>";

                    echo "<td>{$payment_supplier}</td>";
                    echo "<td>{$payment_ref}</td>";
                    echo "<td>{$payment_cost_rating}</td>";
                    echo "<td>{$payment_amount}</td>";

                    //SET THE UNIQUE ID OF DIV TO SHOW ON CLICK OF VIEW//
                    ?>
                    <div id="dom-target-<?php echo $payment_id;?>" style="display: none;">
                        <?php 
                        $output = $payment_supplier; //Again, do some operation, get the output.
                        echo htmlspecialchars($output); /* You have to escape because the result
                                               will not be valid HTML otherwise. */
                        ?>

                        </div>
                        <?php
                    echo "<td>"; 

                        echo "<a view-id='{$payment_id}' class='btn btn-default view-object'>View</a>";
                        echo "<a href='lib/Local/update_payment.php?id={$payment_id}' class='btn btn-default left-margin'>Edit</a>";
                        echo "<a delete-id='{$payment_id}' class='btn btn-default delete-object'>Delete</a></div>";
                    echo "</td>";

                echo "</tr>";

                $x++;
            }

        echo "</table>";

JS : JS:

        <script>
        $(document).on('click', '.view-object', function(e) {

                selectedId=$(this).attr('view-id');         //GET SELECTED ROW ID FROM ANCHOR TAG

                var div = document.getElementById("dom-target-"+selectedId);        //CALL THE SELECTED ROW DIV TO POP UP
                var myData = div.textContent;

                bootbox.alert(myData, function() {
                    console.log("Alert Callback");
                });
            });
        </script>

This may helps you. 这可能对您有帮助。

try this... 尝试这个...

<script>
$(document).on('click', '.view-object', function(e) {

        bootbox.alert($(this).val(), function() {
        console.log("Alert Callback");
    });
});
</script>

There are two main problems with your script: 您的脚本有两个主要问题:

  1. You are recycling the dom-target ID. 您正在回收dom-target ID。 Remember that IDs have to be unique in your HTML document. 请记住,ID在HTML文档中必须唯一 You can change it from ID to a class instead. 您可以将其从ID更改为类。
  2. <div> is not a valid child of <tr> . <div>不是<tr>的有效子代。 Browsers will attempt to fix this, which might involve the element being removed from the table, or sequestered in a <td> . 浏览器将尝试解决此问题,这可能涉及从表中删除元素或将其隔离在<td>
  3. Your function lacks context — when .view-object is clicked on, you need to reference to the current element where the event originated from. 您的函数缺少上下文-单击.view-object时,您需要引用事件起源的当前元素。 Using $(this) will point you in the right direction. 使用$(this)将为您指明正确的方向。

I suggest that you store the value in dom-output as a HTML5 data- attribute associated with the specific row, ie, instead of: 我建议您将值作为与特定行关联的HTML5 data-属性存储在dom-output中,而不是:

    echo "<tr>";

        echo "<td>{$payment_supplier}</td>";
        echo "<td>{$payment_ref}</td>";
        echo "<td>{$payment_cost_rating}</td>";
        echo "<td>{$payment_amount}</td>";
        ?>
        <div id="dom-target" style="display: none;">
            <?php 
            $output = $payment_supplier; //Again, do some operation, get the output.
            echo htmlspecialchars($output); /* You have to escape because the result
                                   will not be valid HTML otherwise. */
            ?>

            </div>

use: 采用:

    echo "<tr data-domtarget='".htmlspecialchars($payment_supplier)."'>";

        echo "<td>{$payment_supplier}</td>";
        echo "<td>{$payment_ref}</td>";
        echo "<td>{$payment_cost_rating}</td>";
        echo "<td>{$payment_amount}</td>";
        ?>

You can use the following script: 您可以使用以下脚本:

$(document).on('click', '.view-object', function() {
    var domTarget = $(this).closest('tr').data('domtarget');
    bootbox.alert(domTarget, function() {
        console.log("Alert Callback");
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM