当前位置: 首页 > 知识库问答 >
问题:

为Vaadin 8 Grid实现列渲染器

童华池
2023-03-14

Vaadin框架指南有一页描述了如何在Vaadin网格中使用列渲染器。这一页描述了实现渲染器,但太简短了。

我想实现一个InstantRenler来补充Vaadin 8.1中添加的部分java.time渲染器集。为LocalDate添加了渲染器

因此,我的代码应该与提供的LocalDateTimeRenderer非常相似。我正试图遵循该准则作为指导。

在搜索Vaadin源代码和阅读文档时,我似乎需要三段源代码:

  • InstantRenderer(与LocalDateTimeRenderer类似)

我已经这样做了,它都编译了。但是我的表无法渲染,我得到的只是页面上的一个白色空框。控制台或日志上没有出现错误。如果我删除对我的InstantRenler的使用,并回退到让我的Instant对象通过它们自己的toString方法的默认呈现,一切正常,表按预期显示。所以我知道我的自定义渲染器有问题。

谈到“服务器端”与“客户端”Vaadin,我是个新手。

我需要执行某种打包吗?目前,我的Vaadin项目中有三个类以及MyUI源文件。

我错过了其他部分吗?

我通过调用no-arg构造函数来实例化我的渲染器:

this.entriesGrid
    .addColumn( Entry::getStart )
    .setCaption( "Start" )
    .setRenderer( new InstantRenderer(  ) )
;

下面是我上面列出的三个文件,几乎完全取自Vaadin源代码。

/*
 * By Basil Bourque. Taken almost entirely from source code published by Vaadin Ltd.
 *
 * --------
 *
 * Copyright 2000-2016 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.basil.timepiece;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

import java.util.Locale;

import elemental.json.JsonValue;

/**
 * A renderer for presenting {@code Instant} objects.
 *
 * @author Vaadin Ltd
 * @since 8.1
 */
