我是javascript和gridstack的新手。我的工作是创建一个基于gridstack的仪表板。我的目标最接近的示例是演示示例knockout.html。此外,我需要添加基于此示例的几个新功能。这是我面临的问题。
我需要为每个小部件添加一个下拉菜单,并将其添加到模板中,请参阅下面的代码。现在我使用一个按钮来切换下拉菜单。我可以用右键单击鼠标来显示下拉菜单吗?
当我添加名为toggleDropdownMenu的方法时,我收到类型错误" TypeError:this.toggleDropdownMenu不是函数",是否应该在某处定义此函数?
我想将指定的小部件设为静态。我在创建类似staticGrid的小部件时添加了一个attr设置:true但不起作用。怎么解决这个问题?
对于每个仪表板,每个小部件的下拉菜单都不同。如何为每个小部件附加包含不同项目的下拉菜单?
请参考代码......非常感谢您的任何建议!
<!DOCTYPE html>
<html lang="en">
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dash board demo</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="./dist/gridstack.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="./dist/gridstack.js"></script>
<style type="text/css">
.grid-stack {
background: lightgoldenrodyellow;
}
.grid-stack-item-content {
color: #2c3e50;
text-align: center;
background-color: #18bc9c;
}
.grid-stack-item-content button{
background-color: Transparent;
background-repeat:no-repeat;
border: none;
cursor:pointer;
overflow: hidden;
outline:none;
color: white;
position: relative;
}
.grid-stack-item-content .dropdown-submenu {
position: relative;
}
.grid-stack-item-content .dropdown-submenu .dropdown-menu {
top: 0;
left: 100%;
margin-top: -1px;
}
</style>
</head>
<body>
<div class="container-fluid">
<h1>Dashboard Demo</h1>
<div>
<button data-bind="click: addNewWidget">Add new widget</button>
</div>
<br>
<div data-bind="component: {name: 'dashboard-grid', params: $data}"></div>
</div>
<script type="text/javascript">
ko.components.register('dashboard-grid', {
viewModel: {
createViewModel: function (controller, componentInfo) {
var ViewModel = function (controller, componentInfo) {
var grid = null;
this.widgets = controller.widgets;
this.afterAddWidget = function (items) {
if (grid == null) {
grid = $(componentInfo.element).find('.grid-stack').gridstack({
auto: false
}).data('gridstack');
}
var item = _.find(items, function (i) { return i.nodeType == 1 });
grid.addWidget(item);
ko.utils.domNodeDisposal.addDisposeCallback(item, function () {
grid.removeWidget(item);
});
};
};
return new ViewModel(controller, componentInfo);
}
},
template:
[
'<div class="grid-stack" data-bind="foreach: {data: widgets, afterRender: afterAddWidget}">',
' <div class="grid-stack-item" data-bind="attr: {\'data-gs-x\': $data.x, \'data-gs-y\': $data.y, \'data-gs-width\': $data.width, \'data-gs-height\': $data.height, \'data-gs-auto-position\': $data.auto_position}">',
' <div class="grid-stack-item-content">',
//' <button data-bind="click: $root.deleteWidget">Delete me</button>',
' <div class="dropdown">',
' <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Click me',
' <span class="caret"></span>',
' </button>',
' <ul class="dropdown-menu">',
' <li data-bind="click: $root.deleteWidget"><a tabindex="-1" href="#">Delete</a></li>',
' <li><a tabindex="-1" href="#">Change Color</a></li>',
' <li><a tabindex="-1" href="#">Copy to</a></li>',
' <li class="dropdown-submenu">',
' <a class="plan" tabindex="-1" href="#">New dropdown <span class="caret"></span></a>',
' <ul class="dropdown-menu">',
' <li><a tabindex="-1" href="#">2nd level dropdown</a></li>',
' <li><a tabindex="-1" href="#">2nd level dropdown</a></li>',
' <li class="dropdown-submenu">',
' <a class="test" href="#">Another dropdown <span class="caret"></span></a>',
' <ul class="dropdown-menu">',
' <li><a href="#">3rd level dropdown</a></li>',
' <li><a href="#">3rd level dropdown</a></li>',
' </ul>',
' </li>',
' </ul>',
' </li>',
' </ul>',
' </div>',
' </div>',
' </div>',
'</div> '
].join('')
});
$(function () {
var Controller = function (widgets) {
var self = this;
this.widgets = ko.observableArray(widgets);
this.addNewWidget = function (i) {
this.widgets.push({
x: 0,
y: 0,
id: 'widget'+i,
width: i== 0 ? 2 : 1, //Math.floor(1 + 3 * Math.random()),
height: i== 0 ? 2 : 1, //Math.floor(1 + 3 * Math.random()),
auto_position: true
});
return false;
};
this.deleteWidget = function (item) {
self.widgets.remove(item);
return false;
};
/**
this.toggleDropdownMenu(function(){
$('.dropdown-submenu a.test').on("click", function(e)
{
$(this).next('ul').toggle();
e.stopPropagation();
e.preventDefault();
});
});
**/
};
var widgets = [
{x: 0, y: 0, width: 2, height: 2, staticGrid: true },
{x: 2, y: 0, width: 1, height: 1},
{x: 3, y: 1, width: 2, height: 2},
{x: 1, y: 2, width: 1, height: 1}
];
var controller = new Controller(widgets);
ko.applyBindings(controller);
});
</script>
</body>
</html>