如何在函数中使用 $categoryId
how can use $categoryId in function
在这里,我在 yii2 GridView
中生成 动态列
$gridColumns = [];
$gridColumns[] = [
'class' => 'yii\grid\SerialColumn',
'contentOptions' => ['width' => 10],
];
foreach ($CategoryList as $categoryId => $categoryName) {
$gridColumns[] = [
'label' => $categoryName,
'value' => function($model) {
return $categoryId; <---- categoryId use in function
}
];
}
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
]);
如何在gridView闭包函数中使用categorId
每列都有唯一的 $categoryId,例如 1,2,3 循环
使用匿名函数use()
$gridColumns[] = [
'label' => $categoryName,
'value' => function($model) use ($categoryId) {
return $categoryId;
}
];
foreach ($CategoryList as $categoryId => $categoryName) {
$categoryIdVariable = "$categoryId"; <---- store value in a variable
$$categoryIdVariable = $categoryIdVariable; <---- store variable in another variable
$gridColumns[] = [
'label' => $categoryName,
'value' => function($model) use($categoryIdVariable) { <---- and use it like a string variable
$categoryId = $categoryIdVariable;
return $categoryId; <---- dynamic categoryId use in function
}
];
}
在这里,我在 yii2 GridView
中生成 动态列$gridColumns = [];
$gridColumns[] = [
'class' => 'yii\grid\SerialColumn',
'contentOptions' => ['width' => 10],
];
foreach ($CategoryList as $categoryId => $categoryName) {
$gridColumns[] = [
'label' => $categoryName,
'value' => function($model) {
return $categoryId; <---- categoryId use in function
}
];
}
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
]);
如何在gridView闭包函数中使用categorId
每列都有唯一的 $categoryId,例如 1,2,3 循环
使用匿名函数use()
$gridColumns[] = [
'label' => $categoryName,
'value' => function($model) use ($categoryId) {
return $categoryId;
}
];
foreach ($CategoryList as $categoryId => $categoryName) {
$categoryIdVariable = "$categoryId"; <---- store value in a variable
$$categoryIdVariable = $categoryIdVariable; <---- store variable in another variable
$gridColumns[] = [
'label' => $categoryName,
'value' => function($model) use($categoryIdVariable) { <---- and use it like a string variable
$categoryId = $categoryIdVariable;
return $categoryId; <---- dynamic categoryId use in function
}
];
}