如何在gridview中增加一行记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在gridview中增加一行记录相关的知识,希望对你有一定的参考价值。

在不使用detalsView等控件的前提下,只在gridview中添加一行记录~~像删除编辑功能那样可以随便在选定行的下一行添加一行记录~~如何实现!~~希望能发完整的源代码~~邮箱:hongyu-1314@163.com。如果满意会追加分数~谢谢啦~~在线等~~

1 、gridview的datasource可能是一个DataTable,可以点击“新增行”按钮的时候,先把DataTable在多加一行DataRow,绑定好,这样gridview的datasource就是多出一行空数据的DataTable。
2 、绑定后,调用RowEditing事件,设置gridview的EditIndex,这样就可以在新增的那行中显示文本框了,以供用户编辑数据。
参考技术A 这个问题我遇到过,一般网上都是用模板列什么的来解决,但是比较麻烦。
思路是这样:
1 gridview的datasource可能是一个DataTable,可以点击“新增行”按钮的时候,先把DataTable在多加一行DataRow,绑定好,这样gridview的datasource就是多出一行空数据的DataTable。
2 绑定后,调用RowEditing事件,设置gridview的EditIndex,这样就可以在新增的那行中显示文本框了,以供用户编辑数据。本回答被提问者和网友采纳
参考技术B 用dataset绑定空数据的方法有个不实用的地方,就是当页面有其他编辑按钮时一刷新页面ds也会刷新,那样新加行就没有了,gridview如果有编辑按钮 同样也会刷掉,除非你都用ajax方法做整个页面。
建议直接点击那个按钮时,直接向数据库插入一条空数据,再读出来就是真正的增加一新行了 可以通过设定新行的 id 决定所在位置
参考技术C 绑定数据库不行么..

如何在yii2 gridview中为大数据表显示一行中的表头名称

我在gridview中有一个表,它有很多列。现在我已经增加了列宽以调整标题名称,但它没有用,标题仍然显示在我用过的两行中:

'contentOptions' =>['style' => 'width:30px'], 

调整列宽。有没有办法增加整个gridview的宽度,以便我可以增加列宽并使标题显示在一行?这是我的gridview代码:

