如何通过单击codeigniter中视图表中的行来填充我的引导模式中的字段

时间:2014-02-08 09:14:07

标签: javascript jquery codeigniter twitter-bootstrap-3

我有这个bootstrap3模式,当我单击表格中的某一行并显示一个学生的详细信息时,可以调用该模态。但问题是细节没有出现在我通过ajax放置值的字段中。顺便说一句,我使用Codeigniter作为我的PHP框架。

我现在正在寻找解决方案,但仍无法找到解决问题的正确答案......

这是我的控制器

public function getStudent(){
        $data = array();
        $id = $this->input->post('stud_id');
        $this->load->model('studentModel');
        $data = $this->studentModel->getStudent($id);

        echo $data; 

}

模型

public function getStudent($id){    
    $query = array();
    $this->db->select('*');
    $this->db->from('stud_bio');
    $this->db->where('stud_id', $id);
    $query = $this->db->get();

    return $query->result();

}

查看

<div class="container">
<div class="row">
  <div class="table table-responsive">
    <table class="table table-hover table-striped table-condensed table-bordered">
      <thead class="header">
        <tr>           
          <th>Name</th>
          <th>Gender</th>
          <th>Marital Status</th>
          <th>Name of Spouse</th>
          <th>Number of Children</th>
          <th>Date of Birth</th>
          <th>Contact Number</th>
          <th>Email</th>             
        </tr>
      </thead>

      <tbody>

        <?php foreach($data as $row){ ?>
          <tr data-toggle="modal"  href="#editStudent" data-id="<?php echo $row->stud_id?>" class="editStudent">

          <td><?php echo $row->name?></td>
          <td><?php if($row->gender == 1){echo "Male";}else{echo "Female";} ?></td>
          <td><?php if($row->marital_status == 1){echo "Single";}else{echo "Married";}?></td>
          <td><?php echo $row->spouse_name ?></td>
          <td><?php echo $row->no_of_children ?></td>
          <td><?php echo $row->DOB ?></td>
          <td><?php echo $row->contactNo ?></td>
          <td><?php echo $row->email ?></td>
        <?php }?>
      </tbody>
    </table>
  </div>
</div>

模态

