1

我在面板中创建了一些用户,然后根据用户权限为他们分配一些信息/数据。

我的问题是。我只想显示和搜索“谁在线”的数据,或者登录用户只能查看和搜索管理员在 DataTable 服务器端分配给他/她的数据。

请帮忙

这是我下面的代码

代码

<table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Type</th>
        <th>Email</th>
        <th>Country</th>
        <th>Username</th>
        <th>Mobile</th>
        <th>Status</th>
        <th>Actions</th>
      </tr>
    </thead>
    <tbody>

    <script type="text/javascript">
    var table;
    $(document).ready(function() {
      table = $('#table').DataTable({

        "processing": true, //Feature control the processing indicator.
        "serverSide": true, //Feature control DataTables' server-side processing mode.
        "responsive": true,
        "iDisplayLength": 50,
        // Load data for the table's content from an Ajax source
        "ajax": {
            "url": "<?php echo site_url('customer/ajax_list')?>",
            "type": "POST"
        },

        //Set column definition initialisation properties.
        "columnDefs": [
        {
          "targets": [ -1 ], //last column
          "orderable": false, //set not orderable

        },
        ],

      });
    });
    </script>
    </tbody>
  </table>

控制器

public function ajax_list() {
$table = 'rs_tbl_users_data'; # DATABASE TABLE NAME

    if( $this->session->userdata('user_id') == 1 && 
        $this->session->userdata('user_type') == 1) {

        $wherUserID_FK = ''; # WHERE CONDITION FOR ADMIN
        # empty is for admin to see all data
    }
    else {
        # WHERE CONDITION FOR LOGIN USER
        $wherUserID_FK = array('user_id_fk' => $this->session->userdata('user_id'));
    }

    $column = array('user_id','username','email','first_name','mobile','reg_date','status','user_type','country','user_id_fk');
    $order = array('user_id' => 'DESC');

    $list = $this->dataTable->get_datatables($table, $column, $wherUserID_FK, $order);
    $data = array();
    $no = $_POST['start'];

    foreach ($list as $person) {

        $no++;
        $row = array();
        $row[] = $person->user_id;
        $row[] = $person->first_name;
        // USER TYPE IS USER IS ADMIN / RESELLER OR ONLY USER
        if( $person->user_type == 1){
            $userType = 'Admin';
            } else if( $person->user_type == 2 ){
                $userType = 'Reseller';
                } else if( $person->user_type == 3) {
                    $userType = 'Customer';
                } else {
                    $userType = '';
                }
        $row[] = '<button type="button" class="btn btn-xs btn-warning">'.$userType.'</button>';
        $row[] = $person->email;
        if( !empty($person->country) ) {
            $cntry = $this->countryName( $person->country );
        } else {
            $cntry = 'No Country Found.';
        }
        $row[] = '<button type="button" class="btn btn-xs btn-primary">'.$cntry.'</button>';
        $row[] = $person->username;

        if( !empty($person->mobile) ){
            $contactNum = $person->mobile;
            } else {
                $contactNum = 'No Contact';
            }
        $row[] = $contactNum;

        # STATUS RECORD SECTION START
        if($person->status == 1 ) {
            $status = '<a href="" class="btn btn-xs btn-info">Active</a>';
        } else {
            $status = '<a href="" class="btn btn-xs btn-danger">InActive</a>';
        }
        $row[] = $status;
        # STATUS RECORD SECTION END

        //ADD HTML FOR ACTION / OPERATIONS
        $row[] = '<div class="btn-group btn-group-xs" role="group" aria-label="Action Buttons">
                  <a class="btn btn-sm btn-success" href="" title="Edit"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
                  <a class="btn btn-sm btn-danger" href="" title="Hapus"><i class="glyphicon glyphicon-trash"></i> Delete</a></div>';

        $data[] = $row;
    }

    $output = array(
                    "draw" => $_POST['draw'],
                    "recordsTotal" => $this->dataTable->count_all($table, $wherUserID_FK),
                    "recordsFiltered" => $this->dataTable->count_filtered($table, $wherUserID_FK),
                    "data" => $data,
            );
    //output to json format
    echo json_encode($output);  
    }

模型

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Datatable_model_new extends CI_Model {

var $table;
var $column;
var $order;
var $where;

public function __construct()
{
    parent::__construct();
    $this->load->database();
}

private function _get_datatables_query($tableName, $where)
{
    $this->db->from($tableName);
    if( !empty($where) ) {
        $this->db->where($where);
    }
    $i = 0;
    foreach ($this->column as $item)
    {
        if($_POST['search']['value']) {
            if( $i === 0){
                $this->db->like($item, $_POST['search']['value']);
            }
            else {
                $this->db->or_like($item, $_POST['search']['value']);
            }
        }
        $column[$i] = $item;
        $i++;
    }

    if(isset($_POST['order']))
    {
        # FOR DESC
        $this->db->order_by($column[$_POST['order']['0']['column']], 'DESC');
    }
    else if(isset($this->order))
    {
        $order = $this->order;
        $this->db->order_by(key($order), $order[key($order)]);
    }
    //echo $query = $this->db->last_query();exit;
}


function get_datatables($tableName, $columnArray, $where, $orderBY)
{
    $this->table = $tableName;
    $this->column = $columnArray;
    $this->order = $orderBY;
    $this->where = $where;

    if( !empty($where) ){
        $this->where = $where;
    }

    $this->_get_datatables_query($tableName, $where);
    $term = $_REQUEST['search']['value'];
    //$this->_get_datatables_query($term, $this->table, $this->where);

    if($_POST['length'] != -1)
        $this->db->limit($_POST['length'], $_POST['start']);

    //$this->db->from($tableName);
    $query = $this->db->get();
    //echo $query = $this->db->last_query();exit;
    return $query->result();
}

function count_filtered($tableName = NULL, $where = NULL)
{
    //$this->_get_datatables_query();
    $this->db->from($tableName);
    if( !empty($where) ) {
        $this->db->where($where);
    }
    $query = $this->db->get();
    //echo $query = $this->db->last_query();exit;
    return $query->num_rows();
}

public function count_all($tableName = NULL, $where = NULL)
{
    $this->db->from($tableName);
    if( !empty($where) ) {
        $this->db->where($where);
    }
    //echo $query = $this->db->last_query();exit;
    return $this->db->count_all_results();
}

public function get_by_id($id)
{
    $this->db->from($this->table);
    //$this->db->where('channel_id',$id);
    $query = $this->db->get();

    return $query->row();
}

public function save($data)
{
    $this->db->insert($this->table, $data);
    return $this->db->insert_id();
}

public function update($where, $data)
{
    $this->db->update($this->table, $data, $where);
    return $this->db->affected_rows();
}

public function delete_by_id($id)
{
    //$this->db->where('channel_id', $id);
    $this->db->delete($this->table);
}
}
4

0 回答 0