日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

el-table操作列的按鈕超過三個時,動態計算,將多余的按鈕放入更多el-dropdown-menu中

作者:湛海不過深藍 更新時間: 2023-07-09 編程語言

一下是封裝好的操作列組件:OperateBtn

<template>
  <div class="button-group">
    <div
      class="visibleButtons"
      v-for="(button, index) in visibleButtons"
      :key="index"
    >
      <el-button
        link
        type="primary"
        :icon="button.icon"
        @click="button.click"
        v-if="button.isshow && button.text !== '刪除'"
      >
        {{ button.text }}
      </el-button>
      <el-popconfirm
        title="確定刪除嗎?"
        width="200px"
        @confirm="button.click"
        :teleported="false"
        placement="bottom"
        v-if="button.isshow && button.text === '刪除'"
      >
        <template #reference>
          <el-button
            link
            type="primary"
            :icon="button.icon"
            @click.stop="button.del"
          >
            {{ button.text }}
          </el-button>
        </template>
      </el-popconfirm>
    </div>

    <el-dropdown v-if="showMore">
      <span class="el-dropdown-link">
        更多
        <el-icon class="el-icon--right">
          <arrow-down />
        </el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item
            v-for="(button, index) in hiddenButtons"
            :key="index"
          >
            <span v-if="button.text !== '刪除'" @click="button.click">
              {{ button.text }}
            </span>
            <el-popconfirm
              title="確定要刪除嗎?"
              confirmButtonText="確定"
              cancelButtonText="取消"
              @confirm="button.click"
              v-if="button.text === '刪除'"
            >
              <template #reference>
                <el-button link @click.stop="button.del">
                  {{ button.text }}
                </el-button>
              </template>
            </el-popconfirm>
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
<script lang="ts">
import { defineComponent, computed, ref } from "vue";

interface Item {
  text: string;
  icon: string;
  isshow: boolean;
  click: () => void;
  del?: () => void;			// 這個是可選屬性  不給也沒事
}

export default defineComponent({
  name: "ButtonGroup",
  props: {
    buttons: {
      type: Array as () => Item[],
      default: () => [],
    },
  },
  setup(props) {
    const isshowButtons = () => {
      return props.buttons?.filter((item: any) => {
        return item.isshow;
      });
    };
    let resultBtn = ref(isshowButtons());
    const visibleButtons = computed(() => {
      if (resultBtn.value.length <= 3) {
        return resultBtn.value;
      } else {
        return resultBtn.value.slice(0, 2);
      }
    });
    const hiddenButtons = computed(() => {
      if (resultBtn.value.length <= 3) {
        return [];
      } else {
        return resultBtn.value.slice(2);
      }
    });
    const showMore = computed(() => {
      return resultBtn.value.length > 3;
    });
    const del = () => {};
    return { visibleButtons, hiddenButtons, showMore, del };
  },
});
</script>

<style lang="scss" scoped>
.button-group {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: center;
}
</style>

下面是在el-table中的使用:

<el-table>
	<el-table-column align="right">
      <operate-btn
		:key="scope.row.id"			// 子組件必須綁定一個key,不然切換表格分頁的時候,表格操作列不會更新
		:buttons="[			// 傳給子組件的值,用來渲染按鈕,包括:按鈕文字、圖標、按鈕的點擊事件、以及按鈕的顯示與隱藏控制
            {
              text: '編輯',
              icon: 'Edit',
              click: () => handleEdit(row),
              isshow: row.status !== 1 ? true : false,
            },
            {
              text: '訂閱詳情',
              icon: 'Warning',
              click: () => toAuthDetail(row),
              isshow: row.status === 2 ? true : false,
            },
            {
              text: '重新上架',
              icon: 'RefreshRight',
              click: () => handleRack(row),
              isshow: row.status === 3 ? true : false,
            },
            {
              text: '下架申請',
              icon: 'BottomRight',
              click: () => handleUnShelve(row),
              isshow: row.status === 2 ? true : false,
            },
            {
              text: '上架申請',
              icon: 'TopRight',
              click: () => handleRack(row),
              isshow: row.status === 0 || row.status === 4 ? true : false,
            },
            {
              text: '刪除',
              icon: 'Delete',
              click: () => delApi(row.id),
              isshow:
                row.status === 0 || row.status === 3 || row.status === 4
                  ? true
                  : false,
              del: () => del($index),
            },
            // 只有微服務平臺和已上架可見
            {
              text: '設置流控規則',
              icon: 'DataAnalysis',
              click: () => openFlowcontrol(row),
              isshow: row.status === 2 && row.source === 1 ? true : false,
            },
            {
              text: '設置超時規則',
              icon: 'Timer',
              click: () => openTimeout(row),
              isshow: row.status === 2 && row.source === 1 ? true : false,
            },
            {
              text: '監控',
              icon: 'Monitor',
              click: () => openMonitor(row),
              isshow: row.status === 2 && row.source === 1 ? true : false,
            },
          ]
		/>
    </el-table-column>
</el-table>

原文鏈接:https://blog.csdn.net/bbt953/article/details/131202872

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新