gridstack.js API
Table of Contents generated with DocToc
- Options 配置
- Grid attributes 网格属性
- Item attributes 元素属性
- Events 事件
-
- added(event, items) 添加
- change(event, items) 改变
- disable(event) 停用
- dragstart(event, ui) 拖拽开始
- dragstop(event, ui) 拖拽结束
- enable(event) 启用
- removed(event, items) 移除
- resizestart(event, ui) 缩放开始
- resizestop(event, ui) 缩放结束
- API
-
- addWidget(el[, x, y, width, height, autoPosition, minWidth, maxWidth, minHeight, maxHeight, id])添加控件
- batchUpdate() 批量更新
- cellHeight() 单位元素高度
- cellHeight(val)
- cellWidth() 单位元素宽度
- commit() 提交变更
- destroy([detachGrid]) 销毁
- disable()禁用
- enable()启用
- enableMove(doEnable, includeNewWidgets)启用移动
- enableResize(doEnable, includeNewWidgets)启用缩放
- getCellFromPixel(position[, useOffset])通过像素点获取元素点对应的网格宽度和高度
- isAreaEmpty(x, y, width, height)判断区域是否为空
- locked(el, val)锁定
- makeWidget(el)
- maxHeight(el, val)
- minHeight(el, val)
- maxWidth(el, val)
- minWidth(el, val)
- movable(el, val)
- move(el, x, y)
- removeWidget(el[, detachNode])
- removeAll([detachNode])
- resize(el, width, height)
- resizable(el, val)
- setAnimation(doAnimate)
- setGridWidth(gridWidth, doNotPropagate)
- setStatic(staticValue)
- update(el, x, y, width, height)
- verticalMargin(value, noUpdate)
- willItFit(x, y, width, height, autoPosition)
- Utils
Options
acceptWidgets
- iftrue
of jquery selector the grid will accept widgets dragged from other grids or from outside (default:false
) See examplealwaysShowResizeHandle
- iftrue
the resizing handles are shown even if the user is not hovering over the widget (default:false
) 如果设置成true
那么控制大小缩放的手柄即时在用户没有将鼠标移动到组件上都会一直显示。 (默认值:false
)animate
- turns animation on (default:false
) 打开动画效果 (默认值:false
)auto
- iffalse
gridstack will not initialize existing items (default:true
) 如果设置成false
gridstack 将不会初始化已经存在的项 (默认值:true
)cellHeight
- one cell height (default:60
). Can be:一个单元格的高度-
- an integer (px) 整形
- a string (ex: '10em', '100px', '10rem') 字符串
- 0 or null, in which case the library will not generate styles for rows. Everything must be defined in CSS files.0或者null,在这种情况下库将不会为rows自动生成样式。任何元素都必须在css文件被中定义。
'auto'
- height will be calculated from cell width. auto 高度将根据单元格宽度计算
ddPlugin
- class that implement drag'n'drop functionallity for gridstack. Iffalse
grid will be static. (default:null
- first available plugin will be used) 实现gridstack拖动的功能的类,如果设置成faslse,网格将是静态的。disableDrag
- disallows dragging of widgets (default:false
).disableResize
- disallows resizing of widgets (default:false
).draggable
- allows to override jQuery UI draggable options. (default:{handle: '.grid-stack-item-content', scroll: false, appendTo: 'body'}
)允许重写JQuery UI的拖拽配置项。handle
- draggable handle selector (default:'.grid-stack-item-content'
) 可拖动手柄的选择器 (默认值:'.grid-stack-item-content'
)handleClass
- draggable handle class (e.g.'grid-stack-item-content'
). If sethandle
is ignored (default:null
)height
- maximum rows amount. Default is0
which means no maximum rows 最大行数float
- enable floating widgets (default:false
) See example 启用浮动控件itemClass
- widget class (default:'grid-stack-item'
) 部件类,默认是‘grid-stack-item’minWidth
- minimal width. If window width is less, grid will be shown in one-column mode (default:768
)最小宽度oneColumnModeClass
- class set on grid when in one column mode (default: 'grid-stack-one-column-mode')placeholderClass
- class for placeholder (default:'grid-stack-placeholder'
) 占位符类placeholderText
- placeholder default content (default:''
) 占位符的文本信息resizable
- allows to override jQuery UI resizable options. (default:{autoHide: true, handles: 'se'}
)允许重写JQueryUI的缩放配置项removable
- iftrue
widgets could be removed by dragging outside of the grid. It could also be a jQuery selector string, in this case widgets will be removed by dropping them there (default:false
) See example如果设置成true控件将能被拖拽到表格的外面。他也可以是一个JQuery的选择器的名称字符串,在这种情况下控件将会被遗弃。removeTimeout
- time in milliseconds before widget is being removed while dragging outside of the grid. (default:2000
)移除延时rtl
- iftrue
turns grid to RTL. Possible values aretrue
,false
,'auto'
(default:'auto'
) See examplestaticGrid
- makes grid static (defaultfalse
). If true widgets are not movable/resizable. You don't even need jQueryUI draggable/resizable. A CSS classgrid-stack-static
is also added to the container.verticalMargin
- vertical gap size (default:20
). Can be:垂直间距大小-
- an integer (px)
- a string (ex: '2em', '20px', '2rem')
width
- amount of columns (default:12
) 列的数量
Grid attributes
data-gs-animate
- turns animation on 开启动画效果data-gs-width
- amount of columns 列的数量data-gs-height
- maximum rows amount. Default is0
which means no maximum rows. 最大行数量。默认值是0
意思是没有最大的行数data-gs-current-height
- current rows amount. Set by the library only. Can be used by the CSS rules. 当前行的数量。只能通过库文件配置,可以使用css规则
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 禁用元素缩放data-gs-no-move
- disable element moving 禁用元素移动data-gs-auto-position
- tells to ignoredata-gs-x
anddata-gs-y
attributes and to place element to the first available positiondata-gs-locked
- the widget will be locked. It means another widget wouldn't be able to move it during dragging or resizing. The widget can still be dragged or resized. You need to adddata-gs-no-resize
anddata-gs-no-move
attributes to completely lock the widget. - 部件将会被锁定,这意味着另一个部件在拖动和缩放的时候不允许移动它 。但是该部件仍然能够被拖动和缩放,所以你需要添加data-gs-no-resize
和data-gs-no-move
属性来完全锁定该部件。
Events
added(event, items)
$('.grid-stack').on('added', function(event, items) { for (var i = 0; i < items.length; i++) { console.log('item added'); console.log(items[i]); } });
change(event, items)
Occurs when adding/removing widgets or existing widgets change their position/size
var serializeWidgetMap = function(items) { console.log(items); }; $('.grid-stack').on('change', function(event, items) { serializeWidgetMap(items); });
disable(event)
$('.grid-stack').on('disable', function(event) { var grid = event.target; });
dragstart(event, ui)
$('.grid-stack').on('dragstart', function(event, ui) { var grid = this; var element = event.target; });
dragstop(event, ui)
$('.grid-stack').on('dragstop', function(event, ui) { var grid = this; var element = event.target; });
enable(event)
$('.grid-stack').on('enable', function(event) { var grid = event.target; });
removed(event, items)
$('.grid-stack').on('removed', function(event, items) { for (var i = 0; i < items.length; i++) { console.log('item removed'); console.log(items[i]); } });
resizestart(event, ui)
$('.grid-stack').on('resizestart', function(event, ui) { var grid = this; var element = event.target; });
resizestop(event, ui)
$(
'.grid-stack').
on(
'resizestop',
function(
event,
ui) {
var grid = this;
var element = event. target;
});
var grid = this;
var element = event. target;
});