<div class="container">
<div class="row">
  <div class="modal fade" id="editStudent" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">
        <?=form_open(base_url('admin/studentController/editStudent'))?>
        <div class="form-horizontal">
          <div class="modal-header"><!--HEADER-->
            <button class="btn btn-primary close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4>Edit Student</h4> 
          </div><!--end of HEADER-->
          <div class="modal-body"><!--BODY-->
            <!--Name-------------------------------------------->
            <div class="form-group">
                <label for="edit_fullname" class="col-sm-2 control-label">Fullname</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" id="edit_fullname" name="edit_fullname"  placeholder="Name">
                </div>
                <label class="radio-inline has-error">
                  <?php echo form_error('edit_fullname','*<span3 class="help-inline">','</span3>'); ?>
                </label>
            </div><!--end of NAME -->
            <!--Gender------------------------------------------>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Gender</label>
                    <div class="col-sm-4">
                        <label for="edit_male" class="radio-inline">
                            <input type="radio" name="edit_genderRadio" value="1" id="edit_male"/>Male
                        </label>
                        <label for="edit_female" class="radio-inline">
                            <input type="radio" name="edit_genderRadio" value="2" id="edit_female"/>Female
                        </label>
                    </div>
                    <label class="radio-inline has-error">
                        <?php echo form_error('edit_genderRadio','*<span3 class="help-inline">','</span3>'); ?>
                    </label>
                </div>
                <!--Civil Status----------------------------------->
                <div class="form-group">
                    <label class="col-sm-2 control-label">Civil Status</label>
                    <div class="col-sm-4">
                        <label for="edit_S" class="radio-inline">
                            <input type="radio" name="edit_civilRadio" value="1"<?=set_radio('civilRadio', '1')?> id="edit_S"/>Single
                        </label>
                        <label for="edit_M" class="radio-inline">
                            <input type="radio" name="edit_civilRadio" value="2"<?=set_radio('civilRadio', '2')?> id="edit_M"/>Married
                        </label>
                    </div>
                    <label class="radio-inline has-error">
                        <?php echo form_error('edit_civilRadio','*<span3 class="help-inline">','</span3>'); ?>
                    </label>
                </div>
                <!--If Married Spouse name--------------------------------->
                <div class="form-group">
                    <label for="edit_spouseName" class="col-sm-2 control-label">Spouse Name</label>
                    <div class="col-sm-4">
                       <input type="text" name="edit_spouseName" id="edit_spouseName" class="form-control" placeholder="Spouse name"/>
                    </div>
                    <label class="radio-inline has-error">
                       <?php echo form_error('edit_spouseName','*<span3 class="help-inline">','</span3>'); ?>
                    </label>
                </div>
                <!--Children #------------------------------------------------->
                <div class="form-group">
                    <label for="edit_numChildren" class="col-sm-2 control-label">Number of Children</label>
                    <div class="col-md-2">
                        <input type="text" class="form-control" name="edit_numChildren" id="edit_numChildren" placeholder="- - -"/>
                    </div>
                    <label class="radio-inline has-error">
                        <?php echo form_error('edit_numChildren','*<span3 class="help-inline">','</span3>'); ?>
                    </label>
                </div>

                <!--Date of birth--------------------------------------------------->
                <div class="form-group">
                    <label for="edit_DOB" class="col-sm-2 control-label">Date of Birth</label>
                    <div class="col-sm-4">
                        <div class='input-group date' data-date-format="yyyy/mm/dd">
                            <input type='text' class="form-control" readonly="" name="edit_DOB" id="edit_DOB" />
                            <span class="input-group-addon">
                                <i class="fa fa-calendar"></i>
                            </span>
                        </div>
                    </div>
                    <label class="radio-inline has-error">
                        <?php echo form_error('edit_DOB','*<span3 class="help-inline">','</span3>');?>
                    </label>
                </div>
                <!--Email----------------------------------------------------------->
                <div class="form-group">
                    <label for="edit_email" class="col-sm-2 control-label">Email</label>
                    <div class="col-sm-4">
                        <input type="email" id="edit_email" name="edit_email" class="form-control" placeholder="Enter email.."/>
                    </div>
                    <label class="radio-inline has-error">
                        <?php echo form_error('edit_email','*<span3 class="help-inline">','</span3>');?>
                    </label>
                </div>
                <!--Contact Number--------------------------------------------------------->
                <div class="form-group">
                     <label for="edit_contactNo" class="col-sm-2 control-label">Contact No.</label>
                     <div class="col-sm-4">
                         <input type="text" id="edit_contactNo" name="edit_contactNo" class="form-control" placeholder="Contact No." />
                     </div>
                     <label class="radio-inline has-error">
                        <?php echo form_error('edit_contactNo','*<span3 class="help-inline">','</span3>');?>
                    </label>
                </div> 
          </div> <!--end of BODY-->
          <div class="modal-footer"><!--FOOTER-->
             <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            <button type="submit" class="btn btn-primary">Save</button>
          </div><!--end of FOOTER-->
        </div><!--end of form-horizontal -->
       <?=form_close()?>
      </div>
    </div>
  </div>
</div>

脚本

<script type="text/javascript">

 $(document).ready(function(){
   var id_edit = $(this).data('id');
   var base_url = <?php echo base_url('admin/studentController/getStudent');?>;
   $('.editStudent').click(function (){
     $.ajax({
      url: base_url,
      type: 'POST',
      data: { 'stud_id': id_edit},
      dataType: 'JSON';
      success: function(result){
           $('.modal-body #edit_fullname').val(result[0].name);
           $('.modal-body .edit_genderRadio').val(result[0].gender);
           $('.modal-body .edit_civilStatus').val(result[0].marital_status);
           $('.modal-body #edit_spouseName').val(result[0].spouse_name);
           $('.modal-body #edit_numChildren').val(result[0].no_of_children);
           $('.modal-body .edit_DOB').val(result[0].DOB);
           $('.modal-body #edit_email').val(result[0].email);
           $('.modal-body #edit_contactNo').val(result[0].contactNo);                      
      }
     });
    });    
  });
</script>

0 个答案:

没有答案