Vue-Grid-Layout分享一款好用的可拖拽组件

22 篇文章 0 订阅

在使用Grafana的过程中,发现Grafana关于视图页面中每一个面板都可拖拽,可随意放大放小,体验非常棒,F12看了Grafana的代码,看打包后的代码很像react,进一步css,看到有grid layout的字眼,然后尝试去搜了下有没有这方面的组件,一搜果然是有这样的组件的。首先看到的是react版本的组件,然后搜了下也有vue相关的。 以前也见过jquery的可拖拽组件demo,但是整体上感觉不如grid-layout优秀。

【1】 VUE Grid Layout.

【2】React Grid Layout

【3】jQuery响应式可拖拽的元素组件网格布局插件

每个组件在GitHub上的介绍还是比较清晰易懂,项目中主要用到了vue-grid-layout组件,下面简单介绍下。

执行如下命令安装

npm install vue-grid-layout --save

然后新增一个grid_layout.vue文件

<template>
  <div>
    <grid-layout
      :is-draggable="true"
      :is-resizable="true"
      :is-mirrored="false"
      :layoutList="testLayout">
    </grid-layout>
  </div>
</template>

<script>
  import gridLayout from './../comm/grid_layout'

  export default {
    data() {
      return {
        testLayout: [
          {"x": 0, "y": 0, "w": 4, "h": 7, "i": "0"},
          {"x": 4, "y": 0, "w": 4, "h": 7, "i": "1"},
          {"x": 8, "y": 0, "w": 4, "h": 7, "i": "2"},
          {"x": 0, "y": 7, "w": 4, "h": 6, "i": "3"},
          {"x": 0, "y": 13, "w": 4, "h": 5, "i": "4"},
          {"x": 4, "y": 7, "w": 4, "h": 11, "i": "5"},
          {"x": 8, "y": 7, "w": 4, "h": 6, "i": "6"},
          {"x": 8, "y": 13, "w": 4, "h": 5, "i": "7"}
        ]
      }
    },
    components: {
      gridLayout
    },
  }
</script>

<style scoped>
</style>
is-draggable 表示是否可拖拽,设置为true,说明是可以随意拖拽的
is-resizable 表示是否可缩放,设置为ture,说明可以通过鼠标调整每一区域的大小
layoutList   是绑定的数据实体,用来标识区域大小的

layoutList是一个对象数据,每一个对象如下
{"x": 0, "y": 0, "w": 4, "h": 7, "i": "0"}
其中:
x 标识该区域左上角点的x坐标
y 标识该区域左上角点的y坐标
w 是宽度
h 是高度
i 是唯一标示,表示必须唯一,且不同,不然,你会发现改变大小的时候,所有的都会被改变。。。
DragLinearLayout是一个LinearLayout, 他可是其子View在其范围内可拖动、可交换位置。默认情况下,子View是不可拖动的,你需要调用DragLinearLayout.setViewDraggable(child, child)方法让其可拖动。项目地址:https://github.com/justasm/DragLinearLayout 效果图:如何使用和使用LinearLayout一样:                        2. 让子View可拖动默认情况下子View是不可拖动的,你需要调用dragLinearLayout.setViewDraggable()让子View变为可拖动的。DragLinearLayout dragLinearLayout = (DragLinearLayout) findViewById(R.id.container); //让子view可拖动,默认是不能拖动的         for(int i = 1; i < dragLinearLayout.getChildCount(); i ){             View child = dragLinearLayout.getChildAt(i);             dragLinearLayout.setViewDraggable(child, child);          }可以动态添加可拖动子viewfinal View view = View.inflate(context, R.layout.view_layout, null); dragLinearLayout.addDragView(view, view.findViewById(R.id.view_drag_handle));   // ..   dragLinearLayout.removeDragView(view);使用OnViewSwapListener检测子view之间的排序变化事件:dragLinearLayout.setOnViewSwapListener(new DragLinearLayout.OnViewSwapListener() {     @Override     public void onSwap(View firstView, int firstPosition,             View secondView, int secondPosition) {         // update data, etc..     } });当在ScrollView中使用DragLinearLayout的时候,如果你想在拖拽的时候ScrollView也能滚动,需要调用setContainerScrollView(Scroll
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值