通过ajax渲染布局

时间:2014-02-17 11:49:15

标签: jquery ajax fuelphp

我通过名为“thisWeek”的控制器调用名为“thisweek”的视图。它将返回一个数组和一个要查看的对象。现在我想通过ajax改变它。所以我打电话给ajax再次调用控制器“thisweek”并传递数据。但它无法呈现布局。 控制器是: -

public function action_thisWeek(){
    $keywords = Input::post("keywords");
    $user = (Input::post("user") == "") ? -2 : Input::post("user");
    $direction = Input::post("direction");
            if(isset($_POST['from_date']) && isset($_POST['to_date'])):
                $data['from'] = $_POST['from_date'];
                $data['to'] =  $_POST['to_date'] ; 
            else:
                echo $data['from'] = (date('N', time()) == 1) ? date('Y-m-d') : date('Y-m-d', strtotime("last monday"));
                echo $data['to'] = date('Y-m-d', strtotime("next sunday"));
            endif;
            $data["dateRange"] = $this->_createDateRangeArray($data['from'], $data['to']);
    if(Input::get("print") && Input::get("roulatie")) {
        $data['reservations'] = $this->getReservations(
            array("keywords" => $keywords, 
                "user" => $user, 
                "direction" => Input::get("print"), 
                "date" => Input::get("roulatie")
            )
        );
    } else {
        $data['reservations'] = $this->getReservations(
            array("keywords" => $keywords, 
                "user" => $user, 
                "direction" => $direction, 
                "from" => $data['from'], 
                "to" => $data['to']
            )
        );      
    }
    if(Input::get("print")) return $this->_print($data);
            if(!isset($_POST['from_date']) && !isset($_POST['to_date'])):
    $this->template->title = "Reserveringen - deze week";
            endif;
            $this->template->content = View::forge('admin/reservations/thisweek', $data);     
}

Ajax调用是: -

$.ajax({
 type: "POST",
url:  "<?php echo Uri::base(false) ?>index.php/admin/reservations/selectWeek",
 data: {to_date:week_to, from_date:week_from },
 success: function(response){
        }
        });

请帮助我如何通过ajax渲染布局。

1 个答案:

答案 0 :(得分:0)

我得到了解决方案,我使用响应来发送html,即我将控制器更新为

public function action_thisWeek(){
    $keywords = Input::post("keywords");
    $user = (Input::post("user") == "") ? -2 : Input::post("user");
    $direction = Input::post("direction");
            if(isset($_POST['from_date']) && isset($_POST['to_date'])):
                $data['from'] = $_POST['from_date'];
                $data['to'] =  $_POST['to_date'] ; 
            else:
                echo $data['from'] = (date('N', time()) == 1) ? date('Y-m-d') : date('Y-m-d', strtotime("last monday"));
                echo $data['to'] = date('Y-m-d', strtotime("next sunday"));
            endif;
            $data["dateRange"] = $this->_createDateRangeArray($data['from'], $data['to']);
    if(Input::get("print") && Input::get("roulatie")) {
        $data['reservations'] = $this->getReservations(
            array("keywords" => $keywords, 
                "user" => $user, 
                "direction" => Input::get("print"), 
                "date" => Input::get("roulatie")
            )
        );
    } else {
        $data['reservations'] = $this->getReservations(
            array("keywords" => $keywords, 
                "user" => $user, 
                "direction" => $direction, 
                "from" => $data['from'], 
                "to" => $data['to']
            )
        );      
    }
            $weekNumber = date("W"); 
    if(Input::get("print")) return $this->_print($data);
            if(!isset($_POST['from_date']) && !isset($_POST['to_date'])):
    $this->template->title = "Reserveringen - ". $weekNumber ." week";
             $this->template->content = View::forge('admin/reservations/thisweek', $data); 
             else:
                 return Response::forge(View::forge('admin/reservations/thisweek', $data)); 
            endif;

}