通过 dependentObservable 使用 Knockoutjs 更新 SlickGrid
根据示例 http://jsfiddle.net/rniemeyer/A9NrP/ 我将 SlickGrid 与 Knockout.js 结合使用
我 网格填充和添加按钮按照示例工作。我遇到的问题是,当我通过 ko.dependentObservable 更新视图模型的 rows 属性时,会触发 ko.bindingHandlers 的“更新”部分,但光滑的网格不会接收更改。
定义绑定的 html:
<div id="grid" data-bind="slickGrid: { data: rows, columns: columns }"></div>
SlickGrid 代码 (与示例相同):
var grid;
ko.bindingHandlers.slickGrid = {
init: function (element, valueAccessor) {
var settings = valueAccessor();
var data = ko.utils.unwrapObservable(settings.data);
var columns = ko.utils.unwrapObservable(settings.columns);
var options = ko.utils.unwrapObservable(settings.options) || {};
grid = new Slick.Grid(element, data, columns, options);
},
update: function (element, valueAccessor, allBindingAccessor, viewModel) {
var settings = valueAccessor();
// I can see the correct data here but the grid does not update
var data = ko.utils.unwrapObservable(settings.data);
grid.render();
}
}
我的模型:
myViewModel = {
data : ko.observableArray(),
tabs: ['High', 'Medium', 'Low'],
rows : ko.observableArray([]),
columns : [
{
id: "id",
name: "ID",
field: "id"
},
{
id: "Location",
name: "Location",
field: "Location"
},
{
id: "Comment",
name: "Comment",
field: "Comment"
}
],
addItem: function() { // this works and SlickGrid adds a new row
this.rows.push(new ModelRow(0, "New", 5.00));
},
}
进行 ajax 调用并触发 ko.bindingHandlers.slickGrid.update 的代码,但 slickgrid 似乎没有接受更改,ajax 确实返回有效数据,并在用户单击链接时触发:
ko.dependentObservable(function () {
if (this.data.lastAlarmRequest) this.data.lastAlarmRequest.abort();
this.data.lastAlarmRequest = $.get("/audit/alarmsdata/high", null, this.rows);
}, i2owater.viewmodels.AlarmsForViewModel);
为什么 addItem 函数可以工作,但 ko.bindingHandlers.slickGrid.update 却不能?在更新函数中,我可以看到网格应绑定到的正确数据。 是不是因为rows属性中的所有数据都被覆盖了?
更新: 我尝试使用 grid.invalidate();但它不起作用,并且还发现一旦执行 ko.dependentObservable , addItem 函数就会停止工作
I'm using SlickGrid with knockout.js based on the example http://jsfiddle.net/rniemeyer/A9NrP/
I have the grid populating and the add button working as per the example. The problem I'm having is when I update the rows property of my viewmodel via a ko.dependentObservable the 'update' section of the ko.bindingHandlers is fired but slick grid does not pick up the changes.
html that defines the binding:
<div id="grid" data-bind="slickGrid: { data: rows, columns: columns }"></div>
SlickGrid code (same as the example):
var grid;
ko.bindingHandlers.slickGrid = {
init: function (element, valueAccessor) {
var settings = valueAccessor();
var data = ko.utils.unwrapObservable(settings.data);
var columns = ko.utils.unwrapObservable(settings.columns);
var options = ko.utils.unwrapObservable(settings.options) || {};
grid = new Slick.Grid(element, data, columns, options);
},
update: function (element, valueAccessor, allBindingAccessor, viewModel) {
var settings = valueAccessor();
// I can see the correct data here but the grid does not update
var data = ko.utils.unwrapObservable(settings.data);
grid.render();
}
}
my model:
myViewModel = {
data : ko.observableArray(),
tabs: ['High', 'Medium', 'Low'],
rows : ko.observableArray([]),
columns : [
{
id: "id",
name: "ID",
field: "id"
},
{
id: "Location",
name: "Location",
field: "Location"
},
{
id: "Comment",
name: "Comment",
field: "Comment"
}
],
addItem: function() { // this works and SlickGrid adds a new row
this.rows.push(new ModelRow(0, "New", 5.00));
},
}
The code that make the ajax call, and fires ko.bindingHandlers.slickGrid.update but slickgrid doesnt seem to pick up the changes, the ajax does return valid data, and is fired when the user clicks on a link:
ko.dependentObservable(function () {
if (this.data.lastAlarmRequest) this.data.lastAlarmRequest.abort();
this.data.lastAlarmRequest = $.get("/audit/alarmsdata/high", null, this.rows);
}, i2owater.viewmodels.AlarmsForViewModel);
Why does the addItem function work but not the ko.bindingHandlers.slickGrid.update? In the update function I can see the correct data that the grid should be bound to.
Is it because all the data in the rows propertys is overwritten?
UPDATE:
I've tried to use grid.invalidate(); but it doesnt work and have also seen that the addItem function stops working once ko.dependentObservable is executed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我更新了 rniemeyer 非常有用的 jsfiddle 来修复滚动条错误。在原始版本中,尝试添加 20 行,然后使用垂直滚动条,您将看到 UI 问题。问题是滚动窗格错误地计算了视图大小,因此滚动条抖动。
这里有一个更新版本。
编辑:Knockout 和 slickgrid 已更新,因此我更新了小提琴:
编辑:再次:
http://jsfiddle.net/5ddNM/79/
请注意以下事项:
I've updated rniemeyer's very useful jsfiddle to fix a scrollbar bug. In the original, try adding 20 rows then use the vertical scrollbar, and you'll see a UI problem. The issue is that the scrollpane incorrectly calculates the view size, so the scrollbar jitters.
Here an updated version.
EDIT: Knockout and slickgrid have been updated so I've updated the fiddle:
EDIT: And again:
http://jsfiddle.net/5ddNM/79/
Note the following:
我刚刚遇到了同样的问题并找到了解决方案,请将您的更新代码更改为以下内容:
顺便说一下,setData 中的 true 参数指示您是否希望网格滚动回顶部。
I just had the same issue and have found the solution, change your update code to the following:
By the way, the true parameter in setData indicates if you want the grid to scroll back to the top or not.
就我而言,我需要将 slickgrid 和淘汰赛与绑定到不同控件的另一个视图模型一起使用。我使用 http://jsfiddle.net/hlascelles/5ddNM/ 作为示例并修复了一些问题。使用 http://jsfiddle.net/Schnapz/z4YLD/2/ 或查看下面的说明。
您可以按照示例中的说明保留网格、项目和视图模型的声明。但是,绑定对于包括 viewModel 本身在内的所有模型都应该是通用的(在 pageLoad 或其他中调用它):
并且 addItem 应该如下所示:
因为使用“this”在这里不起作用。
然后,自定义活页夹应该如下所示(来自某处另一个示例的令牌):
}
另外,我遇到了添加元素的问题,但我看不到结果。经过一些研究 slickgrid 的来源后,我找到了这个解决方案:
因为默认情况下 grid-viewport 的高度为 0px :) 只是在绑定过程中添加了一些选项。
In my case I needed to use slickgrid and knockout with another viewmodels binded to different controls. I used http://jsfiddle.net/hlascelles/5ddNM/ as example and fixed few things there. Use http://jsfiddle.net/Schnapz/z4YLD/2/ or look description below.
Declaration of grid, Item and viewModel you can leave as written in example. But, binding should be common for all models including viewModel itself (call it in pageLoad or else):
And addItem should look like this:
Because using 'this' won't work here.
Then, custom binder should look like this (token from another example somewhere):
}
Also, I had a problem that elements were added, but I couldn't saw the result. After some research slickgrid's sources I found this solution:
Because somehow by default grid-viewport had 0px height :) Just added some options during binding.