Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on resizestop - data-gs-height not updated ? #577

Closed
karam12 opened this issue Jan 4, 2017 · 19 comments
Closed

on resizestop - data-gs-height not updated ? #577

karam12 opened this issue Jan 4, 2017 · 19 comments
Assignees
Labels
Milestone

Comments

@karam12
Copy link

karam12 commented Jan 4, 2017

Hi !

I am not able to get the current height of the element when I'm in the .on('resizestop') event.
When I make : element.getAttribute('data-gs-height'); It gives me always the old value, not the new one.

Here is my code :
https://jsfiddle.net/Karam/wzzm2oua/6/

Thank you for your help.

@DennisPallett
Copy link

We are running into the same problems. It seems the "snapping to the grid" action happens after the resizestop event.

@karam12
Copy link
Author

karam12 commented Jan 4, 2017

I found a solution, but I don't like it !

I calculated the height by looking into the source of gridstack.js

var grid = $(this).data('gridstack');
var height  = (1.5 * grid.opts.cellHeight - 1) 

But it would be greate if this probleme could be corrected.

@radiolips
Copy link
Member

This is updated when jQueryUI tells the code the resize is finished - and the animation is even part of jQueryUI. I tried a couple things before to get this to work properly, to no avail. I'll take another look, but if anyone has any ideas, let me know. I've thought about hooking into the animation completion events, but without full information (that is, the animation could change), this is unreliable.

@karam12
Copy link
Author

karam12 commented Jan 10, 2017

Can't you create another event that is triggered after you change the elements sizes ? (like the 'change' event, one for resize and another one for move)

@Poohyhu
Copy link

Poohyhu commented Jan 11, 2017

change event on data-gs-height and data-gs-width should be solve the problem, just make sure to call the event only once if both are changed

@MysticSmeg
Copy link

Hey people,
😃

Does anyone know if there is a fix, work-around or anything planned to resolve this issue please?

I'm working on a ASP.NET (Core) MVC dashboard project, calling C# controller code to write "x,y" (dragstop) & "width,height" (resizestop) settings back to an SQL table to persist the dashboard state. The code works fine, but the values returned to the methods are not updated until after the events (as stated here).

Unfortunately, the work-around mentioned here (calculating from opts) only works for height, since "opts" does not contain a 'cellWidth' or 'x','y'.

Any help greatly appreciated!!! 😉

@Poohyhu
Copy link

Poohyhu commented Mar 22, 2017

Hi, you can calculate the grid item size from css height/width.

@MysticSmeg
Copy link

MysticSmeg commented Mar 22, 2017 via email

@Poohyhu
Copy link

Poohyhu commented Mar 23, 2017

Hi,
You can try jQuery css() function to get the '.grid-stack-item' width and height.
when you create the grid you can specify the cellheight (height of a row), so have the height divider
cellHeight.
to calculate the cols with is a bit harder, you need the grid container width (in pixels) and you have to divide by grid cols first ....
anyway see below

