Yii2比较并突出显示gridview中两个字符串或文本之间的差异

问题描述 投票:-2回答:1

有没有办法让它发挥作用?我一直在寻找解决方案,但没有找到相关的内容。如果StringHelper有这样的方法会很好!在yii2中某处必须有一个文本差异函数,因为在gii中还有一个突出的差异,不是吗? DiffRendererHtmlInline?这是什么?它来自gii。我们可以以某种方式使用它吗?

https://github.com/pdjshog/yii2/blob/master/framework/gii/components/Pear/Text/Diff.php

它应该在那里但我在Yii中找不到它。事实上,yii框架中还有其他一些:

  • phpspec / PHP-差异/ LIB / Diff.php

看起来很不错。我们能以某种方式使用它吗?

string yii2 highlight difference
1个回答
0
投票

我们可以。它并不完美,但我可以根据自己的需要稍微改变一下。

https://github.com/chrisboulton/php-diff/blob/master/example/example.php

所以它已经存在于yii中,你不需要安装任何东西。

我已经将以下内容添加到index.php中:

require_once \Yii::$app->basePath . '/vendor/phpspec/php-diff/lib/Diff.php';
require_once \Yii::$app->basePath . '/vendor/phpspec/php-diff/lib/Diff/Renderer/Html/InlineMy.php';

好吧唯一的问题是没有一个输出对我来说真的很完美:文本渲染太少,html渲染是完整的表格,而在gridview中它不是很好,所以我已经将Inline.php的副本作为InlineMy.php并删除了我不需要的所有内容:

public function render() {
    $changes = parent::render();
    $html = '';
    if (empty($changes)) {
        return $html;
    }

    foreach ($changes as $i => $blocks) {
        foreach ($blocks as $change) {
            if ($change['tag'] == 'replace') {
                foreach ($change['base']['lines'] as $no => $line) {
                    $html .= '<span style="white-space: nowrap">' . $line . '</span><br>';
                }

                foreach ($change['changed']['lines'] as $no => $line) {
                    $html .= '<span style="white-space: nowrap">' . $line . '</span>';
                }
            }
        }
    }
    return $html;
}

此外,我在父(Array.php)中更改了这个:

<del> ==> <del style="background-color: red">
<ins> ==> <ins style="background-color: green">

我现在正在使用SqlDataProvider,所以我的gridview看起来与平常略有不同(ActiveDataProvider):

[
    'attribute' => 'diff',
    'value' => function ($row) {
        $diff = new Diff(explode("\n", $row["name"]), explode("\n", $row["name2"]));
        $renderer = new Diff_Renderer_Html_Inline;
        return $diff->render($renderer);
    },
    'format' => 'html',
],

输出:

一些 事情 <==红色

有点<==实际上是下划线和绿色,而不是粗体

我相信一个人可以让它变得更优雅,但是对于我而言,这对我来说很好,因为我不想再建造它。

© www.soinside.com 2019 - 2024. All rights reserved.