[英]How can I parse gridstack.js items?
Maybe this is quite simple, but I'm still learning JS and stuff. 也许这很简单,但我还在学习JS和东西。 I'm using the plugin https://github.com/troolee/gridstack.js and want to send ajax requests whenever a widget gets repositioned/resized.
我正在使用插件https://github.com/troolee/gridstack.js,并希望每当窗口小部件重新定位/调整大小时发送ajax请求。 I wrote this (according to the official readme):
我写了这个(根据官方自述文件):
var serialize_widget_map = function (items) {
console.log(items);
};
// onchange position/size
$('.grid-stack').on('change', function (e, items) {
console.log(items);
});
Just to see what the console says: [Object, Object] - maybe because I have 2 widgets on page, but I have to notice that this quantity may vary (widgets might be removed/added dynamically). 只是看看控制台说的是什么:[对象,对象] - 也许是因为我在页面上有2个小部件,但我必须注意到这个数量可能会有所不同(小部件可能被动态删除/添加)。
How can I "parse" this "items" thing so I can access properties of the widgets? 如何“解析”这个“项目”的东西,以便我可以访问小部件的属性?
Just in case someone's looking for the answer to this question, I have solved this problem: 为了防止有人在寻找这个问题的答案,我已经解决了这个问题:
$('.grid-stack').on('change', function (e, items) {
var widgets = [];
for (i = 0; i < items.length; i++) {
var widgetsObj = {
'widgetId': items[i].el.context.id,
'x': items[i].x,
'y': items[i].y,
'width': items[i].width,
'height': items[i].height
}
widgets.push(widgetsObj);
}
}
Because the items variable may contain multiple objects, I loop through it to create a single array of objects with properties I need. 因为items变量可能包含多个对象,所以我遍历它以创建具有我需要的属性的单个对象数组。
i came to your question because i was looking for a way to retrieve the current items / nodes 我提出了你的问题,因为我正在寻找一种方法来检索当前的项目/节点
i found this solution, where an event is not required 我找到了这个解决方案,不需要事件
grid = $('.grid-stack').data('gridstack');
items = grid.grid.nodes;
var serialize_widget_map = function (items) {
console.log(items);
};
$('.grid-stack').on('change', function (e, items) {
var widgets = [];
for (var i = 0; i < items.length; i++) {
var widgetsObj = {
'widgetId': items[i],
'x': items[i].x,
'y': items[i].y,
'width': items[i].width,
'height': items[i].height
}
}
serialize_widget_map(widgetsObj);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.