服务器数据库更改后自动刷新yii2网格

时间:2016-10-03 12:23:05

标签: yii yii2

我有一个涉及卡车验证的网格。我想在没有页面刷新的情况下更改网格的内容。

目前,当页面刷新时,这可以正常工作,但我希望它能够在页面未刷新时工作,即内容在服务器数据库中发生变化时。

这是网格

<?php

$gridColumns = [
 ['class' => 'kartik\grid\SerialColumn'],
  'reg_no',
 [
    'attribute'=>'truck_category',
    'value'=>'truckCategory.category'
 ],

 [
    'class' => 'kartik\grid\ActionColumn',
    'vAlign'=>'middle',
    'urlCreator' => function($action, $model, $key, $index) { return '#'; },
    'viewOptions'=>['title'=>"vdgdv", 'data-toggle'=>'tooltip'],
    'updateOptions'=>['title'=>"update", 'data-toggle'=>'tooltip'],
    'deleteOptions'=>['title'=>"delete", 'data-toggle'=>'tooltip'],
   ]
];


echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'containerOptions' => ['style'=>'overflow: auto'], // only set when $responsive = false
'beforeHeader'=>[
    [
       'options'=>['class'=>'skip-export'] // remove this row from export
    ]
],
'toolbar' =>  [
    [],
    '{export}',
    '{toggleData}'
],
'pjax' => true,
'bordered' => true,
'striped' => false,
'condensed' => false,
'responsive' => true,
'hover' => true,
'floatHeader' => true,
'showPageSummary' => true,
'panel' => [
    'type' => GridView::TYPE_PRIMARY
],

]); ?&GT;

这是渲染网格的控制器

public function actionTrackingcenter()
{
    $query = Truck::find()->where(['truck_status'=>5]);
    $searchModel = new TruckSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams,$query);

    return $this->render('trackingcenter/index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);

}   

有人可以指示如何实现自动更改

1 个答案:

答案 0 :(得分:1)

您可以在下面使用set-time-interval with jQuery

public class ColorListControl : CheckedListBox
{
    public event EventHandler SelectedColorChanged;

    private Color selectedColor = Color.None;

    [DefaultValue( Color.None )]
    public Color SelectedColor
    {
        get { return selectedColor; }
        set
        {
            if( selectedColor != value )
            {
                selectedColor = value;

                for( int i = 0; i < this.Items.Count; i++ )
                {
                    Color itemColor = (Color)this.Items[i];
                    if( itemColor == Color.None )
                        this.SetItemChecked( i, value == Color.None );
                    else
                        this.SetItemChecked( i, value.HasFlag( itemColor ) );
                }

                SelectedColorChanged?.Invoke( this, EventArgs.Empty );
            }
        }
    }

    public ColorListControl()
    {
        CheckOnClick = true;

        foreach( Color value in Enum.GetValues( typeof( Color ) ) )
            this.Items.Add( value );
    }

    protected override void OnItemCheck( ItemCheckEventArgs ice )
    {
        base.OnItemCheck( ice );

        Color checkedColor = (Color)this.Items[ice.Index];

        if( ice.NewValue == CheckState.Checked )
            SelectedColor |= checkedColor;
        else
            SelectedColor &= ~checkedColor;
    }
}

此代码将放在GridView代码

之后

这将每10秒重新加载一次Pjax网格