简体   繁体   English

如何将锚标签的data-id值传递给Code-Igniter控制器功能并以数据模式显示结果?

[英]How to pass data-id value of anchor tag to Code-Igniter controller function and show result in data-modal?

I'm working on Code-Igniter project and I'm trying to show data on click event of anchor tag. 我正在从事Code-Igniter项目,并尝试显示锚标记的click事件数据。 For that I'm passing data-index value using AJAX to Controller and to Model . 为此,我将使用AJAXdata-index值传递给ControllerModel After that I'm getting result, but the result is not shown in modal-box . 之后,我得到结果,但结果未显示在modal-box

Here is my Code: 这是我的代码:

View: 视图:

<a id="propDetail" name="propDetail" data-toggle="modal" 
    data-index="<?= $property->property_id; ?>" 
    title="Details of <?= $property->property_code; ?>" 
    data-target="#myModal" class="open-Details">
    <?= $property->property_code; ?>
</a>

AJAX Code: AJAX代码:

<script type="text/javascript">
$('.open-Details').click(function() {
    $.ajax({
      url : '<?= base_url(); ?>property',
      data: {
        propDetail: $(this).data("index")
      },
      type: "POST",
      success: function(data) {
        console.log('success');
        console.log(data);
      }
    });
  });
</script>

I'm putting the value in variable in controller. 我将值放在控制器中的变量中。

Controller Code: 控制器代码:

public function index()
    {   

        $user = $this->ion_auth->user()->row();
        $data['username'] = $user->username;
        $data['user_id'] = $user->id;

        $id=$this->input->post('property_id');
        $area=$this->input->post('area_id');
        $cluster=$this->input->post('cluster_id');
        if( $area =='') {
            $area =0;
        }
        if($cluster == ''){
            $cluster=0;
        }

        $propDetail = $this->input->post('propDetail');

        $data['areas'] = $this->p->area();
        $data['clusters'] = $this->p->cluster();
        //$data['clusters'] = $this->p->clusterAll($area);
        $data['title'] = 'Property List';

        $data['properties'] = $this->p->getPropertyByAreaCluster($area, $cluster);
        $data['propDetails'] = $this->p->defectsView($propDetail);

        $this->load->view('template/header', $data);
        $this->load->view('Property/property_view', $data);
        $this->load->view('template/footer');
    }

Model Code: 型号代码:

public function defectsView($propDetail)
   {
        $query = $this->db->query("call modalView(?)", $propDetail);
        if ($query) {
            $data = $query->result();
            $query->next_result(); 
            $query->free_result();
            return $data;
        } else {
            return false;
        }
   }

I'm getting result if I'm passing hard coded value to the procedure (I'm using Stored Procedure). 如果将硬编码值传递给过程(我正在使用存储过程),则会得到结果。

Here is my Modal view: 这是我的模态视图:

<div class="modal fade displaycontent" id="myModal">
  <div class="modal-dialog" id="propertyDetails" role="document">
    <div class="modal-content displaycontent">
      <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">Property Datails of</h4>
      </div>
      <div class="modal-body">
        <table class="table table-striped ">
          <tbody>
            <?php if($propDetails) { 
              foreach ($propDetails as $propDetail) { ?> 
              <tr>
                <td>Property Code:</td>
                <td><?= $propDetail->property_code; ?></td>
              </tr>
              <tr>
                <td>Defect Added DAte :</td>
                <td><?= $propDetail->property_added_date; ?></td>
              </tr>
              <tr>
                <td>Room ASYS No.:</td>
                <td><?= $propDetail->property_ASYS_no;?></td>
              </tr> 
              <tr>
                <td>Address:</td>
                <td><?= $propDetail->property_address_1;?></td>
              </tr> 
              <tr>
                <td>Room Count:</td>
                <td><?= $propDetail->rooms;?></td>
              </tr> 
              <tr>
                <td>Defect Count:</td>
                <td><?= $propDetail->defects;?></td>
              </tr> 
            <?php } } ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

Edit: here is my output: 编辑: 这是我的输出:

在此处输入图片说明 I'm not getting, where I'm made a mistake. 我不明白,我犯了一个错误。 Any kind of help is welcome, Thanks in advance. 欢迎任何帮助,在此先感谢。

Since you are sending response via AJAX, you need to echo the output. 由于您是通过AJAX发送响应的,因此您需要显输出。 Else ajax will get blank the response. 其他ajax将使响应空白。

Also depending on how you send the response add dataType in ajax. 还取决于您发送响应的方式,在ajax中添加dataType。 Eg: is sending html response add dataType:'html' , if sending json response add dataType : 'json' 例如:正在发送html响应,添加dataType:'html' ,如果发送json响应,则添加dataType:'json'

JS CODE : JS代码:

<script type="text/javascript">
$('.open-Details').click(function() {
    $.ajax({
      url : '<?= base_url(); ?>property',
      data: {
        propDetail: $(this).data("index")
      },
      dataType :"html"
      type: "POST",
      success: function(data) {
        console.log('success');
        console.log(data);
      }
    });
  });
</script>

PHP CODE : PHP代码:

echo $this->load->view('Property/property_view', $data,true);

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

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