将 cellUpdateEvent 与 YUI DataTable 和 JSON DataSource 一起使用
我正在使用一个具有 (YUI2) JSON DataSource 的 UI,该 UI 用于填充 DataTable。我想做的是,当表中的值更新时,对值发生更改的单元格执行简单的动画。
以下是一些相关的代码片段:
var columns = [
{key: 'foo'},
{key: 'bar'},
{key: 'baz'}
];
var dataSource = new YAHOO.util.DataSource('/someUrl');
dataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
dataSource.connXhrMode = 'queueRequests';
dataSource.responseSchema = {
resultsList: 'results',
fields: [
{key: 'foo'},
{key: 'bar'},
{key: 'baz'}
]
};
var dataTable = new YAHOO.widget.DataTable('container', columns, dataSource);
var callback = function() {
success: dataTable.onDataReturnReplaceRows,
failure: function() {
// error handling code
},
scope: dataTable
};
dataSource.setInterval(1000, null, callback);
这就是我想用它做的事情:
dataTable.subscribe('cellUpdateEvent', function(record, column, oldData) {
var td = dataTable.getTdEl({record: record, column: column});
YAHOO.util.Dom.setStyle(td, 'backgroundColor', '#ffff00');
var animation = new YAHOO.util.ColorAnim(td, {
backgroundColor: {
to: '#ffffff';
}
});
animation.animate();
};
但是,使用 cellUpdateEvent
似乎不起作用。 由于 setInterval
回调而更新的单元格是否会触发 cellUpdateEvent
?
可能是我不完全理解发生了什么在DataTable
的幕后。也许每次查询数据时都会重新绘制整个表格,因此它不知道或不关心单个单元格的更改?解决方案是编写我自己的特定函数来替换 onDataReturnReplaceRows
吗?有人可以启发我如何实现这一目标吗?
编辑:
深入研究 datatable-debug.js 后,看起来 onDataReturnReplaceRows
不会触发 cellUpdateEvent
。它对支持 DataTable
的 RecordSet
调用 reset()
,从而删除所有行;然后它用新数据重新填充表。我尝试将其更改为使用 onDataReturnUpdateRows
,但这似乎也不起作用。
Edit2:
为了实现我想要的控制,我最终编写了自己的基于
的数据列表,这对我正在尝试的问题更有意义来解决。珍妮的下面的回答应该有助于解决大多数其他人的问题,所以我接受它作为解决方案。I'm working with a UI that has a (YUI2) JSON DataSource that's being used to populate a DataTable. What I would like to do is, when a value in the table gets updated, perform a simple animation on the cell whose value changed.
Here are some relevant snippets of code:
var columns = [
{key: 'foo'},
{key: 'bar'},
{key: 'baz'}
];
var dataSource = new YAHOO.util.DataSource('/someUrl');
dataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
dataSource.connXhrMode = 'queueRequests';
dataSource.responseSchema = {
resultsList: 'results',
fields: [
{key: 'foo'},
{key: 'bar'},
{key: 'baz'}
]
};
var dataTable = new YAHOO.widget.DataTable('container', columns, dataSource);
var callback = function() {
success: dataTable.onDataReturnReplaceRows,
failure: function() {
// error handling code
},
scope: dataTable
};
dataSource.setInterval(1000, null, callback);
And here's what I'd like to do with it:
dataTable.subscribe('cellUpdateEvent', function(record, column, oldData) {
var td = dataTable.getTdEl({record: record, column: column});
YAHOO.util.Dom.setStyle(td, 'backgroundColor', '#ffff00');
var animation = new YAHOO.util.ColorAnim(td, {
backgroundColor: {
to: '#ffffff';
}
});
animation.animate();
};
However, it doesn't seem like using cellUpdateEvent
works. Does a cell that's updated as a result of the setInterval
callback even fire a cellUpdateEvent
?
It may be that I don't fully understand what's going on under the hood with DataTable
. Perhaps the whole table is being redrawn every time the data is queried, so it doesn't know or care about changes to individual cells?. Is the solution to write my own specific function to replace onDataReturnReplaceRows
? Could someone enlighten me on how I might go about accomplishing this?
Edit:
After digging through datatable-debug.js, it looks like onDataReturnReplaceRows
won't fire the cellUpdateEvent
. It calls reset()
on the RecordSet
that's backing the DataTable
, which deletes all of the rows; it then re-populates the table with fresh data. I tried changing it to use onDataReturnUpdateRows
, but that doesn't seem to work either.
Edit2:
To achieve the control that I wanted, I ended up writing my own <ul>
-based data list that made a bit more sense for the problem I was trying to solve. Jenny's answer below should help solve this for most others, so I've accepted it as the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
cellUpdateEvent 仅在响应 updateCell() 调用时触发。您想要的是订阅 cellFormatEvent。您的代码中还有一些其他问题,因此这应该可行:
cellUpdateEvent only fires in response to a call to updateCell(). What you want is to subscribe to the cellFormatEvent. There were a couple other issues in your code, so this should work:
该示例将不起作用,因为您添加了一个间隔,而这不是正确的解决方案。因为该函数每次都会被调用。
This example will not work beceause you added an interval and this is not the right solution. Because the function will be called each time.