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

时间:2019-07-18 13:56:57

标签: javascript php html codeigniter datatable

我的目标是将数据库中的每个“ 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);

}

2 个答案:

答案 0 :(得分:1)

尝试:-

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) {
    $in = $out = '';
    if($r->online == 'IN'){
    $in = '<i class="fa fa-circle" style="font-size:36px; color:green"></i>';
     }
    if($r->offline== 'OUT'){
    $out = '<i class="fa fa-circle" style="font-size:36px; color:red"></i>';
     }
       $data[] = array(
            $r->firstName,
            $in,
            $out,
            $r->comment,

       );
  }

  $output = array(
       "draw" => $draw,
         "recordsTotal" => $status->num_rows(),
         "recordsFiltered" => $status->num_rows(),
         "data" => $data
    );
  echo json_encode($output);

}

和js:-

<script>
   $('table').DataTable({


searching: false, paging: false,
    "ajax": {
        url : "<?php echo site_url("getStatu") ?>",
        type : 'GET', 
             },
        "columnDefs": [
        {
        "render": function ( data, type, full, meta) {
        },

    },  ]         
});
</script>

答案 1 :(得分:0)

如果请求返回正确的数据,并且INOUT列是第二列和第三列,则可以这样编写:

$('table').DataTable({
    searching: false, paging: false,
    "ajax": {
        url : "<?php echo site_url("getStatu") ?>",
        type : 'GET', 
    },
    "columnDefs": [
        { 
            targets: 1,
            data: 1,
            "orderable" : false,
            "defaultContent" : "",
            "render": function ( data, type, full, meta) {
                if  (data=="IN")
                {    
                    data = '<i class="fa fa-circle" style="font-size:36px; color:green"></i>'
                }
                return data;
            }
        },
        { 
            targets: 2,
            data: 2,
            "orderable" : false,
            "defaultContent" : "",
            "render": function ( data, type, full, meta) {
                if  (data=="OUT")
                {    
                    data = '<i class="fa fa-circle" style="font-size:36px; color:red"></i>'
                }
                return data;
            }
        }
    ]
});