doufang3001 2019-07-12 09:51
浏览 105
已采纳

我必须从Controller访问表tr以突出显示行

I have My table in controller and I want to select <tr> to highlight table row from view page with script after table row selected

View:

<link rel="stylesheet" href="<?php echo base_url(); ?>assets/plugins/datatables/dataTables.bootstrap4.css">
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/custom/css/custom_style.css">
    <style>
       .highlight { background-color: #1DA5FF; color:#fff; }
        th,
        td {
            white-space: nowrap;
        }
        div.dataTables_wrapper {
            direction: rtl;
        }
        div.dataTables_wrapper {
            width: 1000px;  
            margin: 0 auto;
            font-family: Vazir;
            font-size: 10px;
        }
        th {
            min-width: 80px;
            height: 32px;
            border: 1px solid #222;
            white-space: nowrap;
        }
        td {

            min-width: 80px;
            height: 32px;
            border: 1px solid #222;
            white-space: nowrap;
            text-align: center;
        }
    </style>

<form role="form" id="print_loading_sheet" method="POST" enctype="multipart/form-data">
  <section class="content">
      <div class="row">
       <div class="table-responsive">
         <table id="loading_sheet_table" class="table table-bordered table-striped table-sm" >
        </table>
     </div>
   </div>
  </section>
</form>

<script>
  $(document).ready(function($) {
          var ac_type="<?php echo $_POST['ac_type']; ?>";
          var Station="<?php echo isset($_POST['Station'])? $_POST['Station'] :''; ?>";
          var MainStaion="<?php echo isset($_POST['MainStaion'])? $_POST['MainStaion'] : '';  ?>";
          var All="<?php echo isset($_POST['All'])? $_POST['All'] : '';  ?>";


       $.ajax({
               url :"<?php echo base_url(); ?>booking/report/loading_sheet/LoadingSheetController/loadingSheet",
               type: 'POST',               
               data: {
                ac_type:ac_type,
                Station:Station,
                MainStaion:MainStaion
               },
               dataType: "html",
               success: function(data){

                  $('#loading_sheet_table').html(data);

               },async:false,
               error:function(data){
                console.log('error occured during featch');
               }
           });

      $("#loading_sheet_table tr").click(function() {
      var selected = $(this).hasClass("highlight");
      $("#loading_sheet_table tr").removeClass("highlight");
      if(!selected)
      $(this).addClass("highlight");
      });

   });
</script>

Controller:

<?php 

 public function loadingSheet(){
         $brnachId     = $this->session->userdata('user_branch');
         $ac_type      = $this->input->post('ac_type');

         $formData = array();
         $data = array( 'ac_type'           => $ac_type,                
                         'station'           => $to, );        

          $this->load->model('booking/report/LoadingSheetModel','sendValues');
          $modResult = $this->sendValues->receivingSheetOfStationwise($data,$brnachId); 

          ?>

     <form role="form" id="bilties" name="bilties" method="post">
            <table id="loading_sheet_table" class="table table-bordered  table-sm" style=" overflow: auto;" >
                <thead >
                  <tr>
                  <th class="col1"><input   type="checkbox" name="selectedrecord" class="checkbox" value="1"><br>Bilty Id</th>
                  <th class="col2"><input   type="checkbox" name="selectedrecord" class="checkbox" value="2"><br>LR No</th>
                  <th class="col3"><input   type="checkbox" name="selectedrecord" class="checkbox" value="3"><br>Consignor Name</th>
                  </tr>
                </thead>
                <tbody>
                  <?php foreach($modResult as $bilty):?>

                  <tr>
                  <td><?php echo $bilty->id;?></td>
                  <td><?php echo $bilty->lr_no;?></td>
                  <td><?php echo $bilty->consignor;?></td>
                </tr>
                <?php endforeach; ?>              
                </tbody> 
                  </tr>     
           </table>
    </form>  

         <?php } ?>

I do not know where I am wrong in my code

  • 写回答

3条回答 默认 最新

  • duanchi4184 2019-07-12 10:07
    关注

    just replace your jquery code with

    $(document).on("click","#loading_sheet_table tr",function() {
      var selected = $(this).hasClass("highlight");
      $("#loading_sheet_table tr").removeClass("highlight");
      if(!selected)
      $(this).addClass("highlight");
    });
    

    because you have to take dynamically generated element click event.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!