public class InstantRenderer
        extends com.vaadin.ui.renderers.AbstractRenderer< Object, Instant >
{
    private DateTimeFormatter formatter;
    private boolean getLocaleFromGrid;

    private ZoneId zoneId = ZoneId.systemDefault(); // Basil Bourque.

    /**
     * Creates a new InstantRenderer.
     * <p>
     * The renderer is configured to render with the grid's locale it is
     * attached to, with the format style being {@code FormatStyle.LONG} for the
     * date and {@code FormatStyle.SHORT} for time, with an empty string as its
     * null representation.
     *
     * @see <a href=
     * "https://docs.oracle.com/javase/8/docs/api/java/time/format/FormatStyle.html#LONG">
     * FormatStyle.LONG</a>
     * @see <a href=
     * "https://docs.oracle.com/javase/8/docs/api/java/time/format/FormatStyle.html#SHORT">
     * FormatStyle.SHORT</a>
     */
    public InstantRenderer ()
    {
        this( DateTimeFormatter.ofLocalizedDateTime( FormatStyle.LONG , FormatStyle.SHORT ) , "" );
        getLocaleFromGrid = true;
    }

    /**
     * Creates a new InstantRenderer.
     * <p>
     * The renderer is configured to render with the given formatter, with the
     * empty string as its null representation.
     *
     * @param formatter the formatter to use, not {@code null}
     * @throws IllegalArgumentException if formatter is null
     */
    public InstantRenderer ( DateTimeFormatter formatter )
    {
        this( formatter , "" );
    }

    /**
     * Creates a new InstantRenderer.
     * <p>
     * The renderer is configured to render with the given formatter.
     *
     * @param formatter          the formatter to use, not {@code null}
     * @param nullRepresentation the textual representation of the {@code null} value
     * @throws IllegalArgumentException if formatter is null
     */
    public InstantRenderer ( DateTimeFormatter formatter , String nullRepresentation )
    {
        super( Instant.class , nullRepresentation );

        if ( formatter == null )
        {
            throw new IllegalArgumentException( "formatter may not be null" );
        }

        this.formatter = formatter;
    }

    /**
     * Creates a new InstantRenderer.
     * <p>
     * The renderer is configured to render with the given string format, as
     * displayed in the grid's locale it is attached to, with an empty string as
     * its null representation.
     *
     * @param formatPattern the format pattern to format the date with, not {@code null}
     * @throws IllegalArgumentException if format pattern is null
     * @see <a href=
     * "https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns">
     * Format Pattern Syntax</a>
     */
    public InstantRenderer ( String formatPattern )
    {
        this( formatPattern , Locale.getDefault() );
        getLocaleFromGrid = true;
    }

    /**
     * Creates a new InstantRenderer.
     * <p>
     * The renderer is configured to render with the given string format, as
     * displayed in the given locale, with an empty string as its null
     * representation.
     *
     * @param formatPattern the format pattern to format the date with, not {@code null}
     * @param locale        the locale to use, not {@code null}
     * @throws IllegalArgumentException if format pattern is null
     * @throws IllegalArgumentException if locale is null
     * @see <a href=
     * "https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns">
     * Format Pattern Syntax</a>
     */
    public InstantRenderer ( String formatPattern , Locale locale )
    {
        this( formatPattern , locale , "" );
    }

    /**
     * Creates a new InstantRenderer.
     * <p>
     * The renderer is configured to render with the given string format, as
     * displayed in the given locale.
     *
     * @param formatPattern      the format pattern to format the date with, not {@code null}
     * @param locale             the locale to use, not {@code null}
     * @param nullRepresentation the textual representation of the {@code null} value
     * @throws IllegalArgumentException if format pattern is null
     * @throws IllegalArgumentException if locale is null
     * @see <a href=
     * "https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns">
     * Format Pattern Syntax</a>
     */
    public InstantRenderer ( String formatPattern , Locale locale , String nullRepresentation )
    {
        super( Instant.class , nullRepresentation );

        if ( formatPattern == null )
        {
            throw new IllegalArgumentException( "format pattern may not be null" );
        }

        if ( locale == null )
        {
            throw new IllegalArgumentException( "locale may not be null" );
        }

        formatter = DateTimeFormatter.ofPattern( formatPattern , locale );
    }

    @Override
    public JsonValue encode ( Instant value )
    {
        String dateString;
        if ( value == null )
        {
            dateString = this.getNullRepresentation();
        } else if ( this.getLocaleFromGrid )
        {
            if ( null == this.getParentGrid() )
            {
                throw new IllegalStateException(
                        "Could not find a locale to format with: "
                                + "this renderer should either be attached to a grid "
                                + "or constructed with locale information" );
            }
            ZonedDateTime zdt = value.atZone( this.zoneId );  // Basil Bourque.
            Locale locale = this.getParentGrid().getLocale();
            dateString = zdt.format( formatter.withLocale( locale ) );
        } else
        {
            ZonedDateTime zdt = value.atZone( this.zoneId );  // Basil Bourque.
            dateString = zdt.format( formatter );
        }
        return encode( dateString , String.class );
    }

    @Override
    protected InstantRendererState getState ()
    {
        InstantRendererState s = ( InstantRendererState ) super.getState();
        return s;
    }

    @Override
    protected InstantRendererState getState ( boolean markAsDirty )
    {
        InstantRendererState s = ( InstantRendererState ) super.getState( markAsDirty );
        return s;
    }
}
/*
 * By Basil Bourque. Taken almost entirely from source code published by Vaadin Ltd.
 *
 * --------
 *
 * Copyright 2000-2016 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.basil.timepiece;

import com.vaadin.shared.ui.Connect;

/**
 * A connector for InstantRenderer.
 * <p>
 * The server-side Renderer operates on {@code Instant}s, but the data is
 * serialized as a string, and displayed as-is on the client side. This is to be
 * able to support the server's locale.
 *
 * @author Vaadin Ltd
 * @since 8.1
 */
@Connect( InstantRenderer.class )
public class InstantRendererConnector extends com.vaadin.client.connectors.grid.TextRendererConnector
{

