我该如何解析gridstack.js项目? [英] How can I parse gridstack.js items?
问题描述
也许这很简单,但我还在学习JS和东西。
我正在使用插件 https://github.com/troolee/gridstack.js并且希望在窗口小部件重新定位/调整大小时发送ajax请求。我写了这个(根据官方自述文件):
Maybe this is quite simple, but I'm still learning JS and stuff. I'm using the plugin https://github.com/troolee/gridstack.js and want to send ajax requests whenever a widget gets repositioned/resized. 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);
});
只是看看控制台说的是什么:[对象,对象] - 也许是因为我有2个小部件页面,但我必须注意,这个数量可能会有所不同(小部件可能会被移除/动态添加)。
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).
我如何解析这个项目的东西,所以我可以访问小部件的属性吗?
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);
}
}
因为items变量可能包含多个对象,所以我遍历它以创建具有我需要的属性的单个对象数组。
Because the items variable may contain multiple objects, I loop through it to create a single array of objects with properties I need.
这篇关于我该如何解析gridstack.js项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!