VUE tab切换,通过点击 按钮切换tab

最近写了一个关于医疗的前后端分离的管理系统,写这篇文章纯属是记录项目中的小组件,求大佬们指点(不喜勿喷)

在这里插入图片描述
在这里插入图片描述
好了好了,废话少说,上代码

布局部分

    <div class="diseaseQuestionnaire">
      <div class="dq_head" @mousedown="dragMove($event)">
        <span>问卷</span>
        <span class="add_Zytzbx_head_r fr clearfix" @click="hide_diseaseQuestionnaire()"></span>
      </div>
      <div class="dq_con">
        <ul class="dp_con_head">
          <!-- <li @click="tab(index)" v-for="(item,index) in items" :key="index" :class="{acts : index===curId}">{{item.item}}</li> -->
          <li @click="tab(index)" v-for="(item,index) in items" :key="index" :class="index===curId ? 'active':''">{{item.item}}</li>
        </ul>
        <div class="dp_con_body">
          <div v-show="curId==0">个人基本信息</div>
          <div v-show="curId==1">个人疾病史</div>
          <div v-show="curId==2">个人用药史</div>
          <div v-show="curId==3">家族疾病史</div>
          <div v-show="curId==4">吸烟</div>
          <div v-show="curId==5">膳食</div>
        </div>
      </div>
      <div class="dq_foot">
        <div>
          <span @click="previous_btn()">上一步</span>
          <span @click="next_btn()">下一步</span>
          <span>暂存问卷</span>
        </div>
      </div>
    </div>

js部分

