CJuiProgressBar 封裝 jQuery UI Progressbar,提供視覺化進度指示,支援確定/不確定模式、動畫與動態數值更新。
<?php $this->widget('zii.widgets.jui.CJuiProgressBar', array(
'id' => 'progress',
'value' => 30, // 0-100
'htmlOptions' => array(
'style' => 'width:300px; height:20px;',
),
)); ?>
<?php
Yii::app()->clientScript->registerScript('progress-sim', "
var progress = 0;
var timer = setInterval(function() {
progress += Math.random() * 10;
if (progress >= 100) {
progress = 100;
clearInterval(timer);
$('#status').text('完成!');
}
$('#progress').progressbar('value', progress);
$('#percent').text(Math.floor(progress) + '%');
}, 500);
", CClientScript::POS_READY);
?>
<div id="progress" style="width:300px;"></div>
<span id="percent">0%</span>
<span id="status" style="margin-left:10px;">進行中...</span>
<?php
Yii::app()->clientScript->registerScript('progress-ajax', "
function pollProgress(taskId) {
$.getJSON('".Yii::app()->createUrl('task/progress')."', { id: taskId }, function(data) {
$('#progress').progressbar('value', data.percent);
$('#status').text(data.status);
if (data.percent < 100) {
setTimeout(function() { pollProgress(taskId); }, 2000);
} else {
$('#status').text('任務完成');
}
});
}
// 假設任務啟動後取得 taskId
pollProgress(123);
", CClientScript::POS_READY);
?>
後端 actionProgress 回傳 JSON:{percent: 45, status: '處理第 3 步驟...'}
<?php $this->widget('zii.widgets.jui.CJuiProgressBar', array(
'id' => 'indeterminate',
'value' => false, // 不確定模式
'htmlOptions' => array('style' => 'width:100%;'),
)); ?>
適用於無法預估進度的操作(如連線中、初始化中)。
/* CSS */
#progress .ui-progressbar-value {
background: linear-gradient(to right, #4caf50, #8bc34a) !important;
border-radius: 4px;
}
#progress {
border-radius: 4px;
background: #e0e0e0;
border: 1px solid #bdbdbd;
}