2017-08-01 100 views
0

我在我的GridView中有我的contentOptions,它具有該功能。但除此之外,我希望文本是文本對齊的 - 中心。我應該怎麼做?如何寫yii2的contentOptions有功能和風格?

這裏是我的內容選項

'contentOptions' => function (Service $model) { 
    $services = Service::getServices($model->services); 
    if (BaseStringHelper::countWords($services) < 5) { 
     return $services; 
    } 
    return null; 
} 

回答

2

您可以使用引導程序的類此:

return ['title' => $allPurposes, 'class' => 'text-center']; 

,或者如果您不使用引導程序就可以使用純CSS:

return ['title' => $allPurposes, 'style' => 'text-align:center']; 
+0

感謝。你能否告訴我如何做到這一點,沒有回報?我更新了我的代碼。我也需要這樣做,以「價值」。 – MKD

+1

基本上同樣的東西 - 只是單獨的類 - 'return ['class'=>'text-center'];' – Bizley

+0

不知何故我得到一個錯誤。我已經更新了我的回答,我如何將它寫入'value' – MKD

1

試試:

'contentOptions' => function (Client $model) { 
    $allPurposes = ClientController::getClientIncomeSourceTitles($model->incomeSourceIdArray); 
    if (BaseStringHelper::countWords($allPurposes) > 2) { 
     return ['title' => $allPurposes, 'style' => 'text-align:center']; 
    } 
    return null; 
} 

對於rowOptions:

'rowOptions' => function ($model, $index, $widget, $grid){ 
     return [ 'style'=>'text-align: center;' ]; 
    }, 
+0

我已將您的rowOptions替換爲我的,但這也不起作用。它說,我設置未知的屬性 – MKD

+0

你可以顯示你的完整gridview代碼.. rowOptions是一個gridview屬性。我假設你將它添加到列數組。 – Chinmay

+0

我的代碼已更新 – MKD