    @Override
    public InstantRendererState getState ()
    {
        return ( InstantRendererState ) super.getState();
    }
}
/*
 * By Basil Bourque. Taken almost entirely from source code published by Vaadin Ltd.
 *
 * --------
 *
 * Copyright 2000-2016 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.basil.timepiece;

/**
 * Shared state of InstantRenderer.
 *
 * @author Vaadin Ltd
 * @since 8.1
 */
public class InstantRendererState extends com.vaadin.shared.ui.grid.renderers.TextRendererState
{
    // This code intentionally left blank.
}

我已经在BitBucket上发布了我完整的Maven驱动的项目,以及用于即时、偏移日期时间和分区日期时间的列渲染器所需的所有文件。

我发布了问题#10208为其他java的Vaadin网格实现列渲染器。补充LocalDate的时间类型(Instant、OffsetDateTime、ZonedDateTime)

共有1个答案

元嘉木
2023-03-14

是的,需要特殊的打包。您不能简单地将Vaadin Grid列渲染器实现类扔进常规的Vaadin应用程序中。

列渲染器实现所需的三个类中有两个涉及客户端开发,而不是通常在Vaadin应用程序工作中进行的服务器端开发

幸运的是,这比听起来容易。为了只做一个简单的列渲染器,Vaadin幸运地提供了一些超类来完成大部分繁重的工作。因此,我们不需要了解在Vaadin中隐藏的GWT和JavaScript魔术的所有血腥细节。

通往成功的道路包括:

  • 使用Vaadin提供的模板创建一个单独的项目来构建您自己的Vaadin附加组件。
  • 使用取自Vaadin Framework GitHub项目的源代码填充该项目。

使用Vaadin团队提供的多模块Maven原型启动一个新项目:Vaadin原型小部件见此列表。

  • 渲染器类位于“addon”模块的主包中

当然,在实际工作中,您会删除原型创建的示例MyComponent...文件。

构建完成后,您可以通过导入“addon”模块的包,在“demo”模块的Vaadin应用程序中尝试列渲染器。在这种情况下:

import org.basilbourque.timecolrenderers.InstantRenderer;

我成功实现的Instant列渲染器完全取自Vaadin 8.1.3源代码提供的三个LocalDateTimeRenler相关类。您可以通过在GitHub查找文件功能中键入LocalDateTimeRenler来找到这些类的当前版本。

  • shared/src/main/java/com/vaadin/shared/ui/grid/renders/localdatetimerenderstate。java

 类似资料:
  • Mpx中的列表渲染与原生小程序中完全一致,详情可以查看这里 值得注意的是wx:key与Vue中的key属性的区别,不能使用数据绑定,只能传递普通字符串将数组item中的对应属性作为key,或者传入保留关键字*this将item本身作为key 下面是简单示例: <template> <!-- 使用数组中元素的 id属性/保留关键字*this 作为key值 --> <view wx:for=

  • 我想呈现一些 列,以便此列中的单元格看起来像特殊的小部件。 (s、等应存在于每个单元格中) 我将如何实现这些渲染器? 这个实现应该支持数千个表,并且在滚动表时不会闪烁。

  • v-for 我们用 v-for 指令根据一组数组的选项列表进行渲染。 v-for 指令需要以 item in items 形式的特殊语法, items 是源数据数组并且 item 是数组元素迭代的别名。 基本用法 <ul id="example-1"> <li v-for="item in items"> {{ item.message }} </li> </ul> var ex

  • 本文向大家介绍java基于OpenGL ES实现渲染实例,包括了java基于OpenGL ES实现渲染实例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了java基于OpenGL ES实现渲染的方法。分享给大家供大家参考。具体如下: 1. Run.java文件: 2. GlRenderer.java文件: 希望本文所述对大家的java程序设计有所帮助。

  • 我想使用P3D渲染器用PGraphics实例渲染基本的3D形状,而不使用任何别名/平滑,但是noslooth()似乎不起作用。 我记得在一个纹理上调用。 处理过程中的等价物是什么?

  • 图片