如何将请求ID值传递给模式?

时间:2019-03-04 08:36:04

标签: javascript php codeigniter

当我单击“编辑”按钮时,模态已打开 但是在隐藏字段中,否,我无法接收RequestId。 请帮我如何通过动态请求到模态形式?

这将在Codeigniter框架中使用。

如果可以的话,请重写代码,我将为您提供最大的帮助。

<table width="100%" class="table table-striped">
    <thead class="thead-dark">
        <tr>
            <th scope="col" align="left">#</th>
            <th scope="col">Request Id</th>
            <th scope="col">Agent Id</th>
            <th scope="col">Type</th>
            <th scope="col">Value</th>
            <th scope="col">Comment</th>
            <th scope="col"> Request Date & Time</th>
            <th scope="col">Status</th>
            <th scope="col">Action</th>
        </tr>
    </thead>
    <tbody>
        <?php 
          $sn=1;
          foreach ($result as $row) { 
              //print_r($row);die();
        ?>
        <tr>
            <td align="left"><?php echo $sn ?></td>
            <td><?php echo $row['rid'];?></td>
            <td><?php echo $row['aid'];?></td>
            <td><?php echo $row['type'];?></td>
            <td><?php echo $row['value'];?></td>
            <td><?php echo $row['comment'];?></td>
            <td><?php echo $row['request_time'];?></td>
            <td><button class="btn btn-danger btn-sm">Open</button></td>
            <td>
              <a data-toggle="modal" href="<?php base_url()?>#myModal" class="btn btn-warning btn-sm">Edit</a>
            </td>
        </tr>
        <?php $sn++;}?>
    </tbody>
</table>

5 个答案:

答案 0 :(得分:0)

您可以添加

data-request-id="<?php echo $row['rid']?>"

打开模式的按钮,然后使用jquery / javascript用ajax获取数据

var requestId = $('.modalBtn').attr("data-request-id");

答案 1 :(得分:0)

这是一个使用HTML data-attributes和jQuery将ID传递给模式的示例。

<table width="100%" class="table table-striped">
    <thead class="thead-dark">
        <tr>
            <th scope="col" align="left">#</th>
            <th scope="col">Request Id</th>
            <th scope="col">Agent Id</th>
            <th scope="col">Type</th>
            <th scope="col">Value</th>
            <th scope="col">Comment</th>
            <th scope="col"> Request Date & Time</th>
            <th scope="col">Status</th>
            <th scope="col">Action</th>

        </tr>
    </thead>
    <tbody>
        <?php 
        $sn=1;
        foreach ($result as $row) { 
            //print_r($row);die();
        ?>
        <tr>
            <td align="left"><?php echo $sn ?></td>
            <td><?php echo $row['rid'];?></td>
            <td><?php echo $row['aid'];?></td>
            <td><?php echo $row['type'];?></td>
            <td><?php echo $row['value'];?></td>
            <td><?php echo $row['comment'];?></td>
            <td><?php echo $row['request_time'];?></td>
            <td><button class="btn btn-danger btn-sm">Open</button></td>
            <td>
                <a href="#myModal" data-toggle="modal" data-id="<?php echo $row['rid'];?>" class="btn btn-warning btn-sm">Edit</a>
            </td>
        </tr>
        <?php
            $sn++;
        }?>
    </tbody>
</table>

<script>
    $('#myModal').on('show.bs.modal', function (e) {
        var $btn = e.relatedTarget,
        rId = $btn.data('id');

        // "rId" would be the value from the html data-attribute.
        console.log(rId);
    });
</script>

答案 2 :(得分:0)

<table width="100%" class="table table-striped">
    <thead class="thead-dark">
        <tr>
            <th scope="col" align="left">#</th>
            <th scope="col">Request Id</th>
            <th scope="col">Agent Id</th>
            <th scope="col">Type</th>
            <th scope="col">Value</th>
            <th scope="col">Comment</th>
            <th scope="col"> Request Date & Time</th>
            <th scope="col">Status</th>
            <th scope="col">Action</th>
        </tr>
    </thead>
    <tbody>
        <?php 
          $sn=1;
          foreach ($result as $row) { 
              //print_r($row);die();
        ?>
        <tr>
            <td align="left"><?php echo $sn ?></td>
            <td><?php echo $row['rid'];?></td>
            <td><?php echo $row['aid'];?></td>
            <td><?php echo $row['type'];?></td>
            <td><?php echo $row['value'];?></td>
            <td><?php echo $row['comment'];?></td>
            <td><?php echo $row['request_time'];?></td>
            <td><button class="btn btn-danger btn-sm">Open</button></td>
            <td>
              <a onclick="load_item('<?php echo $row['rid'];?>');" class="btn btn-warning btn-sm">Edit</a>
            </td>
        </tr>
        <?php $sn++;}?>
    </tbody>
</table>

您必须插入的Java代码

 function load_item(rid){
/**
Write your ajax/javascript code here
*/
$("#myModal").modal("show");
 }

答案 3 :(得分:0)

WriteLines()标记中删除Console并根据ID进行Debug.WriteLines()编辑

System.Diagnostics

JS

data-toggle="modal"

答案 4 :(得分:0)

如果我是对的,您想在单击编辑按钮后显示一个模式。

<a href="javascript:void(0)" onclick="get_modal_details('<?php echo $row['rid'];?>')" class="btn btn-warning btn-sm">Edit</a>

我希望$row['rid']是您的唯一ID,这是html模式代码

<div class="modal" id="detail_modal_pop" style="overflow: scroll;"> 
      <div class="modal-dialog modal-lg"> 
        <div class="modal-content">
          <div class="modal-header" >
            <button type="button" class="close close_c_modal" >&times;</button>
            <h4 class="modal-title c_modal">Details</h4>
          </div>
          <div class="modal-body"> 
             <h6 id="load_wait" class="text-center">Please wait...</h6>
             <div id="detail_modal_pop_result" style="display:none;"></div> 
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default close_c_modal" >Close</button>
          </div>
        </div> 
      </div>
 </div>

要在点击“编辑”按钮时显示模式,请使用以下脚本

<script>
function get_modal_details(rid) 
{  
    $('#detail_modal_pop').fadeIn(200);  
    $("#detail_modal_pop_result").hide();
    $("#load_wait").show(); 
    jQuery.ajax({
    url: "<?php echo base_url() ?>some_controller/controller_method", 
    data: { rid: rid },
    type: "POST",
    success:function(data){
        $("#load_wait").hide(); 
        $("#detail_modal_pop_result").show();
        $("#detail_modal_pop_result").html(data); 
    },
    error:function (){}
    }); 
} 
$(".close_c_modal").on("click", function() 
{
    $('#detail_modal_pop').fadeOut(200); 
    $("#detail_modal_pop_result").hide(300);
    $('#detail_modal_pop_result').html('');
    $("#load_wait").show(300);
}); 
 </script>

这是PHP代码

<?php
//"Some_controller.php" Controller

public function controller_method()
{
   if(isset($_POST['rid']))
   {
      $rid = (int)$_POST['rid']; 

      // $get_info = $this->Model->get_info($rid);  
      // Write modal content here...   
     
   }
}
?>

希望这会对您有所帮助。