Skip to content

How to add data to the added node #626

@bas-a

Description

@bas-a

@troolee
How can I add a data object, or just an extra property to a widget when i create a new one via the addWidget function? The only way I know how is to add it to the data-attribute in the dom. Like this:
grid = $('.grid-stack').data('gridstack');
grid.addWidget($('<div><div class="grid-stack-item-content"></div>'), x, y, width, height, autoPosition, minWidth, maxWidth, minHeight, maxHeight, id).attr({"data-custom-id": customId})

Best way would be to add some more properties via the addWidget function. I want to access them via the node of the element in Javascript.

EDIT:
I think I fixed my problem by the following code:

grid.grid.nodes = GridStackUI.Utils.sort(grid.grid.nodes)
let lastWidget = _.last(grid.grid.nodes)
lastWidget.customDataObject = {
name: "John",
surname: "Doe"
}

Is this the correct way of adding data to the widget node object?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions