我有一个这样的页面:
基本上,我选择2个日期并点击按钮,然后下面的数据会在不刷新此页面的情况下发生变化。
以下是控制器中的代码:
if( $this->request->is('ajax') ) {
$this->autoRender = false;
//if ($this->request->isPost()) {
print_r($this->request->data);
// get values here
echo $from=( $this->request->data('start_time'));
echo $to= $this->request->data('end_time');
Debugger::dump($from);
Debugger::dump($to);
//$this->layout = 'customer-backend';
$this->Order->recursive=-1;
$this->Order->virtualFields['benefit']='SUM(Product.product_price - Discount.product_discount)';
$this->Order->virtualFields['number']='COUNT(Order.order_id)';
$option['joins'] = array(
array('table'=>'discounts',
'alias'=>'Discount',
'type'=>'INNER',
'conditions'=>array(
'Order.discount_id = Discount.discount_id',
)
),
array('table'=>'products',
'alias'=>'Product',
'type'=>'INNER',
'conditions'=>array(
'Discount.product_id = Product.product_id'
)
)
);
$option['fields']= array('Discount.product_id','Product.product_name','benefit','number');
$option['conditions']=array('Discount.start_time >='=>$from);
$option['group'] = array('Discount.product_id','Product.product_name');
//$option['limit']=20;
$products = $this->Order->find('all',$option);
//Debugger::dump($products);
$this->set('products',$products);
//}
}
else
{
$from='27 November 2012';
//$this->layout = 'customer-backend';
$this->Order->recursive=-1;
$this->Order->virtualFields['benefit']='SUM(Product.product_price - Discount.product_discount)';
$this->Order->virtualFields['number']='COUNT(Order.order_id)';
$option['joins'] = array(
array('table'=>'discounts',
'alias'=>'Discount',
'type'=>'INNER',
'conditions'=>array(
'Order.discount_id = Discount.discount_id',
)
),
array('table'=>'products',
'alias'=>'Product',
'type'=>'INNER',
'conditions'=>array(
'Discount.product_id = Product.product_id'
)
)
);
$option['fields']= array('Discount.product_id','Product.product_name','benefit','number');
$option['conditions']=array('Discount.start_time >='=>$from);
$option['group'] = array('Discount.product_id','Product.product_name');
//$option['limit']=20;
$products = $this->Order->find('all',$option);
$this->set('products',$products);
}
如果请求是ajax,它会从POST中获取2个值$from
和$to
,并将它们传递给SQL查询。如果请求不是ajax(意味着当日期尚未选择时第一次访问此页面),则为$ from和$ to分配默认值。
以下是我的ajax:
<script>
$(function(){
$('#btnSubmit').click(function() {
var from = $('#from').val();
var to = $('#to').val();
alert(from+" "+to);
$.ajax({
url: "/project/cakephp/orders/hottest_products",
type: 'POST',
data: {"start_time": from, "end_time": to },
success: function(data){
alert("success");
}
});
});
});
它从2个日期选择器获取数据,然后以POST方式将其发送到控制器。
我的问题是,在我选择2个日期并按下按钮后,没有任何反应。数据根据日期不会改变。 对此有任何想法。提前致谢。
答案 0 :(得分:1)
打开页面并在控制台中运行以下内容时:
$(".tab_container").html("loaded from ajax");
产品表现在只显示“从ajax加载”。如果products表的内容是由它自己的模板生成的,那么只有当它是ajax调用时才能让cakephp呈现该模板:http://book.cakephp.org/2.0/en/controllers.html
$this->render('/Path/To/ProductTable/');
如果你的cakephp在调用ajax时只输出product表,你可以尝试运行以下代码:
var from = "2000-01-01";
var to = "2014-01-01";
$.ajax({
url: "/project/cakephp/orders/hottest_products",
type: 'POST',
data: {"start_time": from, "end_time": to }
}).then(
function(result){
$(".tab_container").html(result);
},function(){
console.log("fail",arguments);
}
);