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

reload saved settings #462

Open RobFra opened 9 years ago

RobFra commented 9 years ago

I've added the function that I'll paste here below, It seems to work, in fact it saves to localstorage and load from local storage and values are correct but.... when page is reloaded, tiles remain in the original position!. I'm getting crazy, can't understand what I'm doing wrong or maybe is it an issue?

$(function () { //DOM Ready

var gridster = $(".gridster > ul").gridster(

{ widget_margins: [10, 10],

widget_base_dimensions: [200, 225],

serialize_params: function ($w, wgd) { return { id: $($w).attr('id'), col: wgd.col, row: wgd.row, size_x: wgd.size_x, size_y: wgd.size_y, }; },

draggable:

{

stop: function (event, ui) { console.log('inside stop'); var positions = JSON.stringify(this.serialize());

localStorage.setItem('positions', positions);

console.log(positions);

$.post(

"process.php",

{ "positions": positions },

function (data) {

if (data == 200)

console.log("Data successfully sent to the server");

else

console.log("Error: Data cannot be sent to the server")

}

); }

}

}).data('gridster');

var localData = JSON.parse(localStorage.getItem('positions'));

if (localData != null) { $.each(localData, function (i, value) {

console.log('found positions'); console.log(value.col); console.log(value.row); console.log(value.size_x); console.log(value.size_y); var id_name;

id_name = "#"; id_name = id_name + value.id;

$(id_name).attr({ "data-col": value.col, "data-row": value.row, "data-sizex": value.size_x, "data-sizey": value.size_y });

}); } else {

console.log('No data stored in the local storage. Continue with the default values'); }

});

RobFra commented 9 years ago

Please, is it possible to have an answer? Is it a bug or am I doing anything wrong?

marceloemanoel commented 9 years ago

I've done something similar but instead of local storage I used a simple cookie. So far is working pretty good for me. You can check the code here. Look for the start and store functions.

marceloemanoel commented 9 years ago

I believe that the problem with your code is that you're setting just the data attributes, instead of adding the widgets through gridster.

Instead of this:

$(id_name).attr({ "data-col": value.col, "data-row": value.row, "data-sizex": value.size_x, "data-sizey": value.size_y });

Try this:

gridster.add_widget($(id_name), value.size_x, value.size_y, value.col, value.row);
RobFra commented 9 years ago

Hello, thank you very much for your email. I’ll try your suggestion later, I’ll be back to let you know.

Thanks

From: Marcelo Emanoel Bezerra Diniz [mailto:notifications@github.com] Sent: lunedì 17 novembre 2014 15:26 To: ducksboard/gridster.js Cc: Roberta Franchi Subject: [NEWSENDER] - Re: [gridster.js] reload saved settings (#462) - Messaggio da mittente sconosciuto

I believe that the problem with your code is that you're setting just the data attributes, instead of adding the widgets through gridster.

Instead of this:

$(id_name).attr({ "data-col": value.col, "data-row": value.row, "data-sizex": value.size_x, "data-sizey": value.size_y });

Try this:

gridster.add_widget($(id_name), value.size_x, value.size_y, value.col, value.row);

— Reply to this email directly or view it on GitHubhttps://github.com/ducksboard/gridster.js/issues/462#issuecomment-63311331.

RobFra commented 9 years ago

Hello Marcelo,

adding widgets I receive the following error:

[cid:image001.png@01D0033B.CFBE8DB0]

And with your code: https://gist.github.com/marceloemanoel/5be3a550f1e7ad45fa3f

I’ve never used RequireJs. Can you say me how to use this function or where do I have to put it? I receive the error: define is undefined.

From: Marcelo Emanoel Bezerra Diniz [mailto:notifications@github.com] Sent: lunedì 17 novembre 2014 15:26 To: ducksboard/gridster.js Cc: Roberta Franchi Subject: [NEWSENDER] - Re: [gridster.js] reload saved settings (#462) - Messaggio da mittente sconosciuto

I believe that the problem with your code is that you're setting just the data attributes, instead of adding the widgets through gridster.

Instead of this:

$(id_name).attr({ "data-col": value.col, "data-row": value.row, "data-sizex": value.size_x, "data-sizey": value.size_y });

Try this:

gridster.add_widget($(id_name), value.size_x, value.size_y, value.col, value.row);

— Reply to this email directly or view it on GitHubhttps://github.com/ducksboard/gridster.js/issues/462#issuecomment-63311331.

RobFra commented 9 years ago

Hi Marcelo, here I am again. I think that I’ve found out the problem. It is not working perfectly now but quite better: the problem was that the id of

  • was not the one I use as id_name in the javascript!

    Thanks

    From: Marcelo Emanoel Bezerra Diniz [mailto:notifications@github.com] Sent: lunedì 17 novembre 2014 15:26 To: ducksboard/gridster.js Cc: Roberta Franchi Subject: [NEWSENDER] - Re: [gridster.js] reload saved settings (#462) - Messaggio da mittente sconosciuto

    I believe that the problem with your code is that you're setting just the data attributes, instead of adding the widgets through gridster.

    Instead of this:

    $(id_name).attr({ "data-col": value.col, "data-row": value.row, "data-sizex": value.size_x, "data-sizey": value.size_y });

    Try this:

    gridster.add_widget($(id_name), value.size_x, value.size_y, value.col, value.row);

    — Reply to this email directly or view it on GitHubhttps://github.com/ducksboard/gridster.js/issues/462#issuecomment-63311331.

  • harshmsp commented 9 years ago

    Hello @marceloemanoel If I want to bind gridster data from database then how to re initialize gridster class if already exists in a page.