gridview - 如何截断特定 Yii2 GridView 列中的文本并在悬停时显示它?

标签 gridview twitter-bootstrap-3 yii2 hover truncate

我使用 GridView 小部件渲染表格:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,   
    'columns' => [
        'subject',
        // ...
    ],
]) ?>

我需要截断“主题”列中显示的文本并在悬停时完全显示它,同时保留过滤器行搜索功能。

我设法使用 StringHelper::TruncateWords() 截断文本,但无法找出过滤器行和悬停部分:

[
            'attribute' => 'subject',
            'value' => function($model) {
                $ret = \yii\helpers\StringHelper::truncateWords($model->subject, 5, '...', false);
                return $ret;
            }
],

也许有一种方法可以使用纯 Bootstrap 来完成此操作,而不需要 StringHelper,但我无法使其工作......

最佳答案

实际上有更简单的解决方案。您可以使用纯 CSS 来执行此操作。

.truncate {
   max-width: 150px !important;
   overflow: hidden;
   white-space: nowrap;
   text-overflow: ellipsis;
}

.truncate:hover{
   overflow: visible;
   white-space: normal;
   width: auto;
}

并在 View 中添加类:

[
  'attribute' => 'subject',
  'contentOptions' => ['class' => 'truncate'],
],

根据需要调整最大宽度,通过 css3 添加其他效果,就完成了。

关于gridview - 如何截断特定 Yii2 GridView 列中的文本并在悬停时显示它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43630036/

相关文章:

html - Twitter-bootstrap 3. 在 Opera 中悬停后,一行表格失去边框

php - Yii2 翻译存储在数据库中的数据

php - Yii2 - 错误请求 (#400) 无法验证您的数据提交

c# - asp.net 上的全局化问题 : it's not updating accentuated words

c# - 如何访问 Web 窗体中 GridView 模板字段的属性

c# - 窗体 View 被清除

html - Bootstrap Jumbotron 不会变成整个 body 宽度

c# - Gridview 中的 CommandArgument

javascript - 引导问题

php - Yii2:如何创建自定义删除按钮?