参考网页:
一、下面是摘抄
// DetailView Attributes Configuration$attributes = [ [ 'group'=>true, 'label'=>'SECTION 1: Identification Information', 'rowOptions'=>['class'=>'info'] ], [ 'columns' => [ [ 'attribute'=>'id', 'label'=>'Book #', 'displayOnly'=>true, 'valueColOptions'=>['style'=>'width:30%'] ], [ 'attribute'=>'book_code', 'format'=>'raw', 'value'=>''.$model->book_code.'', 'valueColOptions'=>['style'=>'width:30%'], 'displayOnly'=>true ], ], ], [ 'columns' => [ [ 'attribute'=>'book_name', 'valueColOptions'=>['style'=>'width:30%'], ], [ 'attribute'=>'color', 'format'=>'raw', 'value'=>" " . $model->color . '
', 'type'=>DetailView::INPUT_COLOR, 'valueColOptions'=>['style'=>'width:30%'], ], ], ], [ 'group'=>true, 'label'=>'SECTION 2: Price / Valuation Amounts', 'rowOptions'=>['class'=>'info'], //'groupOptions'=>['class'=>'text-center'] ], [ 'attribute'=>'buy_amount', 'label'=>'Buy Amount ($)', 'format'=>['decimal', 2], 'inputContainer' => ['class'=>'col-sm-6'], ], [ 'attribute'=>'sale_amount', 'label'=>'Sale Amount ($)', 'format'=>['decimal', 2], 'inputContainer' => ['class'=>'col-sm-6'], ], [ 'label'=>'Difference ($)', 'value'=>$model->buy_amount - $model->sale_amount, 'format'=>['decimal', 2], 'inputContainer' => ['class'=>'col-sm-6'], // hide this in edit mode by adding `kv-edit-hidden` CSS class 'rowOptions'=>['class'=>'warning kv-edit-hidden', 'style'=>'border-top: 5px double #dedede'], ], [ 'group'=>true, 'label'=>'SECTION 3: Book Details', 'rowOptions'=>['class'=>'info'], //'groupOptions'=>['class'=>'text-center'] ], [ 'columns' => [ [ 'attribute'=>'publish_date', 'format'=>'date', 'type'=>DetailView::INPUT_DATE, 'widgetOptions' => [ 'pluginOptions'=>['format'=>'yyyy-mm-dd'] ], 'valueColOptions'=>['style'=>'width:30%'] ], [ 'attribute'=>'status', 'label'=>'Available?', 'format'=>'raw', 'value'=>$model->status ? 'Yes' : 'No', 'type'=>DetailView::INPUT_SWITCH, 'widgetOptions' => [ 'pluginOptions' => [ 'onText' => 'Yes', 'offText' => 'No', ] ], 'valueColOptions'=>['style'=>'width:30%'] ], ] ], [ 'columns' => [ [ 'attribute'=>'author_id', 'format'=>'raw', 'value'=>Html::a('John Steinbeck', '#', ['class'=>'kv-author-link']), 'type'=>DetailView::INPUT_SELECT2, 'widgetOptions'=>[ 'data'=>ArrayHelper::map(Author::find()->orderBy('name')->asArray()->all(), 'id', 'name'), 'options' => ['placeholder' => 'Select ...'], 'pluginOptions' => ['allowClear'=>true, 'width'=>'100%'], ], 'valueColOptions'=>['style'=>'width:30%'] ], [ 'attribute'=>'rememberMe', 'label'=>'Remember?', 'format'=>'raw', 'type'=>DetailView::INPUT_SWITCH, 'widgetOptions' => [ 'pluginOptions' => [ 'onText' => 'Yes', 'offText' => 'No', ] ], 'value'=>$model->rememberMe ? 'Yes' : 'No', 'valueColOptions'=>['style'=>'width:30%'] ], ] ], [ 'attribute'=>'synopsis', 'format'=>'raw', 'value'=>'' . $model->synopsis . '', 'type'=>DetailView::INPUT_TEXTAREA, 'options'=>['rows'=>4] ]];// View file rendering the widgetecho DetailView::widget([ 'model' => $model, 'attributes' => $attributes, 'mode' => 'view', 'bordered' => $bordered, 'striped' => $striped, 'condensed' => $condensed, 'responsive' => $responsive, 'hover' => $hover, 'hAlign'=>$hAlign, 'vAlign'=>$vAlign, 'fadeDelay'=>$fadeDelay, 'deleteOptions'=>[ // your ajax delete parameters 'params' => ['id' => 1000, 'kvdelete'=>true], ], 'container' => ['id'=>'kv-demo'], 'formOptions' => ['action' => Url::current(['#' => 'kv-demo'])] // your action to delete]);// Controller actionpublic function actionDetailViewDemo() { $model = new Demo; $post = Yii::$app->request->post(); // process ajax delete if (Yii::$app->request->isAjax && isset($post['kvdelete'])) { echo Json::encode([ 'success' => true, 'messages' => [ 'kv-detail-info' => 'The book # 1000 was successfully deleted. ' . Html::a(' Click here', ['/site/detail-view-demo'], ['class' => 'btn btn-sm btn-info']) . ' to proceed.' ] ]); return; } // return messages on update of record if ($model->load($post) && $model->save()) { Yii::$app->session->setFlash('kv-detail-success', 'Success Message'); Yii::$app->session->setFlash('kv-detail-warning', 'Warning Message'); } return $this->render('detail-view', ['model'=>$model]);}