数据表 columndef 渲染未命中且没有错误

我的目标是将数据库中的每个“OUT”或“IN”更改为红色或绿色圆圈,但没有任何变化,也没有错误。在 IN 和 OUT 列中将包含值“IN”和值“OUT”,我想转换为绿色圆圈或红色圆圈。


我不知道我做错了什么。


看法


                <th scope="col">Name</th>

            <th scope="col" id="IN">IN</th>

            <th scope="col" id="OUT">OUT</th>

            <th scope="col">Comments</th>

js


    <script>

       $('table').DataTable({

   searching: false, paging: false,

    "ajax": {

        url : "<?php echo site_url("getStatu") ?>",

        type : 'GET', 

             },

        "columnDefs": [

        {

                "data" : "OUT", "orderable" : false,  "defaultContent" : "", 

            "render": function ( data, type, full, meta) {

                  if  (data=="OUT")

                  {    

                                return data ='<i class="fa fa-circle" style="font-size:36px; color:red"></i>'


                   } 

            },


        },  ]         

});

控制器


public function getStatu(){


     $draw = intval($this->input->get("draw"));

      $start = intval($this->input->get("start"));

      $length = intval($this->input->get("length"));



    $this->load->model('Status_Board_Model');


    $status = $this->Status_Board_Model->getStatu();


   $data = array();


      foreach($status->result() as $r) {


           $data[] = array(

                $r->firstName,

                $r->online,

                $r->offline,

                $r->comment,


           );

      }


      $output = array(

           "draw" => $draw,

             "recordsTotal" => $status->num_rows(),

             "recordsFiltered" => $status->num_rows(),

             "data" => $data

        );

      echo json_encode($output);


}


天涯尽头无女友
浏览 141回答 2
2回答

一只甜甜圈

尝试这个:-public function getStatu(){&nbsp;$draw = intval($this->input->get("draw"));&nbsp; $start = intval($this->input->get("start"));&nbsp; $length = intval($this->input->get("length"));$this->load->model('Status_Board_Model');$status = $this->Status_Board_Model->getStatu();$data = array();&nbsp; foreach($status->result() as $r) {&nbsp; &nbsp; $in = $out = '';&nbsp; &nbsp; if($r->online == 'IN'){&nbsp; &nbsp; $in = '<i class="fa fa-circle" style="font-size:36px; color:green"></i>';&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; if($r->offline== 'OUT'){&nbsp; &nbsp; $out = '<i class="fa fa-circle" style="font-size:36px; color:red"></i>';&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;$data[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $r->firstName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $in,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $out,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $r->comment,&nbsp; &nbsp; &nbsp; &nbsp;);&nbsp; }&nbsp; $output = array(&nbsp; &nbsp; &nbsp; &nbsp;"draw" => $draw,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"recordsTotal" => $status->num_rows(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"recordsFiltered" => $status->num_rows(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"data" => $data&nbsp; &nbsp; );&nbsp; echo json_encode($output);}和js:-<script>&nbsp; &nbsp;$('table').DataTable({searching: false, paging: false,&nbsp; &nbsp; "ajax": {&nbsp; &nbsp; &nbsp; &nbsp; url : "<?php echo site_url("getStatu") ?>",&nbsp; &nbsp; &nbsp; &nbsp; type : 'GET',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; "columnDefs": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "render": function ( data, type, full, meta) {&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; },&nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});</script>

开心每一天1111

如果请求返回正确的数据,并且INandOUT列是第 2 和第 3 列,您可以这样写:$('table').DataTable({&nbsp; &nbsp; searching: false, paging: false,&nbsp; &nbsp; "ajax": {&nbsp; &nbsp; &nbsp; &nbsp; url : "<?php echo site_url("getStatu") ?>",&nbsp; &nbsp; &nbsp; &nbsp; type : 'GET',&nbsp;&nbsp; &nbsp; },&nbsp; &nbsp; "columnDefs": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; targets: 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "orderable" : false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "defaultContent" : "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "render": function ( data, type, full, meta) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp; (data=="IN")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = '<i class="fa fa-circle" style="font-size:36px; color:green"></i>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; targets: 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "orderable" : false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "defaultContent" : "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "render": function ( data, type, full, meta) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp; (data=="OUT")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = '<i class="fa fa-circle" style="font-size:36px; color:red"></i>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]});
打开App,查看更多内容
随时随地看视频慕课网APP