题
呼叫所有 DWR 专家!
我目前正在使用反向 Ajax 将数据动态添加到网页中的表中。
当我运行以下方法时:
public static void addRows(String tableBdId, String[][] data) {
Util dwrUtil = new Util(getSessionForPage()); // Get all page sessions
dwrUtil.addRows(tableBdId, data);
}
根据需要在我的网页中创建新行。
但是,为了稍后更新这些新创建的值,标签需要有一个元素 ID 供我访问。
我查看了 DWR javadoc,您可以指定一些附加选项,请参阅 http://directwebremoting.org/dwr/browser/addRows ,但这对我来说没有什么意义,文档非常稀疏。
如果有人能给我一个关于如何为创建的 td 元素指定元素 id 的线索,我将不胜感激。或者,如果有人知道替代方法,我很想知道。
亲切的问候
卡尔
解决方案
我能得到的最接近的是一些参数传递给该元素的ID。见下文:
public static void addRows(String tableBdId, String[] data, String rowId) {
Util dwrUtil = new Util(getSessionForPage()); // Get all page sessions
// Create the options, which is needed to add a row ID
String options = "{" +
" rowCreator:function(options) {" +
" var row = document.createElement(\"tr\");" +
" row.setAttribute('id','" + rowId + "'); " +
" return row;" +
" }," +
" cellCreator:function(options) {" +
" var td = document.createElement(\"td\");" +
" return td;" +
" }," +
" escapeHtml:true\"}";
// Wrap the supplied row into an array to match the API
String[][] args1 = new String[][] { data };
dwrUtil.addRows(tableBdId, args1, options);
其他提示
你的这行代码真的有效吗?
dwrUtil.addRows(tableBdId, data);
DWR addRows方法至少需要4个参数3才能工作, , 他们是:
- ID: :表元素的 id(最好是 tbody 元素);
- 大批: :数组(或 DWR 1.1 中的对象),更新表中的每一行包含一个条目;
- 细胞功能: :一组函数(每列一个),用于从传递的行数据中提取单元格数据;
- 选项: :包含各种选项的对象。
这 ID, 大批 和 细胞功能 是 必需的, ,就你而言,你必须通过 选项 还因为 您想要自定义行创建并设置 TD id. 。一探究竟:
在 options 参数中,您需要使用一个名为“cellCreator”的参数来告知您自己创建 td html 元素的方式。一探究竟:
// Use the cellFuncs var to set the values you want to display inside the table rows
// the syntax is object.property
// use one function(data) for each property you need to show on your table.
var cellFuncs = [
function(data) { return data.name_of_the_first_object_property ,
function(data) { return data.name_of_the_second_object_property; }
];
DWRUtil.addRows(
tableBdId,
data,
cellFuncs,
{
// This function is used for you customize the generated td element
cellCreator:function(options) {
var td = document.createElement("td");
// setting the td element id using the rowIndex
// just implement your own id bellow
td.id = options.rowIndex;
return td;
}
});
不隶属于 StackOverflow