如何将锚标记的data-id值传递给Code-Igniter控制器函数并在data-modal中显示结果?

时间:2017-12-28 09:02:59

标签: javascript jquery html ajax codeigniter-3

我正在开发Code-Igniter项目,我正在尝试显示有关锚标记的点击事件的数据。为此,我使用data-indexAJAX值传递给ControllerModel。之后我得到了结果,但结果没有显示在modal-box

这是我的代码:

查看:

<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代码:

<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>

我将值放在控制器中的变量中。

控制器代码:

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');
    }

型号代码:

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;
        }
   }

如果我将硬编码值传递给过程(我正在使用存储过程),我会得到结果。

以下是我的模态视图:

<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>

编辑: 这是我的输出:

enter image description here 我没有得到,我犯了错误。欢迎任何形式的帮助,提前致谢。

1 个答案:

答案 0 :(得分:1)

由于您是通过AJAX发送响应,因此您需要回显输出。否则ajax会得到回应。

另外,根据您发送响应的方式,在ajax中添加dataType。 例如:发送html响应添加 dataType:'html',如果发送json响应添加 dataType:'json'

JS CODE:

<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代码:

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