var gridOptions =
{
cellHeight: 32,
width: 12
}
$('#grid-container').on('resizestop', function(event)
{
item = event.target; // the grid item (element)
cssWidth = parseInt($(item).css('width'), 10);
cssHeight = parseInt($(item).css('height'), 10);

itemHeight = Math.round(cssHeight / gridOptions.cellHeight);
itemWidth = Math.round(cssWidth/(parseInt($(this).css('width'), 10) / gridOptions.width));
}

@MysticSmeg
Copy link

MysticSmeg commented Mar 23, 2017

Hey, thanks for the example Poohyhu.
😃

I understand what you mean now, and I've implemented something similar.

Unfortunately, the height calculation needs some work (will post if I get it working) as the value is out (bit high) and this work-around doesn't address the x,y issue; it's a pretty cool package otherwise 👍.

I'm hoping you manage to resolve this in a future release of GridStack?

Thanks again

---- update ----

Have found var newHeight = Math.floor(cssHeight / gridOptions.cellHeight) - 1; works better.

@Poohyhu
Copy link

Poohyhu commented Mar 23, 2017

Hi,

not so nice, but workable solution for dragstop and resizestop
o = event.target;
setTimeout(function(){
x = $(o).attr('data-gs-x');
}, 50);

you can get any live data, just wait for a few millisecond 😃
(of course you have to put the code in the right event handler, and with the right attribute)

otherwise, for x and y you can use the jQuery offset().top and offset().left to calculate the right grid x y

regards

@MysticSmeg
Copy link

You're pretty awesome! Nice work, thanks 😄

The complete work-around (which works for now) is below (for others):

    <script type="text/javascript">

        // default ready function
        $(function () {
            var options = {
                animate: true,
                alwaysShowResizeHandle: true
            };
            $('.grid-stack').gridstack(options);

            new function () {
                this.grid = $('.grid-stack').data('gridstack');

// more code doing stuff here

                $('.grid-stack').on('resizestop', function(event, ui) {
                    var element = event.target;
                    var id = element.children[1].children[0].id;  //allows indexing of multiple items
                    var selectedItem = $('.grid-stack-item')[id - 1];
                    setTimeout(function () {
                        var newWidth = $(selectedItem).attr('data-gs-width');
                        var newHeight = $(selectedItem).attr('data-gs-height');
                        resizeInstrument(id, newWidth, newHeight);
                    }, 50);
                });

                $('.grid-stack').on('dragstop', function (event, ui) {
                    var grid = this;
                    var element = event.target;
                    var id = element.children[1].children[0].id; //allows indexing of multiple items
                    var selectedItem = $('.grid-stack-item')[id - 1];
                    setTimeout(function () {
                        var newX = $(selectedItem).attr('data-gs-x');
                        var newY = $(selectedItem).attr('data-gs-y');
                        moveInstrument(id, newX, newY);
                    }, 50);
                });

// even more code doing stuff here

        });
    </script>

...and example of called function code, which hooks into ASP.NET / MVC / C# code (located in site.js):

function resizeInstrument(id, width, height) {
    var csvData = id + "," + width + "," + height;

    $.ajax({
        //POST type response with text (csv) parameter
        type: "POST",
        datatype: "text/plain",
        //url is comprised of Controller name/method name/ + any query data (parameters)
        url: "/Home/ResizeInstrument/?data=" + csvData,
        contentType: "application/csv; charset=utf-8",
        cache: false,
        success: function (data) {
            // id of div to contain results
            $('.grid-stack-item-content#instrument' + id).html(data);
        }
    });
}

Where did I leave my refactoring tool box...?

@Poohyhu
Copy link

Poohyhu commented Mar 23, 2017

welcome,

but you don't need the children thing. the event.target is the selected object
so the $(element).attr() should be enough

regards

@MysticSmeg
Copy link

I'm rendering charts (using high-charts) into affected <divs>, and since the data for the charts is coming from a database table (as merged script), I need to look up specific data each time the user interacts with a specific <div>. The id comes from the primary key in the table, and is written into the id of the <div> containing the chart during this.loadGrid(). The chart <div>is a child to the grid-stack-container <div>.

I had to dig about a bit to locate the id, and $(element).attr('id'); returns undefined in this case. But I understand what you're saying; there is likely a better way to do it. Thanks.

@radiolips radiolips self-assigned this Apr 20, 2017
@radiolips radiolips added this to the v0.3.0 milestone Apr 20, 2017
@radiolips
Copy link
Member

This will be fixed in v0.3.0 which will be release 4/21.

@radiolips
Copy link
Member

Fixed on the develop branch. Will be on master tomorrow. Feel free to test it now. Please note that the event you need is now called gsresizestop and it returns the event and the resized element.

@pootpootman
Copy link

This will get you the updated values.
$(this).data('_gridstack_node');

@radiolips
Copy link
Member

@pootpootman In what context would you do that? Using the event is the best way to get the size of the resized widget.

@joemorcullo8
Copy link

joemorcullo8 commented Nov 16, 2017

Hi I'm having an issue regarding this gsresizestop.

#818

https://jsfiddle.net/60yu4p9t/7/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants