调整浏览器大小时,网格堆栈Knockout JS小部件不会被删除

时间:2017-01-10 02:50:19

标签: javascript html

我对Gridstack和Knockout JS相对较新。

尝试弄清楚过去几天为什么在调整浏览器大小时不会正确删除小部件。 请注意,此问题会在所有浏览器上发生。

场景A:"不调整浏览器"

Here's the initial display of the code:

我可以在浏览器大小未更改时删除小部件。

场景B:"浏览器调整大小(垂直)"

添加小部件后(或者您可以刷新浏览器再次显示默认值),我已经垂直调整了浏览器的大小。

The widgets re-aligned

当我尝试删除小部件后重新删除小部件并不被删除时,这很奇怪。

这是Gridstack或Knockout的已知问题吗?

非常感谢任何帮助。

这里是代码:



<!DOCTYPE html>
<html lang="en">
	
<head>

	<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Investigation Sample 1</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://github.com/troolee/gridstack.js/tree/master/dist/gridstack.css"/>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/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="https://github.com/troolee/gridstack.js/tree/master/dist/gridstack.js"></script>
    <script src="https://github.com/troolee/gridstack.js/tree/master/dist/gridstack.jQueryUI.js"></script>

    <style type="text/css">

    	.grid-stack {
    		background: lightgoldenrodyellow;
    	}

    	.grid-stack-item-content {
    		color: #2c3e50;
    		text-align: center;
    		background-color: #18bc9c;
    	}

    </style>

</head>
<body>

	<div class="container-fluid">

		<h1> Investigation Sample 1</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 Button</button></div>',
                    '   </div>',
                    '</div> '
				].join('')

		});
		
		$(function () {
            var Controller = function (widgets) {
                var self = this;

                this.widgets = ko.observableArray(widgets);

                this.addNewWidget = function () {
                    this.widgets.push({
                        x: 0,
                        y: 0,
                        //width: Math.floor(1 + 3 * Math.random()),
                        //height: Math.floor(1 + 3 * Math.random()),
                        width: 2,
                        height: 3,
                        auto_position: true
                    });
                    return false;
                };

                this.deleteWidget = function (item) {
                    self.widgets.remove(item);
                    return false;
                };
            };

            var widgets = [
                {x: 0, y: 0, width: 2, height: 2},
                {x: 2, y: 0, width: 4, height: 2},
                {x: 6, y: 0, width: 2, height: 4},
                {x: 1, y: 2, width: 4, height: 2}
            ];

            var controller = new Controller(widgets);
            ko.applyBindings(controller);
        });

	</script>

</body>

</html>
&#13;
&#13;
&#13;

0 个答案:

没有答案