简体   繁体   English

在Jquery ui模式对话框中显示foreach数据

[英]Display foreach data in Jquery ui modal dialog

I have a datatable with foreach data from database and want to add button to each row, which would open the Jquery ui modal, where user will be able to edit the data from row. 我有一个数据表,其中包含数据库中的foreach数据,并希望向每行添加按钮,这将打开Jquery ui模态,用户可以在其中编辑行中的数据。

How can I display this foreach data in dialog for every row? 如何在对话框的每一行中显示此foreach数据?

My table: 我的桌子:

<tbody>
        @if($invoices)
            @foreach($invoices as $invoice)
                <tr>
                    <td>{{$invoice->invoice_number}}</td>
                    <td>{{$invoice->status}}</td>
                    <td>{{$invoice->created_at}}</td>
                    <td>{{$invoice->supplier_name}}</td>
                    <td>{{$invoice->delivery_date}}</td>
                    <td>{{$invoice->comment}}</td>
                    <td><a class="opener"><i class="fa fa-fw fa-edit"></i></a></td>

                </tr>
            @endforeach
        @endif
        </tbody>

HTML modal window, where should this data be: HTML模态窗口,此数据应在哪里:

<div id="dialog" class="dialog">
    <input id="name">
</div>

Jquery ui dialog script: jQuery UI对话框脚本:

  $( ".opener" ).click(function() {
      $( "#dialog" ).dialog( "open" );
});

I guess what you are looking for is this (put it where your HTML is) - It checks whether the data array $invoices is empty or not - If not empty, it adds a <table> with each row <tr> where the elements <td> are in them with the $invoice information. 我猜您正在寻找的是(将其放在HTML位置)-检查数据数组$invoices是否为空-如果不为空,则在每行<tr>处添加一个<table> ,其中元素<td>包含$invoice信息。

<?php   
    if(!empty($invoices)) {
        echo("<table>");
        foreach($invoices as $invoice) {
            echo("<tr>".
                    "<td>".$invoice->invoice_number."</td>".
                    "<td>".$invoice->status."</td>".
                    "<td>".$invoice->created_at."</td>".
                    "<td>".$invoice->supplier_name."</td>".
                    "<td>".$invoice->delivery_date."</td>".
                    "<td>".$invoice->comment."</td>".
                    "<td><a class='opener'><i class='fa fa-fw fa-edit'>open dialog</i></a></td>".
                "</tr>");
        }
        echo("</tr></table>");
    }
?>

Your modal window and jQuery looks just fine. 您的模态窗口和jQuery看起来都很好。

To display the data for the current row, you will need two things: 要显示当前行的数据,您将需要两件事:

1) To get the values from the row. 1)从行中获取值。

2) To put those values in the dialog. 2)将这些值放在对话框中。

First set some classes to the td's you would want to show in the dialog, lets say they are the first 3. Also set an attribute which will store the name you would like to be in the input 首先将一些类设置为要在对话框中显示的td,让我们说它们是前3种。还要设置一个属性,该属性将存储要输入的名称

<tr>
    <td class="data" data-name="inv_number">{{$invoice->invoice_number}}</td>
    <td class="data" data-name="inv_status" >{{$invoice->status}}</td>
    <td class="data" data-name="created_at">{{$invoice->created_at}}</td>
    <td>{{$invoice->supplier_name}}</td>
    <td>{{$invoice->delivery_date}}</td>
    <td>{{$invoice->comment}}</td>
    <td><a class="opener"><i class="fa fa-fw fa-edit"></i></a></td>
 </tr>

Then on click, find the row and store the values: 然后单击,找到该行并存储值:

$( ".opener" ).click(function() {
  var values = {};
  $(this).closest('tr') //find the current row
    .find('.data') // find the td's
    .each(function(index, td) { // for each get and store the value
        var inputName = $(td).data('name'); //get the name
        var inputValue = $(td).text(); // get the text which the td holds
        values[inputName] = inputValue; // set the values
    });

  $( "#dialog" ).html(''); // clear the previously created inputs
  $( "#dialog" ).dialog( "open" );

Now create the inputs according to the stored values: 现在根据存储的值创建输入:

  $.each(values, function(name, value) {
      $( "#dialog" ).append('<input name="'+name+'" value="'+value+'" />');
  });


});

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

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