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);
});
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);
}
}
Because the items variable may contain multiple objects, I loop through it to create a single array of objects with properties I need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With