2016-07-03 3 views
0
나는 아약스 데이터 테이블에서 일하고 있어요

와 나는 오류를 가지고 페이지를로드하려고 할 때 테이블 ID를 경고ajax URL에서 _remap 함수로 Codeigniter 함수를 호출하는 방법은 무엇입니까?

Datatables = 테이블 - 나는 그것을 할 수있는 뭔가가 있다고 생각

무효 JSON 응답 로드 할 수있는 컨트롤러 함수를 찾을 수없는 것처럼 datatable에서 아약스 URL을 가진 것 같습니다.

내 컨트롤러에서 _remap() 기능을 사용하고 있습니다. 그 이유는 URL과 충돌이있어서 내 기능을 호출 할 수 없다고 생각하는 이유입니다. 이 경우 해결해야하는데 무엇

<?php 

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

class Requests extends CI_Controller { 
var $pgToLoad; 

public function __construct() { 
    parent::__construct(); 
    #this will start the session 
    session_start(); 

    if(!isset($_SESSION['userId']) || !isset($_SESSION['userLevel']) || !isset($_SESSION['employeeid']) || !isset($_SESSION['firstname']) || !isset($_SESSION['lastname'])) { 
     redirect('home', 'location'); 
    } 

    #this will load the model 
    $this->load->model('Contents'); 

    #get last uri segment to determine which content to load 
    $continue = true; 
    $i = 0; 
    do { 
     $i++; 
     if ($this->uri->segment($i) != "") $this->pgToLoad = $this->uri->segment($i); 
     else $continue = false;    
    } while ($continue);   
} 

public function index() { 
    $this->load->helper('url'); 
    $this->main(); 

} 

public function main() { 
    #set default content to load 
    $this->pgToLoad = empty($this->pgToLoad) ? "Requests" : $this->pgToLoad; 
    $disMsg = ""; 

    #this will delete the record selected 
    if($this->uri->segment(2) == 'leave') { 
     $this->leave(); 
    } 

    #this will logout the user and redirect to the page 
    if($this->uri->segment(2) == 'logout') { 
     session_destroy(); 
     redirect('home', 'location'); 
    }     

    $data = array ('pageTitle' => 'Payroll System | ADMINISTRATION', 
        'disMsg' => $disMsg,            
        'mainCont' => $this->mainCont); 

    $this->load->view('mainTpl', $data, FALSE); 
} 



    public function ajax_list() 
{ 
    $list = $this->Contents->get_datatables(); 
    $data = array(); 
    $no = $_POST['start']; 
    foreach ($list as $leave) { 
     $no++; 
     $row = array(); 
     $row[] = $leave->id; 
     $row[] = $leave->type; 
     $row[] = $leave->startdate; 
     $row[] = $leave->enddate; 
     $row[] = $leave->duration; 
     $row[] = $leave->reason; 
     $row[] = $leave->status;  

     //add html for action 
     $row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_person('."'".$leave->id."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a> 
       <a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person('."'".$leave->id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>'; 

       $data[] = $row; 


    } 

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

#this will display the form when editing the product 
public function leave() { 


    $data['employee'] = $this->Contents->exeGetEmpToEdit($_SESSION['userId']); 
     $this->mainCont = $this->load->view('pages/requests/leave', '', TRUE); 

} 
public function _remap() { 
    $this->main(); 
} 

아약스 데이터 테이블 코드

$(document).ready(function() { 

    //datatables 
table = $('#table').DataTable({ 

    "processing": true, //Feature control the processing indicator. 
    "serverSide": true, //Feature control DataTables' server-side processing mode. 
    "order": [], //Initial no order. 

    // Load data for the table's content from an Ajax source 
    "ajax": { 
     **"url": "<?php echo site_url('requests/leave')?>", 
     "type": "POST" 
    }, 

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

}); 

:

이 내 컨트롤러? 당신이

답변

1
case $method == 'IS_AJAX': 

귀하의 $ 방법이 URL에 IS_AJAX되지 감사 :

http://localhost/2fb/index.php/redirect 이는 방법없이 리디렉션 컨트롤러에 당신을 데려 것 ("인덱스"로 기본 설정됩니다). 당신은 문자 그대로 필요합니다 :

http://localhost/2fb/index.php/redirect/IS_AJAX ...이 경우에 들어갑니다. 상수 IS_AJAX와 요청 된 메소드를 혼동하는 것 같습니다. 인덱스를 확인할 때 올바르게 사용하고있는 것처럼 보입니다 (기본 케이스와 동일하기 때문에 중복 됨).

$ method 또는 _remap()에서 첫 번째 매개 변수의 이름을 지정하면 항상 호출 된 라우팅 된 컨트롤러 함수가됩니다.

EDIT : 이전에는 언급하지 못했지만 스위치 블록은 전달한 표현식을 평가하므로 비교를 수동으로 수행 할 필요가 없습니다. 예 :

switch ($method) { 
    // case $method === 'index': 
    case 'index': 
     $this->load->view('main'); 
    break; 
} 
관련 문제