删除小部件后如何定位jquery Gridster? [英] How to position the jquery gridster after remove a widget?

查看:99
本文介绍了删除小部件后如何定位jquery Gridster?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以删除小部件,但是在定位其余小部件时遇到了问题.

I can remove widget but I am facing problem with positioning the remain widgets.

当我从顶部移除窗口小部件时,底部窗口小部件将自动移至顶部,看起来不错.但是,当我从左侧删除窗口小部件时,右侧窗口小部件不会移到左侧吗?

When I remove a widget from top the bottom widget will automatically get moved to top its looks good. But when I remove a widget from left the right widget not gets moved to left?

推荐答案

最后我解决了这个问题

<div class="gridster">
    <ul>
      <li data-row="1" data-col="1" data-sizex="2" data-sizey="2">0<span class="removeWidget">X</span></li>
      <li data-row="1" data-col="3" data-sizex="1" data-sizey="2">1<span class="removeWidget">X</span></li>
      <li data-row="1" data-col="4" data-sizex="1" data-sizey="1">2<span class="removeWidget">X</span></li>
      <li data-row="3" data-col="2" data-sizex="3" data-sizey="1">3<span class="removeWidget">X</span></li>
      <li data-row="4" data-col="1" data-sizex="1" data-sizey="1">4<span class="removeWidget">X</span></li>
      <li data-row="3" data-col="1" data-sizex="1" data-sizey="1">5<span class="removeWidget">X</span></li>
      <li data-row="4" data-col="2" data-sizex="1" data-sizey="1">6<span class="removeWidget">X</span></li>
      <li data-row="5" data-col="2" data-sizex="1" data-sizey="1">7<span class="removeWidget">X</span></li>
      <li data-row="4" data-col="4" data-sizex="1" data-sizey="1">8<span class="removeWidget">X</span></li>
      <li data-row="1" data-col="5" data-sizex="1" data-sizey="3">9<span class="removeWidget">X</span></li>
    </ul>
  </div>

<script type="text/javascript">
      var gridster;

      $(function()
      {
        create_gridster();
          function create_gridster()
          {
            gridster = $(".gridster ul").gridster({

              helper: 'clone',
              resize: {
                enabled: true,
                start: function (e, ui, $widget) {
                    console.log("started the resize event");
                },
                stop: function (e, ui, $widget) {
                    var newHeight = this.resize_coords.data.height;
                    var newWidth = this.resize_coords.data.width;
                }
              },
              draggable:
              {
                  enabled: true,
                  start: function(event, ui){
                    console.log("started the draggable event");
                  },
                  stop: function (e, ui, $widget) {

                        console.log($(this));
                        console.log($(ui)[0].$helper.context);
                         console.log("stoped the draggable event");
                    }

              }
            }).data('gridster');
            }




$(".removeWidget").click(function(e)
            {
                var $div=$(e.target).parent();
                //removed the selected widget
                gridster.remove_widget($div);

                //Get available columns
                var cols=[];
                $.each(gridster.serialize(),function(i,item){
                    if($.inArray(item.col,cols)===-1){
                        cols.push(item.col);
                    }
                });

                gridster.destroy(false);
                //remove element of the removed widget
                $div.remove();

                //Get empty column( no widgets in that column)
                var emptyCol=0;
                $.each(cols,function(i,item){
                    if($.inArray(i+1,cols)===-1)
                        emptyCol=i+1;
                });

                //Verify if any of the widget accupies the empty column
                var isEmptyCol=false;
                $("ul li").each(function(e){
                    var col=$(this).attr("data-col");
                    if(col<emptyCol && !isEmptyCol){
                        isEmptyCol=emptyCol-col===$(this).attr("data-sizex")?false:true;
                    }
                });

                //Fill the empty column from the next columns
                if(emptyCol!==0){
                    var rowCol={};
                    $("ul li").each(function(e){
                        var col=$(this).attr("data-col");
                        var row=$(this).attr("data-row");

                        if(col>emptyCol && isEmptyCol)
                        {
                            $(this).attr("data-col",col-1);
                            $(this).data("col",col-1);
                        }
                    });
                }

                //redraw the gridster
                create_gridster();
          });

      });

    </script>

这篇关于删除小部件后如何定位jquery Gridster?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