如何将Foreach中的数据保存在表中并在弹出窗口(Jquery)中显示相同的数据


How can I save data from Foreach in table and display the same data in popover (Jquery)

>我有一个问题..正在使用编码点火器并在表中有一个foreach循环,这是正常的事情..但我想将数据保存在表中并将其显示在popover上.. ?? 我该怎么做?

<?php if(isset($data)) { ?>
<?php foreach($data as $key) {?>
<tr>
  <th scope="row">
    <?php echo $key->id ?></th>
  <th scope="row" name="nasip">
    <?php echo $key->nasname ?></th>
  <th scope="row">
    <?php echo $key->shortname ?></th>
  <th scope="row">
    <?php echo $key->type ?></th>
  <th scope="row">
    <?php echo $key->ports ?></th>
  <th scope="row">
    <?php echo $key->secret ?></th>
  <th scope="row">
    <?php echo $key->server ?></th>
  <th scope="row">
    <?php echo $key->community ?></th>
  <th scope="row">
    <?php echo $key->description ?></th>
  <th scope="row"><a data-toggle="modal" data-target=".bs-example-modal-sm" id="delete" class="btn btn-danger active" role="button">Delete</a>
  </th>
  <th scope="row"><a data-toggle="modal" data-target="#exampleModal" href="" class="btn btn-success active" id="edit" role="button">Edit Nas</a>
  </th>
</tr>
<?php }?>
<?php }else{ echo "<div>NO DATA !</div>"; } ?>
<!-- Section Edit Page -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="exampleModalLabel">Update Nas Info</h4>
      </div>
      <div class="modal-body">
        <form id="form_id">
          <div class="form-group">
            <label for="recipient-name" class="control-label">NAS IP</label>
            <input type="text" class="form-control" id="nasname" value="<?php echo $key->nasname ?>">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Shortname:</label>
            <input type="text" class="form-control" id="shortname" value="<?php echo $key->shortname ?>">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Type:</label>
            <input type="text" class="form-control" id="type" value="<?php echo $key->type ?>">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Ports:</label>
            <input type="text" class="form-control" id="ports" value="<?php echo $key->ports ?>">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Secret:</label>
            <input type="text" class="form-control" id="secret" value="<?php echo $key->secret ?>">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Server:</label>
            <input type="text" class="form-control" id="server" value="<?php echo $key->server ?>">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Community:</label>
            <input type="text" class="form-control" id="community" value="<?php echo $key->community ?>">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Description:</label>
            <input type="text" class="form-control" id="description" value="<?php echo $key->description ?>">
          </div>
        </form>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Send message</button>
        </div>
      </div>

如果我使用这种方式,我无法进行更新或删除等...那么我该如何使用Jquery解决此问题呢?

非常感谢..

如果您的查询返回大约 20 或 30 个结果,您可以在没有 JQuery 的情况下执行的操作:为每个结果创建一个模式,然后显示用户选择的模式。如果您有很多结果,具体取决于您的服务器和客户端性能,则此解决方案不是最佳方法,因为执行时间和超时风险很高:

<?php 
$table = "<table>";
$modals = "";
if(isset($data)) {
    foreach($data as $key) {
        $table .=  '
<tr>
  <th scope="row">
    '.$key->id.'</th>
  <th scope="row" name="nasip">
    '.$key->nasname.'</th>
  <th scope="row">
    '.$key->shortname.'</th>
  <th scope="row">
    '.$key->type.'</th>
  <th scope="row">
    '.$key->ports.'</th>
  <th scope="row">
    '.$key->secret.'</th>
  <th scope="row">
    '.$key->server.'</th>
  <th scope="row">
    '.$key->community.'</th>
  <th scope="row">
    '.$key->description.'</th>
  <th scope="row"><a data-toggle="modal" data-target=".bs-example-modal-sm" id="delete" class="btn btn-danger active" role="button">Delete</a>
  </th>
  <th scope="row"><a data-toggle="modal" data-target="#edit'.$key->id.'" href="" class="btn btn-success active" id="edit" role="button">Edit Nas</a>
  </th>
</tr>';
        $modals .= '
<div class="modal fade" id="#edit'.$key->id.'" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="exampleModalLabel">Update Nas Info</h4>
      </div>
      <div class="modal-body">
        <form id="form_id">
          <div class="form-group">
            <label for="recipient-name" class="control-label">NAS IP</label>
            <input type="text" class="form-control" id="nasname" value="'.$key->nasname.'">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Shortname:</label>
            <input type="text" class="form-control" id="shortname" value="'.$key->shortname.'">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Type:</label>
            <input type="text" class="form-control" id="type" value="'.$key->type.'">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Ports:</label>
            <input type="text" class="form-control" id="ports" value="'.$key->ports.'">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Secret:</label>
            <input type="text" class="form-control" id="secret" value="'.$key->secret.'">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Server:</label>
            <input type="text" class="form-control" id="server" value="'.$key->server.'">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Community:</label>
            <input type="text" class="form-control" id="community" value="'.$key->community.'">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Description:</label>
            <input type="text" class="form-control" id="description" value="'.$key->description.'">
          </div>
        </form>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Send message</button>
        </div>
      </div>
    </div>
  </div>
</div>
        ';
    }
    $table .= "</table>";
}
else {
    echo "<div>NO DATA !</div>";
}
?>