README.md 6.04 KB
Newer Older
Pavel Reznikov's avatar
Pavel Reznikov committed
1
2
3
gridstack.js
============

Pavel Reznikov's avatar
Pavel Reznikov committed
4
5
gridstack.js is a jQuery plugin for widget layout. This is drag-and-drop multi-column grid. It allows you to build 
draggable responsive bootstrap v3 friendly layouts. It also works great with [knockout.js](http://knockoutjs.com)
Pavel Reznikov's avatar
Pavel Reznikov committed
6

Pavel Reznikov's avatar
readme    
Pavel Reznikov committed
7
Inspired by [gridster.js](http://gridster.net). Built with love.
Pavel Reznikov's avatar
Pavel Reznikov committed
8
9
10
11

Demo
====

Pavel Reznikov's avatar
Pavel Reznikov committed
12
Please visit http://troolee.github.io/gridstack.js/ for demo.
Pavel Reznikov's avatar
Pavel Reznikov committed
13
14
15
16


Usage
=====
Pavel Reznikov's avatar
readme    
Pavel Reznikov committed
17

Pavel Reznikov's avatar
Pavel Reznikov committed
18
19
## Requirements

Pavel Reznikov's avatar
Pavel Reznikov committed
20
21
22
23
* http://underscorejs.org (>= 1.7.0)
* http://jquery.com (>= 1.11.0)
* http://jqueryui.com (>= 1.11.0)
* (Optional) http://knockoutjs.com (>= 3.2.0)
Pavel Reznikov's avatar
license    
Pavel Reznikov committed
24

Pavel Reznikov's avatar
Pavel Reznikov committed
25
26
27
28
## Basic usage

```html
    <div class="grid-stack">
Pavel Reznikov's avatar
typo    
Pavel Reznikov committed
29
30
31
32
33
34
35
36
37
38
        <div class="grid-stack-item" 
            data-gs-x="0" data-gs-y="0" 
            data-gs-width="4" data-gs-height="2">
                <div class="grid-stack-item-content"></div>
        </div>
        <div class="grid-stack-item" 
            data-gs-x="4" data-gs-y="0" 
            data-gs-width="4" data-gs-height="4">
                <div class="grid-stack-item-content"></div>
        </div>
Pavel Reznikov's avatar
Pavel Reznikov committed
39
40
41
42
43
44
45
46
    </div>
    
    <script type="text/javascript">
    $(function () {
        var options = {
            cell_height: 80,
            vertical_margin: 10
        };
Pavel Reznikov's avatar
typo    
Pavel Reznikov committed
47
        $('.grid-stack').gridstack(options);
Pavel Reznikov's avatar
Pavel Reznikov committed
48
49
50
51
    });
    </script>
```

Pavel Reznikov's avatar
Pavel Reznikov committed
52
53
## Options

Pavel Reznikov's avatar
Pavel Reznikov committed
54
55
56
57
- `auto` - if `false` it tells to do not initialize existing items (default: true)
- `cell_height` - one cell height (default: 60)
- `handle` - draggable handle selector (default: '.grid-stack-item-content')
- `float` - enable floating widgets (default: false)
Pavel Reznikov's avatar
Pavel Reznikov committed
58
- `item_class` - widget class (default: 'grid-stack-item')
Pavel Reznikov's avatar
Pavel Reznikov committed
59
- `min_width` - minimal width. If window width is less grid will be shown in one-column mode (default: 768)
Pavel Reznikov's avatar
Pavel Reznikov committed
60
61
- `placeholder_class` - class for placeholder (default: 'grid-stack-placeholder')
- `vertical_margin` - vertical gap size (default: 20)
Pavel Reznikov's avatar
Pavel Reznikov committed
62
- `width` - amount of columns (default: 12)
Pavel Reznikov's avatar
Pavel Reznikov committed
63

Pavel Reznikov's avatar
Pavel Reznikov committed
64
65
## Grid attributes

66
 - `data-gs-width` - amount of columns
Pavel Reznikov's avatar
Pavel Reznikov committed
67

Pavel Reznikov's avatar
Pavel Reznikov committed
68
69
70
71
72
73
## Item attributes

- `data-gs-x`, `data-gs-y` - element position
- `data-gs-width`, `data-gs-height` - element size
- `data-gs-max-width`, `data-gs-min-width`, `data-gs-max-height`, `data-gs-min-height` - element constraints
- `data-gs-no-resize` - disable element resizing
Pavel Reznikov's avatar
Pavel Reznikov committed
74
- `data-gs-no-move` - disable element moving 
Pavel Reznikov's avatar
Pavel Reznikov committed
75
76
- `data-gs-auto-position` - tells to ignore `data-gs-x` and `data-gs-y` attributes and to place element to the first 
    available position
Pavel Reznikov's avatar
Pavel Reznikov committed
77
78
79
80
81
82
83
84
    
## Events

### onchange 

Occurs when widgets change their position/size

```javascript
Pavel Reznikov's avatar
samples    
Pavel Reznikov committed
85
86
87
88
    var serialize_widget_map = function (items) {
        console.log(items);
    };

Pavel Reznikov's avatar
Pavel Reznikov committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    $('.grid-stack').on('change', function (e, items) {
        serialize_widget_map(items);
    });
```

## API

### add_widget(el, x, y, width, height, auto_position)

Creates new widget.

Parameters:

- `el` - widget to add
- `x`, `y`, `width`, `height` - widget position/dimensions (Optional)
- `auto_position` - if `true` then `x`, `y` parameters will be ignored and widget will be places on the first available
position

Pavel Reznikov's avatar
Pavel Reznikov committed
107
```javascript
Pavel Reznikov's avatar
Pavel Reznikov committed
108
$('.grid-stack').gridstack();
Pavel Reznikov's avatar
Pavel Reznikov committed
109

Pavel Reznikov's avatar
Pavel Reznikov committed
110
var grid = $('.grid-stack').data('gridstack');
Pavel Reznikov's avatar
Pavel Reznikov committed
111
112
113
grid.add_widget(el, 0, 0, 3, 2, true);
```

Pavel Reznikov's avatar
Pavel Reznikov committed
114
115
116
117
118
119
### remove_widget(el)

Removes widget from the grid.

Parameters:

Pavel Reznikov's avatar
samples    
Pavel Reznikov committed
120
- `el` - widget to remove
Pavel Reznikov's avatar
Pavel Reznikov committed
121

Pavel Reznikov's avatar
Pavel Reznikov committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
## Use with knockout.js

```javascript
ko.components.register('dashboard-grid', {
    viewModel: {
        createViewModel: function (params, componentInfo) {
            var ViewModel = function (params, componentInfo) {
                var grid = null;

                this.widgets = params.widgets;

                this.afterAddWidget = function (items) {
                    _.each(items, function (item) {
                        item = $(item);

                        if (grid == null) {
                            grid = $(componentInfo.element).find('.grid-stack').gridstack({
                                auto: false
                            }).data('gridstack');
                        }

                        grid.add_widget(item);
144
145
146
                        ko.utils.domNodeDisposal.addDisposeCallback(item[0], function () {
                            grid.remove_widget(item);
                        });
Pavel Reznikov's avatar
Pavel Reznikov committed
147
148
149
150
151
152
153
154
155
156
157
                    }, this);
                };

            };

            return new ViewModel(params, componentInfo);
        }
    },
    template: [
        '<div class="grid-stack">',
        '   <!-- ko foreach: widgets, afterRender: afterAddWidget -->',
Pavel Reznikov's avatar
Pavel Reznikov committed
158
159
160
161
162
        '       <div class="grid-stack-item" data-bind="attr: {',
        '               \'data-gs-x\': x, \'data-gs-y\': y,',
        '               \'data-gs-width\': width, \'data-gs-height\': height}">',
        '           <span data-bind="text: $index"></span>',
        '       </div>',
Pavel Reznikov's avatar
Pavel Reznikov committed
163
164
165
166
167
168
169
170
171
172
173
174
175
        '   <!-- /ko -->',
        '</div>'
    ].join('\n')
});
```

and HTML:

```html
<div data-bind="component: {name: 'dashboard-grid', params: $data}"></div>
```


176
177
178
Changes
=======

Pavel Reznikov's avatar
Pavel Reznikov committed
179
180
181
182
183
#### v0.1.1 (development version)

- add `float` option
- fix default css rule for inner content

184
185
#### v0.1.0 (2014-11-18)

Pavel Reznikov's avatar
Pavel Reznikov committed
186
187
Very first version.

188

Pavel Reznikov's avatar
license    
Pavel Reznikov committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
License
=======

The MIT License (MIT)

Copyright (c) 2014 Pavel Reznikov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.