yui,表单提交和数据表
我是一名 Java 程序员,但不是 JavaScript 程序员。 我刚刚发现了 YUI,并正在尝试开始使用它。 我想要尝试做的是将查询表单放在页面顶部,用户按“提交”,结果会显示在查询表单下方的 YUI 数据表中。
当然,通常情况下,在 HTML 表单上,用户按“提交”,请求将发送到服务器,我使用 Struts 来处理它,然后将请求转发到 JSP,并将 HTML 发送回浏览器。 这就是我每天所做的事情。 对于 Ajax,我知道它的不同之处在于我应该返回 XML。 不是问题。 容易做。
我的问题涉及 YUI 方面的问题。 当按下“提交”按钮时,会发生什么? 不是正常的表单提交,对吗? 我是否实现了一个 onSubmit() JavaScript 函数,然后触发一些 YUI DataSource 来获取数据? 请求参数如何传递? 希望我不必手动构建请求。 我猜我使用的是 YAHOO.util.XHRDataSource,这是数据表的数据源。
我已经设法使用 HTML 表格元素让 YUI DataTable 工作。 现在我只需要把它切换到真实数据即可。 奇怪的是,YUI 文档似乎有点薄弱,除非我遗漏了一些东西。 也许这只是 YUI 文档没有涵盖的基本 Ajax?
I'm a Java programmer, but not a JavaScript programmer. I have just discovered YUI, and am trying to get started using it. What I'd like to try and do is have the query form at the top of the page, the user press Submit and the results show up in a YUI DataTable below the query form.
Normally, of course, on a HTML form the user presses Submit, the request gets sent to the server, and I use Struts to process it and then forward the request to a JSP and HTML gets sent back to the browser. That's what I do on a daily basis. With Ajax, I understand it's different in that I should return XML instead. Not a problem. Easy to do.
The questions I have deal with the YUI side of things. When the Submit button is pressed, what happens? Not normal form submission, right? Do I implement an onSubmit() JavaScript function which then triggers some YUI DataSource to go fetch the data? How do the request params get passed? Hopefully I don't have to construct the request manually. I'm guessing that I use a YAHOO.util.XHRDataSource and that's the DataSource for the DataTable.
I've managed to get the YUI DataTable to work using an HTML table element. Now I just need to switch it over to real data. Curiously, the YUI documentation seems a bit weak here, unless I'm missing something. Maybe this is just basic Ajax that the YUI docs don't cover?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,使用 Ajax 时,您不需要从服务器返回 XML,您可以返回纯文本、XML、JSON 字符串,实际上是您想要的任何形式的文本数据。 这里提供了一个使用 JSON 数据填充数据表的示例:
http://developer .yahoo.com/yui/examples/datatable/dt_xhrjson.html
此处提供了如何使用 Ajax 和 YUI 发送 POST 请求的示例。
http://developer.yahoo.com/yui/examples/connection/post。 html
这应该可以帮助您开始,现在只需将它们链接在一起即可。
要连接到服务器,您可以使用 Yahoo.util.Connect.asyncRequest 方法,该方法接受以下参数:
请参阅此处的示例,该方法使用“GET”,因此您可以使用“GET”或“POST”,只需确保您传入参数
http://developer.yahoo.com/yui/examples /json/json_connect.html
一旦您的函数返回“onSuccess”,请执行以下操作将响应文本解析为 JSON
“jsonData”对象现在包含您的数据,因此现在您可以按照以下示例操作:
http://developer.yahoo.com/yui/examples/datatable/dt_basic.html
它向您展示了如何使用保存数据源的本地对象来初始化数据表。 基本上它会是这样的
为了让它工作,你必须在 HTML 代码中有一个 id 为“basic”的“div”容器,注意这与 DataTable 上的第一个参数匹配
希望这会有所帮助
First of all, when working with Ajax you don't need to return XML from the server, you could return plain text, XML, JSON strings, literally any form of textual data that you want. One example of populating a Datatable with JSON data is provided here:
http://developer.yahoo.com/yui/examples/datatable/dt_xhrjson.html
An example of how to send a post request using Ajax and YUI is provided here.
http://developer.yahoo.com/yui/examples/connection/post.html
That should get you started, now just link both of them together.
To connect to the server you can use the Yahoo.util.Connect.asyncRequest method which takes in the following parameters:
See an example here, this one uses "GET" so you can use either "GET" or "POST" just make sure you pass in your parameters
http://developer.yahoo.com/yui/examples/json/json_connect.html
Once your function returns on your "onSuccess" do the following to parse the response text to JSON
The "jsonData" object now contains your data, so now you can follow this example:
http://developer.yahoo.com/yui/examples/datatable/dt_basic.html
It shows you how to initialize a datatable with a local object holding the datasource. basically it would be something like this
For this to work, you have to have a "div" container in the HTML code with an id of "basic" note this matches the first parameter on the DataTable
Hope this helps