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

Problem to load serialize gridster - wrong widget position #330

Closed sosoriov closed 10 years ago

sosoriov commented 10 years ago

I have a problem to load a gridster from serialize object.

Some of my widgets don't appear in the correct position, but in the serialize object they are ok:

My original gridster is:

image

After serialize and load it appears

image

This is the serialize object

image

Any idea of it?

Thanks

BarryatVendorpages commented 10 years ago

Im facing the same problem...

sosoriov commented 10 years ago

I still have the problem, any idea?

BarryatVendorpages commented 10 years ago

this probaly makes no sense: https://github.com/ducksboard/gridster.js/issues/335

BarryatVendorpages commented 10 years ago

But the same goes for you, if you want a function to see what I mean let me know ;)

sosoriov commented 10 years ago

@BarryatVendorpages it would be very useful for me thanks (y)

BarryatVendorpages commented 10 years ago
//Sorts tiles according to position
jQuery.fn.tileposition = function() {
    //Gets tile's row
pos = new Array();
$.each(this, function(i,object){
    row = $(object).attr('data-row');
    pos[i] = {'row' : row, 'current' : i};
});

//Sorts tiles position acording to row
sortedpos = pos.sort(function(a,b) {
return parseInt(a.row,10) - parseInt(b.row,10);
});

//Creates posarray used below to sort object
posarray = new Array();
$.each(sortedpos, function(i,value){
    posarray[i] = value.current;
 });

// get copy of DOM element array
var copy = this.toArray();

// don't exceed bounds of arrays
var len = Math.min(posarray.length, copy.length);

for (var i = 0; i < len; i++) {
    var index = posarray[i];
    // make sure incoming index is within bounds of DOM object array
    if (index < copy.length) {         
        // put the index item into the i location
        this[i] = copy[index];
    }
}
return this;
};

// The above function basically just reorders dom elements/widget into the appropriate order returning another object... at which point I save each value into a database one after another, later each is recalled in appropriate order, gridster is initiated and all is OKAY :D Sorry about the mess of the above function, be sure to optimize it. Its just a rough.

var correctlistofwidgets = $('#listofwigets > li').tileposition();    

Good Luck

JulienVerkest commented 10 years ago

@BarryatVendorpages Thanks! Your function solves my problem.

sosoriov commented 10 years ago

@BarryatVendorpages Thanks!!! It works

topherreynoso commented 10 years ago

Here's a rails helper of the same, in case anyone needs it:

def dashboard_ordered(widgets)
  pos = []
  widgets.each_with_index do |widget,i|
    row = widget['row']
    pos[i] = { row: row, current: i }
  end
  sortedpos = pos.sort_by{|widget| widget[:row]}
  posarray = []
  sortedpos.each_with_index do |widget,i|
    posarray[i] = widget[:current]
  end
  copy = widgets.dup
  len = posarray.count 
  len = copy.count if copy.count < posarray.count
  i = 0
  while i < len
    index = posarray[i]
    if index < copy.count
      widgets[i] = copy[index]
    end
    i += 1
  end
  return widgets
end