gpt4 book ai didi

javascript - 在 Javascript 算法中重用 GWT DatePicker 的策略

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:51:02 26 4
gpt4 key购买 nike

在我的 GWT 应用程序中,我使用 Javascript 库为用户提供 SQL Where 字符串生成器功能 - 用于支持“高级搜索”。

javascript 资源目前只提供纯 html 日期文本字段。如果我使用纯 JS,我会合并许多第 3 方日期选择器库之一。

但是,我已经在客户端中安装了 GWT 日期编辑器(以支持其他 UI 功能)。

谁能推荐一种将 GWT 弹出式编辑器合并到我的遗留 javascript 中的策略?由于 GWT 编译器混淆,我认为我无法可靠地预测 GWT 日期编辑器组件类的名称。

我想这是从 GWT 推送配置或从 javascript 源中拉取配置之间的平衡。

干杯,伊恩

最佳答案

首先,在您的 html 中创建您希望日期选择器所在的位置,如下所示:

<span id="dateBox"/>

创建一个新的入口点,称为集成

package com.example.integration.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.datepicker.client.DateBox;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/

public class Integration implements EntryPoint {

@Override
public void onModuleLoad() {

//create date box
DateBox dateBox = new DateBox();

//set the value for the form submit
dateBox.getTextBox().getElement().setId("dateValueField");

//we need to override the default format
dateBox.setFormat(new DateBox.DefaultFormat() {

private DateTimeFormat dtf = DateTimeFormat.getFormat("MM/dd/yyyy");

@Override
public void reset(DateBox dateBox, boolean abandon) {
super.reset(dateBox, abandon);
}

@Override
public Date parse(DateBox dateBox, String text, boolean reportError) {
return super.parse(dateBox, text, reportError);
}

@Override
public String format(DateBox dateBox, Date date) {
if(date == null) return "";
return this.dtf.format(date);
}
});

//add to the span
RootPanel.get("dateBox").add(dateBox);
}
}

你应该把它放在一个新模块中,比如 com.example.integration.Integration.gwt.xml。

  <module rename-to='integration'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>

<inherits name='com.google.gwt.user.theme.clean.Clean'/>

<!-- Specify the app entry point class. -->
<entry-point class='com.example.integration.client.Integration'/>

<!-- Specify the paths for translatable code -->
<source path='client'/>
<source path='shared'/>
</module>

现在您已完成此操作,您需要执行标准的 GWT 操作以将编译后的代码添加到您的 HTML 中。您的最终 HTML 应该类似于:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<link type="text/css" rel="stylesheet" href="Integration.css">
<title>Web Application Starter Project</title>

<script type="text/javascript" language="javascript" src="integration/integration.nocache.js"></script>
</head>

<body>

<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>

<!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled
in order for this application to display correctly.
</div>
</noscript>

<form id="form">
<!-- old style date including formatting javascript and all the old cruft -->
Old Style Date:<input type="text" id="sometext" value="MM/DD/YYYY"/><br/>

<!-- new style. substitute your own styles and whatnot to make it not strange -->
New Hotness:<span id="dateBox"/>

<!-- you can submit this form as it is and it will just work (tm) -->
</form>
</body>
</html>

现在您的表单中将有一个表单项(文本输入框),其 ID 为“dateValueField”。这将像常规表单元素一样提交。

因此,这些建议中的一些应该能够帮助您上路。祝你好运。

注意,有一些更小、更简单的 javascript 库(包括一些 jQuery 的东西)可以更容易地做到这一点。

关于javascript - 在 Javascript 算法中重用 GWT DatePicker 的策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6111547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com