data(){
    return {
         // 选项卡
      curId: 0,
      items: [
        { item: '个人基本信息' },
        { item: '个人疾病史' },
        { item: '个人用药史' },
        { item: '家族疾病史' },
        { item: '吸烟' },
        { item: '膳食' }
      ],
    }
}


    // 拖拽
    dragMove(e) {
      let odiv = e.target; // 获取目标元素
      let aaa = e.target.parentElement; // 获取目标元素的父级元素
      //计算出鼠标相对点击元素的位置,e.clientX获取的是鼠标的位置,OffsetLeft是元素相对于外层元素的位置
      let x = e.clientX - aaa.offsetLeft;
      let y = e.clientY - aaa.offsetTop;
      document.onmousemove = (e) => {
        // 获取拖拽元素的位置
        let left = e.clientX - x;
        let top = e.clientY - y;
        this.positionX = left;
        this.positionY = top;
        //console.log(document.documentElement.clientHeight,aaa.offsetHeight)
        // 把拖拽元素 放到 当前的位置
        if (left <= 0) {
          left = 0;
        } else if (
          left >=
          document.documentElement.clientWidth - aaa.offsetWidth
        ) {
          //document.documentElement.clientWidth 屏幕的可视宽度
          left = document.documentElement.clientWidth - aaa.offsetWidth;
        }
        if (top <= 0) {
          top = 0;
        } else if (
          top >=
          document.documentElement.clientHeight - aaa.offsetHeight
        ) {
          // document.documentElement.clientHeight 屏幕的可视高度
          top = document.documentElement.clientHeight - aaa.offsetHeight;
        }
        aaa.style.left = left + "px";
        aaa.style.top = top + "px";
      };
      // 为了防止 火狐浏览器 拖拽阴影问题
      document.onmouseup = (e) => {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    },


    // 选项卡
    tab(index) {
      this.curId = index;
    },
    // 隐藏问卷
    hide_diseaseQuestionnaire() {
      $('.diseaseQuestionnaire').hide();
    },
    

    // 上一步
    previous_btn() {
      if (this.curId == 0) {
        // console.log('当前点击的是', this.curId);
        this.isSuc = true;
        setTimeout(() => {
          this.isSuc = false;
        }, 1000);
        this.sucMsg = '当前已是第一页';
      } else if (this.curId < this.items.length) {
        this.curId--;
        console.log(this.curId);
      }
    },
    // 下一步
    next_btn() {
      if (this.curId < this.items.length - 1) {
        this.curId++;
        console.log(this.curId);

      } else if (this.curId === this.items.length - 1) {
        console.log(this.curId === this.items.length - 1);
        this.isSuc = true;
        setTimeout(() => {
          this.isSuc = false;
        }, 1000);
        this.sucMsg = '当前已是最后一页';
        return
      }

    },

CSS部分


.diseaseQuestionnaire {
  width: 1000px;
  height: 800px;
  position: fixed;
  left: 30%;
  top: 10%;
  background-color: #fff;
  border: 1px solid #ccc;
}
.dq_head {
  width: 100%;
  height: 30px;
  line-height: 30px;
  background-color: #6caef5;
  padding-left: 10px;
  box-sizing: border-box;
  cursor: move;
}
.dq_head span {
  font-size: 12px;
  font-weight: bold;
  color: #404040;
  height: 16px;
}
.dq_con {
  width: 100%;
  height: 700px;
}
.dp_con_head {
  width: 1000px;
  border-bottom: 1px #c3d9e0 solid;
  background-color: #daeef5;
  word-break: keep-all;
  white-space: nowrap;
  overflow-x: scroll;
  overflow-y: auto;
  color: #404040;
  font-size: 16px;
}
.dp_con_head li {
  display: inline-block;
  border: 1px #c3d9e0 solid;
  border-bottom: none;
  padding: 5px 10px;
  font-size: 16px;
  cursor: pointer;
  background-color: #daeef5;
}
/*滚动条整体样式*/
.dp_con_head::-webkit-scrollbar {
  width: 4px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 4px;
}

/*滚动条里面小方块*/
.dp_con_head::-webkit-scrollbar-thumb {
  border-radius: 5px;
  -webkit-box-shadow: inset005pxrgba(0, 0, 0, 0.2);
  background: rgba(0, 0, 0, 0.2);
}
/*滚动条里面轨道*/
.dp_con_head::-webkit-scrollbar-track {
    -webkit-box-shadow: inset005pxrgba(0, 0, 0, 0.2);
  border-radius: 0;
  background: rgba(0, 0, 0, 0.1);
}
.dp_con_body {
  height: 660px;
  overflow-y: scroll;
  padding: 30px;
  box-sizing: border-box;
}
.dp_con_body::-webkit-scrollbar {
  width: 4px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 4px;
}
.dp_con_body::-webkit-scrollbar-thumb {
  border-radius: 5px;
  -webkit-box-shadow: inset005pxrgba(0, 0, 0, 0.2);
  background: rgba(0, 0, 0, 0.2);
}
.dp_con_body::-webkit-scrollbar-track {
    -webkit-box-shadow: inset005pxrgba(0, 0, 0, 0.2);
  border-radius: 0;
  background: rgba(0, 0, 0, 0.1);
}
.dp_con_body_item_a {
  padding: 10px;
  box-sizing: border-box;
}
.dp_con_body_item_a label {
  margin: 0 20px;
}
.dq_foot {
  width: 100%;
  height: 70px;
  position: relative;
  background-color: #eee;
}
.dq_foot > div {
  position: absolute;
  right: 20px;
  top: 20px;
}
.dq_foot > div span {
  display: inline-block;
  padding: 5px 10px;
  background-color: #eba828;
  color: #fff;
  cursor: pointer;
  margin: 0 10px;
}
.active {
  background-color: #fff !important;
  border-bottom: none;
  color: #000;
}


Vue.js 是一个用于构建用户界面的渐进式框架。在Vue中实现tab切换与表格的结合使用,通常是通过动态组件或者条件渲染的方式,根据当前选中的tab来显示相应的表格内容。 以下是一个简单的例子来说明如何使用Vue实现tab切换显示不同表格的功能: 1. 首先定义数据结构,包括tab列表和对应的表格数据。 2. 使用`v-for`指令来遍历tab列表,为每个tab创建一个按钮作为切换入口。 3. 使用`v-if`或`v-show`指令根据当前选中的tab来决定哪个表格显示。 ```html <template> <div> <!-- tab切换按钮 --> <div v-for="(tab, index) in tabs" :key="index" class="tab-button"> <button @click="selectTab(index)">{{ tab.name }}</button> </div> <!-- 根据选中的tab显示对应的表格 --> <div v-if="currentTab === 0"> <table> <tr> <th>ID</th> <th>Name</th> </tr> <tr v-for="item in tableData1" :key="item.id"> <td>{{ item.id }}</td> <td>{{ item.name }}</td> </tr> </table> </div> <div v-else-if="currentTab === 1"> <table> <tr> <th>ID</th> <th>Age</th> </tr> <tr v-for="item in tableData2" :key="item.id"> <td>{{ item.id }}</td> <td>{{ item.age }}</td> </tr> </table> </div> <!-- 更多表格... --> </div> </template> <script> export default { data() { return { tabs: [ { name: 'Table 1', index: 0 }, { name: 'Table 2', index: 1 }, // 更多tab... ], currentTab: 0, tableData1: [ // 表格1的数据 ], tableData2: [ // 表格2的数据 ], // 更多表格数据... }; }, methods: { selectTab(index) { this.currentTab = index; } } } </script> <style> /* 添加一些基本的样式 */ .tab-button { /* 样式代码 */ } </style> ``` 在这个例子中,我们定义了一个`tabs`数组来存储tab信息,`currentTab`来跟踪当前选中的tab索引,以及两个表格的数据`tableData1`和`tableData2`。通过点击不同的tab切换按钮,会调用`selectTab`方法更新`currentTab`的值,从而根据当前选中的tab显示对应的表格内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

攻城狮很忙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值