yii2 gridview:widget 能不能传一个自定义的参数
2个回答
展开全部
1,使用sqldatasources控件来关联gridview,只需设置一下属性,没上面代码就可以实现显示数据。 2,查询语句查询返回一个数据集,然后赋值给gridview的datasource属性,绑定即可。 剩余的就是gridview的设置了,多点击里面的东西看看试试即知道了。.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-09-16
展开全部
情境要求:
要在订单(Order)视图的gridview中显示出客户(Customer)状态,并使其具有与其它字段相同的排序和搜索功能。
数据库结构
订单表order含有字段customer_id 与 客户表customer的id字段关联
首先确保在Order Model中包含以下代码:
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
}
用gii会自动生成此代码;
第一步:
在OrderSearch添加一个$customer_name变量
class OrderSearch extends Order
{
public $customer_state; //<=====就是加在这里
}
第二步:
修改OrderSearch中的search函数
public function search($params)
{
$query = Order::find();
$query->joinWith(['customer']);<=====加入这句
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
/* 其它字段不要动 */
/* 下面这段是加入的 */
/*=============*/
'customer_state' => [
'asc' => ['customer.customer_state' => SORT_ASC],
'desc' => ['customer.customer_state' => SORT_DESC],
'label' => 'Customer State'
],
/*=============*/
]
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'user_id' => $this->user_id,
'customer_id' => $this->customer_id,
'order_time' => $this->order_time,
'pay_time' => $this->pay_time,
]);
$query->andFilterWhere(['like', 'status', $this->status]);
$query->andFilterWhere(['like', 'customer.customer_state', $this->customer_state]) ;//<=====加入这句
return $dataProvider;
}
第三步:
修改order/index视图的gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'customer_id',
'status',
['label'=>'客户', 'attribute' => 'customer_state', 'value' => 'customer.customer_state' ],//<=====加入这句
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
要在订单(Order)视图的gridview中显示出客户(Customer)状态,并使其具有与其它字段相同的排序和搜索功能。
数据库结构
订单表order含有字段customer_id 与 客户表customer的id字段关联
首先确保在Order Model中包含以下代码:
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
}
用gii会自动生成此代码;
第一步:
在OrderSearch添加一个$customer_name变量
class OrderSearch extends Order
{
public $customer_state; //<=====就是加在这里
}
第二步:
修改OrderSearch中的search函数
public function search($params)
{
$query = Order::find();
$query->joinWith(['customer']);<=====加入这句
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
/* 其它字段不要动 */
/* 下面这段是加入的 */
/*=============*/
'customer_state' => [
'asc' => ['customer.customer_state' => SORT_ASC],
'desc' => ['customer.customer_state' => SORT_DESC],
'label' => 'Customer State'
],
/*=============*/
]
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'user_id' => $this->user_id,
'customer_id' => $this->customer_id,
'order_time' => $this->order_time,
'pay_time' => $this->pay_time,
]);
$query->andFilterWhere(['like', 'status', $this->status]);
$query->andFilterWhere(['like', 'customer.customer_state', $this->customer_state]) ;//<=====加入这句
return $dataProvider;
}
第三步:
修改order/index视图的gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'customer_id',
'status',
['label'=>'客户', 'attribute' => 'customer_state', 'value' => 'customer.customer_state' ],//<=====加入这句
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询