ducksboard / gridster.js

gridster.js is a jQuery plugin that makes building intuitive draggable layouts from elements spanning multiple columns
http://gridster.net/
MIT License
6.04k stars 1.2k forks source link

serialize_params Returns Odd Info #595

Open JayRennemeyer opened 6 years ago

JayRennemeyer commented 6 years ago

I'm trying to get the ID of a widget container when it gets resized. I added an .attr call to the serialize_params function and it worked for a while. It returned [{"element_id":"admin_stats","col":7,"row":3,"size_x":6,"size_y":4}]. But now it's sending weird data back to the resize.stop function.

Gridster Code:

        var gridster = $('.gridster > .widgetContainer').gridster({
            widget_margins: [10, 10],
            widget_base_dimensions: [100, 60],
            min_cols: 12,
            max_cols: 12,
            resize: {
                enabled: true,
                stop: function(e, ui, $widget) {
                    var widgetInfo = JSON.parse($widget);  // <---Throws error shown below.
                    var newHeight = this.resize_coords.data.height;
                    var newWidth = this.resize_coords.data.width; 
                    $('li#' + widgetInfo.element_id).attr('backgriound-color','red');
                }
            },
            serialize_params: function($w, wgd) {
                return {
                  element_id: $w.attr('id'),
                  col: wgd.col,
                  row: wgd.row,
                  size_x: wgd.size_x,
                  size_y: wgd.size_y,
                };
            }
        }).data('gridster');

The error shown in the console is: Uncaught SyntaxError: Unexpected token o in JSON at position 1

If I display the results of JSON.stringify($widget), it contains: {"0":{"jQuery11240030910348659392684":192,"sizzle1505230551480":{"undefined":{"parentNode":[19,14,true]}}},"length":1,"prevObject":{"0":{},"context":{},"length":1},"context":{}}

What is happening and how do I fix it?