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.03k stars 1.2k forks source link

Not able to change the position of a widget programmatically #230

Open dininski opened 11 years ago

dininski commented 11 years ago

Hi,

I have the following issue - I have a page and some gridster widgets on it. Their positions are saved in a database so that the next time a user opens the page his widgets are in the same place as he left them (I am saving a widget's position, size and element id in a database). What I am attempting to do is display all widgets and then re-position them. I managed to set their position using the dataset property of the widget, but the gridmap is not updated which leads to problems with dragging the elements.

I managed to get a somewhat working version by removing all the widgets and then adding them one by one with an updated position, however the performance is abysmal and there should be a better way. I even tried extending the code so that the gridmap can be rebuilt from scratch, but to no avail. Please advise if there is a better way on achieving this functionality.

lucknerjb commented 10 years ago

@dininski

Try something along the lines of the following. It worked for me. I am having issues with the last column "disappearing" but that may be an entirely different issue. Hope it helps.

var _gridster = [GRIDSTER init code here]
for (var i = 0; i < widgetCount; i++) {
          var el = $('div.widget[data-id="' + widgets[i].data_id + '"');
          var dataRow = Math.floor(i / colCount) + 1;
          var dataCol = (i % colCount) + 1;

          el.attr("data-row", dataRow);
          el.attr("data-col", dataCol);
          //el.attr("data-sizex", "1");
          //el.attr("data-sizey", "1");
        }

        _gridster.init();
lucknerjb commented 10 years ago

@dininski

Actually, don't run the init() call. It seems that if you just update the positioning - say on window resize or something - gridster takes care of the rest. Good luck!

simplenotezy commented 10 years ago

And if you call the init twice, it will also cause troubles.

marcosicp commented 8 years ago

@lucknerjb thanks!