我在 Yii 应用程序中使用了融合图表扩展。我正在使用 ajax 提交来创建各种图表。它呈现,但是当我尝试使用 ajax 过滤它时,我得到相同的输出。
这是我在视图中的代码:
<?php
$this->renderPartial('_search'); ?>
<p id="ChartArea" style="text-align:center;">
<?php
$this->renderPartial('_chart',array('cchart'=>'mychart',
'chartType'=>'Line','chartName'=>'callTrend','data'=>array('')));?>
在 _search.php 中:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'calltrend-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('onsubmit'=>"return false;"),
));
echo CHtml::hiddenField('chartType','Line');
echo CHtml::hiddenField('chartName','callTrend');
echo " ".CHtml::Label('From:','from_date',
array('style'=>'display:inline;clear:both;'))." ";
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'attribute'=>'from_date',
'name'=>'from_date',
'htmlOptions'=>array('class'=>'input-small','style'=>'margin-top:9px;'),
'options'=>array(
'dateFormat' => 'yy-mm-dd',
'altField' => '#self_pointing_id',
'altFormat' => 'yy-mm-dd',
'changeYear'=>'true',
'changeMonth'=>'true',
),
));
echo " ".CHtml::Label('To:','to_date',array('style'=>'display:
inline;clear:both;'))." ";
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'attribute'=>'to_date',
'name'=>'to_date',
'htmlOptions'=>array('class'=>'input-small','style'=>'margin-top:9px;'),
'options'=>array(
'dateFormat' => 'yy-mm-dd',
'altField' => '#self_pointing_id',
'altFormat' => 'yy-mm-dd',
'changeYear'=>'true',
'changeMonth'=>'true',),
));
echo " ".CHtml::Label('Project:','project',array('style'=>'
display:inline;clear:both;'))." ";
echo CHtml::dropDownList('project','',CHtml::listData(Project::model()->findAll()
,'id','name'),array('empty' => '--- Choose---','class'=>'inputmedium','style'=>'
clear:both;margin-top:9px;'));
echo " ".CHtml::submitButton(Yii::t('app',
'Generate'),array('class'=>'btn btn-primary','style'=>'clear:both;'));
echo " ".CHtml::ajaxSubmitButton('Generate',
CHtml::normalizeUrl(array('site/chart')),
array(
'data'=>'js:jQuery(this).parents("form").serialize()+ "&request=added"',
'success'=>'function(data){
$("#ChartArea").html(data);
}'
),
array(
'id'=>'ajaxSubmitBtn',
'name'=>'ajaxSubmitBtn',
'class'=>'btn btn-primary'
));
$this->endWidget();
在控制器中:actionChart() 方法:
public function actionChart()
{
if(Yii::app()->request->isAjaxRequest)
{
$chartName=$_POST["chartName"];
if($chartName=="callTrend")
{
$data = array('from_date'=>$_POST["from_date"],'to_date'
=>$_POST["to_date"],'project_id'=>$_POST["project"]);
$cchart="mychart";
}
elseif($chartName=="topProducts"){
$data = array('from_date'=>$_POST["from_date1"],'to_date'=>$_POST["to_date1"]);
$cchart="mychart1";
}
elseif($chartName=="topCallers"){
$data = array('project_id'=>$_POST["project1"]);
$cchart="mychart2";
}
$chartType=$_POST["chartType"];
}
else
{
$data=array();
$chartName="callTrend";
$cchart="mychart";
$chartType="Line";
}
$this->renderPartial('_chart',array('cchart'=>$cchart,'chartName' =>$chartName,
'chartType'=>$chartType,'data'=>$data));
}
在我的控制器中:actionCallTrend 方法:
public function actionCallTrend($from_date=NULL,$to_date=NULL,$project_id=NULL)
{
echo Yii::trace(CVarDumper::dumpAsString($from_date),'vardump');
echo Yii::trace(CVarDumper::dumpAsString($to_date),'vardump');
Yii::app()->fusioncharts->setChartOptions( array( 'caption'=>'Last Week Call Trend',
'xAxisName'=>'Date', 'yAxisName'=>'Calls' ) );
$con="";
if($from_date!=NULL && $to_date==NULL)
{
$con="DATE(answer_ts) >= '$from_date'"; // date is database date column field
}elseif($to_date!=NULL && $from_date==NULL)
{
$con="DATE(answer_ts) <= '$to_date'";
}elseif($from_date!=NULL and $to_date!=NULL){
$con="DATE(answer_ts) >= '$from_date' and DATE(answer_ts) <= '$to_date'";
}
if(($from_date!=NULL or $to_date!=NULL) && $project_id!=NULL)
{
$con.=" and project_id = '$project_id'"; // date is database date column field
}
$stats=Creport::model()->findAll(array(
'select'=>'COUNT(id) as data_count, DATE(answer_ts) as unique_date, answer_ts',
'condition'=>$con,
'group'=>'DATE(answer_ts)',
'order'=>'answer_ts desc',
'limit'=>10,
));
$ddg=Yii::app()->dateFormatter;
foreach ($stats as $s){
Yii::app()->fusioncharts->addSet(array('label'=>$ddg->format("dd",$s->answer_ts)."-"
.$ddg->format("MMM",$s->answer_ts), 'value'=>$s->data_count));
}
Yii::app()->fusioncharts->useI18N = true;
Yii::app()->fusioncharts->addTrendLine(array('startValue'=>'700000',
'color'=>'009933', 'displayvalue'=>'Target'));
Yii::app()->fusioncharts->addDefinition(array('name'=>'CanvasAnim',
'type'=>'animation', 'param'=>'_xScale', 'start'=>'0', 'duration'=>'1'));
Yii::app()->fusioncharts->addApplication(array('toObject'=>'Canvas',
'styles'=>'CanvasAnim'));
Yii::app()->fusioncharts->getXMLData();
}
这是我的代码。它可以在没有 ajax 的情况下工作,但是当我使用 ajax 过滤字段时,将显示相同的输出,它们不会在 actionCallTrend 控制器中获得 from_date、to_date 和 project_id。