我试图通过Cjuidiag框中的按钮更新mongodb后更新CGridview。 我的代码:
的index.php
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
'id'=>'uploaddialog',
'options'=>array(
'title'=>'Edit Documents',
'autoOpen'=>false,
'modal'=>true,
'width'=>958,
'height'=>623,
),
));
?>
<iframe id="cru-frame-struct" width="100%" height="100%" frameBorder="0" scrolling="yes" >
<?php //$this->renderPartial('uploaddocument',array()); ?>
</iframe>
<?php $this->endWidget('zii.widgets.jui.CJuiDialog');?>
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'Prd-grid',
'dataProvider'=>$dataProvider,
'columns' => array(
array(
'class'=>'CButtonColumn','template' => '{updateline}',
'buttons' => array(
'updateline' => array(
'options'=>array('style'=>'margin-left:20px'),
'imageUrl'=>\Yii::app()->request->baseUrl.'/images/update.png',
'url'=>'$this->grid->controller->createUrl("editdocument",
array("id"=>$data["Document_id"],"OriginObject"=>isset($data["OriginObject"])?$data["OriginObject"]:""))', //
'click'=>'function(){
$("#cru-frame-struct").attr("src",$(this).attr("href"));
$("#uploaddialog").dialog("open");
return false;
}',
)
),
),
array(
'name' => 'ID',
'type' => 'raw',
'value' => '$data["Document_id"]'
),
),
'htmlOptions' => array(
'class' => ""
),
));
?>
Uploaddocument.php
<?php echo CHtml::button('Update WorkFlow',array('id'=>'updworkflow','class'=>'btn','onclick'=>'workflow()','background-style'=>'none')); ?>
<h4><?php echo "Document Log";?></h4>
<?php
$mongo = new MongoClient( Yii::app()->mongodb->connectionString);
$dbname = Yii::app()->mongodb->dbName;
$temp = array();
$colname = 'Documents';
$collection = $mongo->$dbname->$colname;
$cursor = $collection->find(array("_id" => new MongoId($DocId)));
foreach ($dcursor as $ddoc) {
if(isset($ddoc['Workflows']))
{
$sdoc =($ddoc['Workflows']);
foreach ($sdoc as $wflw)
{
$temp ["Date"]= $wflw["Date"];
$temp ["Initials"]= $wflw["Initials"];
$temp ["Status"]= $wflw["Status"];
$temp ["Comments"]= $wflw["Comments"];
$pendarray[] = $temp;
}
}
}
$dataProvider = new \CArrayDataProvider ($pendarray,array('keyField'=>'Date'));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'documentlog-grid',
'dataProvider'=>$dataProvider,
'columns' => array(
array(
'name' => 'Date',
'type' => 'raw',
'value' => 'isset($data["Date"])?$data["Date"]:""'
),
array(
'name' => 'Initials',
'type' => 'raw',
'value' => 'isset($data["Initials"])?$data["Initials"]:""'
),
array(
'name' => 'Status',
'type' => 'raw',
'value' => 'isset($data["Status"])?$data["Status"]:""'
),
array(
'name' => 'Comment',
'type' => 'raw',
'value' => 'isset($data["Comments"])?$data["Comments"]:""'
),
),
'htmlOptions' => array(
'class' => ""
),
));
?>
Controller.php这样:
public function actionUpdateWorkflow()
{
$status = isset($_POST["Status"])?$_POST["Status"]:"";
$comments = isset($_POST["Comments"])?$_POST["Comments"]:"";
$now = new \DateTime();
$mongo = new \MongoClient( \Yii::app()->mongodb->connectionString);
$dbname = \Yii::app()->mongodb->dbName;
$colname = 'Documents';
$mongo->$dbname->$colname->update(array("_id" => new \MongoId($_POST["id"])),
array('$addToSet' => array(
'Workflows' => array(
'Date' => $now->format('Y-m-d H:i:s'),
'Initials' =>\Yii::app()->user->getUserName(),
'Status'=>$status,
'Comments'=>$comments,
)))
);
//echo \CHtml::script("$.fn.yiiGridView.update('documentlog-grid');
// window.parent.$.fn.yiiGridView.update('documentlog-grid');");
\Yii::app()->end();
}
public function actionIndex()
{
$this->render('index');
}
public function actioneditdocument()
{
$this->layout = '//layouts/customIFrame';
$this->renderPartial('uploaddocument',array(
'DocId'=>$DocId,
));
}
}
的JavaScript
<script>
function workflow()
{
$.ajax({
type:"POST",
url :"<?php echo CController::createUrl('default/UpdateWorkflow'); ?>",
data:{"Comments":comments,
"Status":status,
"id":docid
},
success:function (data) {
$.fn.yiiGridView.update('documentlog-grid');
return true;
}
error: function(){
alert('failure ');
}
});
}
</script>
我不确定此代码中的错误。我用mongo Data填充网格。我希望在每次更新/插入mongodb按钮后刷新网格数据。 请帮我解决这个问题。
答案 0 :(得分:0)
你可以尝试这样做并告诉我它是如何工作的:
$('#documentlog-grid').yiiGridView('update');
也可以从浏览器控制台尝试。