<div class="listing-width">
                <?php Pjax::begin(); ?>
                <?=
                GridView::widget([

                    'dataProvider' => $dataProvider,
                    //       / 'filterModel' => $searchModel,
                    'layout' => "
{items}
<div class='row'><div class='text-xs-left col-md-6'>{summary}</div><div class='text-xs-right col-md-6'>{pager}</div></div>",
                    'pager' => array(
                        'options' => ['class' => 'pagination'],
                        'prevPageLabel' => '&lt',
                        'nextPageLabel' => '&gt',
                        'firstPageLabel' => '&laquo',
                        'lastPageLabel' => '&raquo',
                        'maxButtonCount' => 3, // Set maximum number of page buttons that can be displayed
                    ),
                    'tableOptions' => [
                        //'id' => 'exampleFooAddRemove',

                        'class' => 'table table-bordered table-hover toggle-circle ',
                        'data-page-size' => '7',
                    ],
                    'rowOptions' => function ($model, $key, $index, $grid) {
                return [
                    'style' => "display: table-row;",
                    'class' => '',];
            },

                    'columns' => [
//                        ['class' => 'yiigridSerialColumn',
//                            'header' => 'S.No.',
////                            'headerOptions' => [ 'data-hide'=>'phone, tablet'],
//                            'contentOptions' => ['class' => 'footable-visible footable-first-column'],
//                        ],

                        ['attribute' => 'id',
                            'format' => 'html',
                            'label' => 'Loan ID',
                           'contentOptions' =>['style' => 'width:30px'],


                           'headerOptions' => [ 'data-hide'=>'phone, tablet'],
//                            'contentOptions' => ['class' => 'footable-visible'],

                            'value' => function ($model) {
                        return Html::a($model->id, ['loans/view', 'id' => $model->id]);
                    }],
                              ['attribute' => 'SSN',
                            'format' => 'html',
                            'label' => 'SSN',
                            'headerOptions' => [ 'data-hide'=>'phone, tablet'],
                                    'contentOptions' =>['style' => 'width:30px'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                            'value' => function ($model) {
                        return Html::a($model->id, ['loans/view', 'id' => $model->id]);
                    }],
                        ['label' => 'Date Created',
                            'class' => 'yiigridDataColumn',
                            'attribute' => 'date_created',
                            'headerOptions' => ['class' => 'footable-visible footable-sortable footable-sorted'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                              'contentOptions' =>['style' => 'width:30px'],
                             'value' => function($model) {
                                    return AppConstants::date_format($model->date_created);
                            }
                        ],
                        ['attribute' => 'first_name',
                            'label' => 'First Name',
                            'headerOptions' => [ 'data-hide'=>'phone'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                              'contentOptions' =>['style' => 'width:30px'],
                            'format' => 'html',
                            'value' => function ($model) {
                            if($model->customer)
                            return Html::a(ucfirst($model->customer->first_name), ['customers/view', 'id' => $model->customer->id]);
                    }],
                        ['attribute' => 'last_name',
                            'format' => 'html',
                            'headerOptions' => [ 'data-hide'=>'phone'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                              'contentOptions' =>['style' => 'width:30px'],
                            'label' => 'Last Name',
                            'value' => function ($model) {
                            if($model->customer)
                            return Html::a(ucfirst($model->customer->last_name), ['customers/view', 'id' => $model->customer->id]);
                    }],
//                        ['attribute' => 'ssn',
//                            'label' => 'Last Name',
//                            'headerOptions' => ['class' => 'footable-visible footable-sortable footable-sorted'],
//                            'contentOptions' => ['class' => 'footable-visible'],
//                            'value' => function ($model) {
//                        return $model->customer->ssn;
//                    }],
                        ['attribute' => 'email_address',
                            'headerOptions' => [ 'data-hide'=>'phone'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                              'contentOptions' =>['style' => 'width:30px'],
                            'label' => 'Email Address',
                            'value' => function ($model) {
                           if($model->customer)
                            return $model->customer->email_address;
                    }],
                        ['attribute' => 'store_name',
                            'label' => 'Store Name',
                            'headerOptions' => [ 'data-hide'=>'phone, tablet'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                              'contentOptions' =>['style' => 'width:30px'],
                            'format' => 'html',
                            'value' => function ($model) {
                        return @Html::a($model->store->store_name, ['stores/view', 'id' => $model->store->id]);
                    }],
                        ['attribute' => 'program_name',
                            'label' => 'Program Name',
                            'format' => 'html',
                            'headerOptions' => [ 'data-hide'=>'phone, tablet'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                              'contentOptions' =>['style' => 'width:30px'],
                            'value' => function ($model) {
                        return @Html::a($model->interestProgram->program_name, ['programs/view', 'id' => $model->interestProgram->id]);
                    }],
                        ['attribute' => 'principle_amount',
                            'headerOptions' => [ 'data-hide'=>'phone, tablet'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                              'contentOptions' =>['style' => 'width:30px'],
                            'label' => 'Principal Amount',
                            'value' => function ($model) {
                         return '$'.  AppConstants::amount_format($model->principle_amount);
                    }],
                        ['attribute' => 'total_amount',
                            'headerOptions' => [ 'data-hide'=>'phone, tablet'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                              'contentOptions' =>['style' => 'width:30px'],
                            'label' => 'Total Amount',
                            'value' => function ($model) {
                        return '$' . AppConstants::amount_format($model->total_amount);
                    }],
//                        ['label' => 'Due Date',
//                            'class' => 'yiigridDataColumn',
//                            'attribute' => 'due_date',
//                            'headerOptions' => ['class' => 'footable-visible footable-sortable footable-sorted'],
//                            'contentOptions' => ['class' => 'footable-visible'],
//                             'value' => function($model) {
//                                    return AppConstants::date_format($model->due_date);
//                            }
//                        ],

                        ['label' => 'Effective Date',
                            'class' => 'yiigridDataColumn',
                            'attribute' => 'effective_date',
                            'headerOptions' => [ 'data-hide'=>'phone, tablet'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                              'contentOptions' =>['style' => 'width:30px'],
                            'value' => function($model) {
                        return AppConstants::date_format($model->effective_date);
                    }
                        ],
                        ['attribute' => 'loan_status',
                            'value' => function($model) {
                                return '<span class="tag tag-table '.Yii::$app->appconstants->loanColor[$model->loan_status].' ">'.Yii::$app->appconstants->loanStatus[$model->loan_status].'</span>';
                            },
                            'label' => 'Loan Status',
                            'headerOptions' => [ 'data-hide'=>'phone, tablet'],
//                            'contentOptions' => ['class' => 'footable-visible'],
                                      'contentOptions' =>['style' => 'width:30px'],
                            'format' => 'html'        
                        ],
                        ['class' => 'yiigridActionColumn',
                            'template' => '{view} {delete}',
                            'header' => 'Actions',
                            'class' => 'yiigridActionColumn',
                        ],


                    ],
                ]);
                ?>
                <?php Pjax::end(); ?>
                  </div> 

他们现在正在展示enter image description here

答案

在css文件中:

.grid-view {
    width: ...px !important;
    overflow-x: scroll !important;
}
另一答案

找到解决方案:

你必须围绕“Gridview”创建一个div给该div赋值类,例如:class = listing-width

在该div上应用以下css:

.listing-width {
   overflow-x: scroll;
}
.listing-width  table{
   max-width: ....px;
    width: ....px;
}

然后为每列的标题指定宽度:

'headerOptions' => [ 'style'=>'width: ....px']

这将解决您的问题,对于包含一行中的“标题”和水平滚动条的大量列的表。

另一答案

试试这个:-)在网格中

'headerOptions' => ['style'=>'white-space:nowrap']

[或者]在CSS中

.grid-view th {
    white-space: nowrap;
}

以上是关于如何在gridview中增加一行记录的主要内容,如果未能解决你的问题,请参考以下文章

无记录时显示gridview表头,并增加一行显示“没有记录”绑定SqlDataSource控件时

android如何删除gridview中的某个item

如何随机着色gridview(每行随机颜色)

Gridview中的JQuery如何在文本框在同一行更改时检查复选框

在GRIDVIEW控件中如何选中一行,然后按选中那行的数据调入到另外一个窗口?

如何更改 DevExpress GridView 中一行的背景色?