Yii2-GridView 中让关联字段带搜索和排序功能 [ 2.0 版本 ]
情境要求:
要在订单(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_name; //<=====就是加在这里
}
第二步:
修改OrderSearch中的search函数
public function search($params)
{
$query = Order::find();
$query->joinWith(['customer']);<=====加入这句
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
/* 其它字段不要动 */
/* 下面这段是加入的 */
/*=============*/
'customer_name' => [
'asc' => ['customer.customer_name' => SORT_ASC],
'desc' => ['customer.customer_name' => SORT_DESC],
'label' => 'Customer Name'
],
/*=============*/
]
]);
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_name', $this->customer_name]) ;//<=====加入这句
return $dataProvider;
}
第三步:
修改order/index视图的gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'customer_id',
'status',
['label'=>'客户', 'attribute' => 'customer_name', 'value' => 'customer.customer_name' ],//<=====加入这句
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
好了就这样,希望对你有帮助。
桃篮翁
注册时间:2014-10-23
最后登录:2019-12-12
在线时长:5小时16分
最后登录:2019-12-12
在线时长:5小时16分
- 粉丝9
- 金钱43
- 威望30
- 积分393
共 21 条评论
不错。 正好最近在学习2!
艾玛,前几天在群里问,没人理我,这就有教程了!
试了下,不出现搜索框,
修改
['label'=>'客户', 'attribute' => 'customer_name', 'value' => 'customer.customer_name' ]
为
['label'=>'客户', 'attribute' => 'customer_name', 'value' => 'customer.customer_name', ,'filter'=>Html::activeTextInput($searchModel, 'customer_name',['class'=>'form-control']) ]
现在搜索框出现了,但是输入后还是不能查询出结果,然后修改gii生成的那个搜索模型文件有个rules方法里将 customer_name 属性加入到安全搜索字段中
['customer_name','safe]
这样就可以实现搜索了你的实现了吗?我的搜索没有反应
@songyichao
@songyichao 你实现了吗?我这个搜索也不启作用。
@MrHao 实现了
@MrHao
[
'attribute' => 'name', 'filter' => Html::activeTextInput($model, 'name', [ 'class' => 'form-control', 'id' => null ]), 'label' => 'name', ],
搜索没问题了,可为啥'客户'字是黑色的,列头单击不能排序?
搞定了,加上这段
OK了!
你好,setSort的参数怎么写?
您好,搜索可以了,排序不行,要怎么写?
@涛哥 $dataProvider->setSort([
'attributes' => [ 'customer_name' => [ 'asc' => ['customer.customer_name' => SORT_ASC], 'desc' => ['customer.customer_name' => SORT_DESC], 'label' => 'Customer Name' ], ]
]);
'customer.customer_name'这玩意要是1或者0 怎么把它变成是或者否啊
呃(⊙o⊙)… 同求
return $model->customer->customer_name==0?'否':'是'
关联表后,两个表都有同一个字段,该如何区分呢
表1的id直接使用
id
,表2(关联的表)的id使用表2.id
那你知道怎么给时间搜索时间的那个文本框添加时间插件吗?让它点击之后就出现时间表
你好!我想请问一下我就有个id字段想在页面上的表头点击进行排序,view里面怎么写?出现点击链接?
->andFilterWhere(['like', 'yii_category.name', $this->cate_name]);
查询表换的加前缀啊!
我按你说的走咋到这里就报错了啊?$query->joinWith(['customer']);<=====加入这句
报错内容 : Caused by: Unknown Method – yii\base\UnknownMethodException
Calling unknown method: app\models\Bug::get{{%user}}()
$dataProvider->setSort([
'attributes' => [ /* 其它字段不要动 */ /* 下面这段是加入的 */ /*=============*/ 'customer_name' => [ 'asc' => ['customer.customer_name' => SORT_ASC], 'desc' => ['customer.customer_name' => SORT_DESC], 'label' => 'Customer Name' ], /*=============*/ ] ]);
这个label干嘛用的
'label' => 'Customer Name'
页面里表格的字段名
我有一个脚本表, 一个设备表, 脚本表里每一个脚本对应多台设备。我想在脚本的gridview里得到设备的数量。并且可以排序就好了,不需要查询。求指点啊。
我只想查出某个客户的订单,怎么写?
搜索不起作用,求楼主怎么破?
'label' => 'Customer Name' 这个的实际作用是什么?我看view的columns中也有label
搜索不起作用需要在验证规则中将对应的字段设置为安全的
`
public function rules()
{ return [ [['id', 'status'], 'integer'], [['username', 'realname'], 'safe'], // 这行为我需要增加的搜索字段 ]; }
`
view层还需要注意一点
['label'=>'客户', 'attribute' => 'customer_name', 'value' => 'customer.customer_name' ],//<=====加入这句
attribute 要在model里面设置好,如果直接写中文不能排序和搜索
['class' => 'yii\grid\ActionColumn'] 这个干嘛的
/** * @return \yii\db\ActiveQuery */ public function getUser() { return $this->hasOne(UserProfile::className(), ['user_id' => 'user_id']); }
class InquiryRecordSearch extends InquiryRecord
{
public $mobile; public $nickname; /** * @inheritdoc */ public function rules() { return [ [['id', 'status', 'create_time', 'car_models_id', 'user_id'], 'integer'], [['order_id'], 'string'], [['mobile','nickname'],'safe'], ]; } /** * @inheritdoc */ public function scenarios() { // bypass scenarios() implementation in the parent class return Model::scenarios(); } /** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = InquiryRecord::find(); // add conditions that should always apply here $query->joinWith(['user']); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'defaultPageSize'=>Yii::$app->params['defaultPageSize'] ], 'sort'=>[ 'defaultOrder' => [ 'id' => SORT_DESC ] ], ]);
$dataProvider->setSort([ 'attributes' => [ /* 其它字段不要动 */ /* 下面这段是加入的 */ /*=============*/ 'mobile' => [ 'asc' => ['mobile' => SORT_ASC], 'desc' => ['mobile' => SORT_DESC], 'label' => 'Mobile' ], 'nickname' => [ 'asc' => ['nickname' => SORT_ASC], 'desc' => ['nickname' => SORT_DESC], 'label' => 'Nickname' ], /*=============*/ ] ]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere([ 'id' => $this->id, 'order_id' => $this->order_id, 'status' => $this->status, 'create_time' => $this->create_time, 'car_models_id' => $this->car_models_id, 'user_id' => $this->user_id, ]); $query->andFilterWhere(['like', 'mobile', $this->mobile]); $query->andFilterWhere(['like', 'nickname', $this->nickname]); return $dataProvider; }
}
GridView::widget([
'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ //['class' => 'yii\grid\SerialColumn'], //'id', 'order_id', [ 'attribute' => 'nickanme', 'label' => '申请人', 'value' => 'user.nickname', 'filter' => Html::activeTextInput($searchModel, 'nickname', [ 'class' => 'form-control' ]), ], [ 'attribute' => 'mobile', 'label' => '联系电话', 'value' => 'user.mobile', 'filter' => Html::activeTextInput($searchModel, 'mobile', [ 'class' => 'form-control' ]), ],