Categories
Ant Design Vue

Ant Design Vue — Pagination and Steps

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Pagination

We can add pagination buttons with the a-pagination component.

For example, we can write:

<template>
  <div>
    <a-pagination v-model="current" :total="50" show-less-items/>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      current: 2
    };
  }
};
</script>

We have the a-pagination component with the v-model directive to bind the current page clicked to a reactive property.

total is the total number of pages.

show-less-items makes the button bar display fewer buttons.

We can listen to the showSizeChange event that is emitted when page size changes:

<template>
  <div>
    <a-pagination
      show-size-changer
      :default-current="3"
      :total="500"
      @showSizeChange="onShowSizeChange"
    />
  </div>
</template>

<script>
export default {
  name: "App",
  methods: {
    onShowSizeChange(current, pageSize) {
      console.log(current, pageSize);
    }
  }
};
</script>

current has the current page. pageSize has the page size.

Also, we can make the pagination buttons smaller with the size prop:

<template>
  <div>
    <a-pagination size="small" :total="50"/>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

And we can enable simple mode with the simple prop:

<template>
  <div>
    <a-pagination simple :default-current="2" :total="50"/>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We can listen to the change event to watch for page changes:

<template>
  <div>
    <a-pagination :current="current" :total="50" @change="onChange"/>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      current: 3
    };
  },
  methods: {
    onChange(current) {
      this.current = current;
    }
  }
};
</script>

The current parameter has the current page clicked.

The show-total prop lets us show the total by setting the prop to a function:

<template>
  <div>
    <a-pagination
      :total="100"
      :show-total="total => `Total ${total} items`"
      :page-size="20"
      :default-current="1"
    />
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Steps

We can add a component to show steps with the a-steps component:

<template>
  <div>
    <a-steps :current="1">
      <a-step>
        <template slot="title">Finished</template>
        <span slot="description">This is a description.</span>
      </a-step>
      <a-step title="In Progress" sub-title="Left 00:00:08" description="This is a description."/>
      <a-step title="Waiting" description="This is a description."/>
    </a-steps>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

The current prop sets the index of current step.

sub-title has the subtitle.

We can make it smaller with the with the size prop:

<template>
  <div>
    <a-steps :current="1" size="small">
      <a-step title="Finished"/>
      <a-step title="In Progress"/>
      <a-step title="Waiting"/>
    </a-steps>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Also, we can add the steps with icons:

<template>
  <div>
    <a-steps :current="1">
      <a-step status="finish" title="Login">
        <a-icon slot="icon" type="user"/>
      </a-step>
      <a-step status="finish" title="Verification">
        <a-icon slot="icon" type="solution"/>
      </a-step>
      <a-step status="process" title="Pay">
        <a-icon slot="icon" type="loading"/>
      </a-step>
    </a-steps>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We can control the steps with the current prop:

<template>
  <div>
    <a-steps :current="current">
      <a-step v-for="item in steps" :key="item.title" :title="item.title"/>
    </a-steps>
    <div class="steps-content">{{ steps[current].content }}</div>
    <div class="steps-action">
      <a-button v-if="current < steps.length - 1" type="primary" [@click](http://twitter.com/click "Twitter profile for @click")="next">Next</a-button>
      <a-button
        v-if="current == steps.length - 1"
        type="primary"
        [@click](http://twitter.com/click "Twitter profile for @click")="$message.success('Processing complete!')"
      >Done</a-button>
      <a-button v-if="current > 0" style="margin-left: 8px" [@click](http://twitter.com/click "Twitter profile for @click")="prev">Previous</a-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      current: 0,
      steps: [
        {
          title: "First",
          content: "First-content"
        },
        {
          title: "Second",
          content: "Second-content"
        },
        {
          title: "Last",
          content: "Last-content"
        }
      ]
    };
  },
  methods: {
    next() {
      this.current++;
    },
    prev() {
      this.current--;
    }
  }
};
</script>

The button changes the current reactive property to move through the steps.

Conclusion

We can add steps display and pagination buttons to our Vue app with Ant Design Vue.

Categories
Ant Design Vue

Ant Design Vue — Menu Theme and Page Header

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Menu Theme

We can set the theme prop to change the menu theme:

<template>
  <div id="app">
    <a-menu
      style="width: 256px"
      :default-selected-keys="['1']"
      :default-open-keys="['sub1']"
      mode="inline"
      :theme="theme"
      :selected-keys="[current]"
      @click="handleClick"
    >
      <a-menu-item key="1">
        <a-icon type="mail"/>Navigation One
      </a-menu-item>
      <a-menu-item key="2">
        <a-icon type="calendar"/>Navigation Two
      </a-menu-item>
      <a-sub-menu key="sub1">
        <span slot="title">
          <a-icon type="appstore"/>
          <span>Navigation Three</span>
        </span>
        <a-menu-item key="3">Option 3</a-menu-item>
        <a-menu-item key="4">Option 4</a-menu-item>
        <a-sub-menu key="sub1-2" title="Submenu">
          <a-menu-item key="5">Option 5</a-menu-item>
          <a-menu-item key="6">Option 6</a-menu-item>
        </a-sub-menu>
      </a-sub-menu>
      <a-sub-menu key="sub2">
        <span slot="title">
          <a-icon type="setting"/>
          <span>Navigation Four</span>
        </span>
        <a-menu-item key="7">Option 7</a-menu-item>
        <a-menu-item key="8">Option 8</a-menu-item>
      </a-sub-menu>
    </a-menu>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      current: "1",
      theme: "dark"
    };
  },
  methods: {
    handleClick(e) {
      console.log("click ", e);
      this.current = e.key;
    },
    changeTheme(checked) {
      this.theme = checked ? "dark" : "light";
    }
  }
};
</script>

Page Header

We can add a page header with the a-page-header component:

<template>
  <div id="app">
    <a-page-header
      style="border: 1px solid rgb(235, 237, 240)"
      title="Title"
      sub-title="This is a subtitle"
      @back="() => null"
    />
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We add the title and sub-title props to add the title and subtitles.

The back event is emitted when we click the back button.

We can add it with breadcrumbs with the breadecrumbs prop:

<template>
  <div id="app">
    <a-page-header
      style="border: 1px solid rgb(235, 237, 240)"
      title="Title"
      :breadcrumb="{ props: { routes } }"
      sub-title="This is a subtitle"
    />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      routes: [
        {
          path: "index",
          breadcrumbName: "First-level Menu"
        },
        {
          path: "first",
          breadcrumbName: "Second-level Menu"
        }
      ]
    };
  }
};
</script>

We can populate the extra slot of the a-page-header with our own content:

<template>
  <div id="app">
    <a-page-header
      style="border: 1px solid rgb(235, 237, 240)"
      title="Title"
      sub-title="This is a subtitle"
      @back="() => $router.go(-1)"
    >
      <template slot="extra">
        <a-button key="2">Operation</a-button>
        <a-button key="1" type="primary">Primary</a-button>
      </template>
      <a-descriptions size="small" :column="3">
        <a-descriptions-item label="Created">james smith</a-descriptions-item>
        <a-descriptions-item label="Title">Writer</a-descriptions-item>
      </a-descriptions>
    </a-page-header>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We add the extra slot and populate it with buttons.

Also, we populate the default slot with the a-description component to add more content.

We can also set the background by setting the background color on the container and setting the ghost prop to false to show the background:

<template>
  <div style="background-color: #F5F5F5; padding: 24px;">
    <a-page-header
      :ghost="false"
      title="Title"
      sub-title="This is a subtitle"
      @back="() => $router.go(-1)"
    >
      <template slot="extra">
        <a-button key="2">Operation</a-button>
        <a-button key="1" type="primary">Primary</a-button>
      </template>
      <a-descriptions size="small" :column="3">
        <a-descriptions-item label="Created">james smith</a-descriptions-item>
        <a-descriptions-item label="Title">Writer</a-descriptions-item>
      </a-descriptions>
    </a-page-header>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Conclusion

We can set a theme for menus and add a page header to our Vue app with Ant Design Vue.

Categories
Ant Design Vue

Ant Design Vue — Layouts

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Header and Side Bar

We can add a header and sidebar together. For example, we can write:

<template>
  <div id="app">
    <a-layout id="components-layout-demo-top-side-2">
      <a-layout-header class="header">
        <div class="logo"/>
        <a-menu
          theme="dark"
          mode="horizontal"
          :default-selected-keys="['2']"
          :style="{ lineHeight: '64px' }"
        >
          <a-menu-item key="1">nav 1</a-menu-item>
          <a-menu-item key="2">nav 2</a-menu-item>
          <a-menu-item key="3">nav 3</a-menu-item>
        </a-menu>
      </a-layout-header>
      <a-layout>
        <a-layout-sider width="200" style="background: #fff">
          <a-menu
            mode="inline"
            :default-selected-keys="['1']"
            :default-open-keys="['sub1']"
            :style="{ height: '100%', borderRight: 0 }"
          >
            <a-sub-menu key="sub1">
              <span slot="title">
                <a-icon type="user"/>subnav 1
              </span>
              <a-menu-item key="1">option1</a-menu-item>
              <a-menu-item key="2">option2</a-menu-item>
            </a-sub-menu>
            <a-sub-menu key="sub2">
              <span slot="title">
                <a-icon type="laptop"/>subnav 2
              </span>
              <a-menu-item key="3">option3</a-menu-item>
              <a-menu-item key="4">option4</a-menu-item>
            </a-sub-menu>
            <a-sub-menu key="sub3">
              <span slot="title">
                <a-icon type="notification"/>subnav 3
              </span>
              <a-menu-item key="5">option5</a-menu-item>
              <a-menu-item key="6">option60</a-menu-item>
            </a-sub-menu>
          </a-menu>
        </a-layout-sider>
        <a-layout style="padding: 0 24px 24px">
          <a-breadcrumb style="margin: 16px 0">
            <a-breadcrumb-item>Home</a-breadcrumb-item>
            <a-breadcrumb-item>List</a-breadcrumb-item>
            <a-breadcrumb-item>App</a-breadcrumb-item>
          </a-breadcrumb>
          <a-layout-content
            :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
          >Content</a-layout-content>
        </a-layout>
      </a-layout>
    </a-layout>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      collapsed: false
    };
  }
};
</script>

We put the a-layout-header component on top.

Then the a-layout contains the a-layout-sider with the sidebar.

a-sub-menu has the submenus.

The bottom a-layout has the content.

Header and Content

We can also add a header and content without the sidebar:

<template>
  <div id="app">
    <a-layout id="components-layout-demo-top" class="layout">
      <a-layout-header>
        <div class="logo"/>
        <a-menu
          theme="dark"
          mode="horizontal"
          :default-selected-keys="['2']"
          :style="{ lineHeight: '64px' }"
        >
          <a-menu-item key="1">nav 1</a-menu-item>
          <a-menu-item key="2">nav 2</a-menu-item>
          <a-menu-item key="3">nav 3</a-menu-item>
        </a-menu>
      </a-layout-header>
      <a-layout-content style="padding: 0 50px">
        <a-breadcrumb style="margin: 16px 0">
          <a-breadcrumb-item>Home</a-breadcrumb-item>
          <a-breadcrumb-item>List</a-breadcrumb-item>
          <a-breadcrumb-item>App</a-breadcrumb-item>
        </a-breadcrumb>
        <div :style="{ background: '#fff', padding: '24px', minHeight: '280px' }">Content</div>
      </a-layout-content>
      <a-layout-footer style="text-align: center">Ant Design</a-layout-footer>
    </a-layout>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      collapsed: false
    };
  }
};
</script>

We just add the a-layout-content without the a-layout component.

Also, we can add the sidebar without the header:

<template>
  <div id="app">
    <a-layout id="components-layout-demo-responsive">
      <a-layout-sider
        breakpoint="lg"
        collapsed-width="0"
        [@collapse](http://twitter.com/collapse "Twitter profile for @collapse")="onCollapse"
        [@breakpoint](http://twitter.com/breakpoint "Twitter profile for @breakpoint")="onBreakpoint"
      >
        <div class="logo"/>
        <a-menu theme="dark" mode="inline" :default-selected-keys="['4']">
          <a-menu-item key="1">
            <a-icon type="user"/>
            <span class="nav-text">nav 1</span>
          </a-menu-item>
          <a-menu-item key="2">
            <a-icon type="video-camera"/>
            <span class="nav-text">nav 2</span>
          </a-menu-item>
          <a-menu-item key="3">
            <a-icon type="upload"/>
            <span class="nav-text">nav 3</span>
          </a-menu-item>
        </a-menu>
      </a-layout-sider>
      <a-layout>
        <a-layout-header :style="{ background: '#fff', padding: 0 }"/>
        <a-layout-content :style="{ margin: '24px 16px 0' }">
          <div :style="{ padding: '24px', background: '#fff', minHeight: '360px' }">content</div>
        </a-layout-content>
        <a-layout-footer style="textAlign: center">Ant Design</a-layout-footer>
      </a-layout>
    </a-layout>
  </div>
</template>

<script>
export default {
  name: "App",
  methods: {
    onCollapse(collapsed, type) {
      console.log(collapsed, type);
    },
    onBreakpoint(broken) {
      console.log(broken);
    }
  }
};
</script>

Conclusion

We can add various layouts with Ant Design Vue.

Categories
Ant Design Vue

Ant Design Vue — Grids and Layouts

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Grid Order

We can change the grid order by setting the order prop:

<template>
  <div id="app">
    <a-row type="flex">
      <a-col :span="6" :order="4">1 order-4</a-col>
      <a-col :span="6" :order="3">2 order-3</a-col>
      <a-col :span="6" :order="2">3 order-2</a-col>
      <a-col :span="6" :order="1">4 order-1</a-col>
    </a-row>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We set the order to a number.

Grid Column Offset

We can add the offset prop to add gaps between grid columns. For example, we can write:

<template>
  <div id="app">
    <a-row>
      <a-col :span="8">col-8</a-col>
      <a-col :span="8" :offset="8">col-8</a-col>
    </a-row>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Responsive Grid

We can set the sizes of the columns at different breakpoints.

For example, we can write:

<template>
  <div id="app">
    <a-row>
      <a-col :xs="2" :sm="4" :md="6" :lg="8" :xl="10">Col</a-col>
      <a-col :xs="20" :sm="16" :md="12" :lg="8" :xl="4">Col</a-col>
      <a-col :xs="2" :sm="4" :md="6" :lg="8" :xl="10">Col</a-col>
    </a-row>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

to set the column size for the xs , sm , md and lg breakpoints.

Push and Pull

We can also set the column order with the push and pull props:

<template>
  <div id="app">
    <a-row>
      <a-col :span="18" :push="6">col-18 col-push-6</a-col>
      <a-col :span="6" :pull="18">col-6 col-pull-18</a-col>
    </a-row>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Layout

Ant Design Vue comes with various components for creating layouts.

We can use the a-layout , a-layout-header , a-layout-content , and a-layout-footer components:

<template>
  <div id="app">
    <a-layout>
      <a-layout-header>Header</a-layout-header>
      <a-layout-content>Content</a-layout-content>
      <a-layout-footer>Footer</a-layout-footer>
    </a-layout>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

The header is black.

We can trigger content to be displayed when we click on a link on a menu:

<template>
  <div id="app">
    <a-layout id="components-layout-demo-custom-trigger">
      <a-layout-sider v-model="collapsed" :trigger="null" collapsible>
        <div class="logo"/>
        <a-menu theme="dark" mode="inline" :default-selected-keys="['1']">
          <a-menu-item key="1">
            <a-icon type="user"/>
            <span>nav 1</span>
          </a-menu-item>
          <a-menu-item key="2">
            <a-icon type="video-camera"/>
            <span>nav 2</span>
          </a-menu-item>
          <a-menu-item key="3">
            <a-icon type="upload"/>
            <span>nav 3</span>
          </a-menu-item>
        </a-menu>
      </a-layout-sider>
      <a-layout>
        <a-layout-header style="background: #fff; padding: 0">
          <a-icon
            class="trigger"
            :type="collapsed ? 'menu-unfold' : 'menu-fold'"
            @click="() => (collapsed = !collapsed)"
          />
        </a-layout-header>
        <a-layout-content
          :style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: '280px' }"
        >Content</a-layout-content>
      </a-layout>
    </a-layout>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      collapsed: false
    };
  }
};
</script>

We created a sidebar menu with the a-menu component. How it’s displaying us controlled by the collapsed component.

a-layout displays beside the menu.

Conclusion

We can add layouts and grids with Ant Design Vue.

Categories
Ant Design Vue

Ant Design Vue — Dropdowns and Top Menus

Ant Design Vue or AntD Vue, is a useful UI framework made for Vue.js.

In this article, we’ll look at how to use it in our Vue apps.

Dropdown

We can add dropdowns with the a-dropdown component.

For example, we can write:

<template>
  <div id="app">
    <a-dropdown>
      <a class="ant-dropdown-link" @click="e => e.preventDefault()">Hover me
        <a-icon type="down"/>
      </a>
      <a-menu slot="overlay">
        <a-menu-item>
          <a href="javascript:;">1st menu item</a>
        </a-menu-item>
        <a-menu-item>
          <a href="javascript:;">2nd menu item</a>
        </a-menu-item>
      </a-menu>
    </a-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We add the a-dropdown with the a element being its trigger.

a-menu shows the menu.

Context Menu

Also, we can add a context menu by setting the trigger prop to the ['contextmenu'] array:

<template>
  <div id="app">
    <a-dropdown :trigger="['contextmenu']">
      <div
        :style="{
        textAlign: 'center',
        background: '#f7f7f7',
        height: '200px',
        lineHeight: '200px',
        color: '#777',
      }"
      >Right Click on here</div>
      <a-menu slot="overlay">
        <a-menu-item key="1">1st menu item</a-menu-item>
        <a-menu-item key="2">2nd menu item</a-menu-item>
        <a-menu-item key="3">3rd menu item</a-menu-item>
      </a-menu>
    </a-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Also, we can add a divider and disable menu items:

<template>
  <div id="app">
    <a-dropdown>
      <a class="ant-dropdown-link" @click="e => e.preventDefault()">Hover me
        <a-icon type="down"/>
      </a>
      <a-menu slot="overlay">
        <a-menu-item key="0">
          <a target="_blank" rel="noopener noreferrer" href="https://www.google.com/">1st menu item</a>
        </a-menu-item>
        <a-menu-item key="1">
          <a target="_blank" rel="noopener noreferrer" href="https://www.ebay.com/">2nd menu item</a>
        </a-menu-item>
        <a-menu-divider/>
        <a-menu-item key="3" disabled>3rd menu item(disabled)</a-menu-item>
      </a-menu>
    </a-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

a-menu-divider adds the divider and the disabled prop disables the item.

The placement of the dropdown menu can be changed:

<template>
  <div id="app">
    <a-dropdown placement="bottomRight">
      <a-button>bottom right</a-button>
      <a-menu slot="overlay">
        <a-menu-item key="0">
          <a target="_blank" rel="noopener noreferrer" href="https://www.google.com/">1st menu item</a>
        </a-menu-item>
        <a-menu-item key="1">
          <a target="_blank" rel="noopener noreferrer" href="https://www.ebay.com/">2nd menu item</a>
        </a-menu-item>
      </a-menu>
    </a-dropdown>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

We set the placement prop to change the placement.

Menu

We can add a menu with the a-menu component as the container and a-menu-item for the menu items:

<template>
  <div id="app">
    <a-menu v-model="current" mode="horizontal">
      <a-menu-item key="mail">
        <a-icon type="mail"/>Navigation One
      </a-menu-item>
      <a-menu-item key="app" disabled>
        <a-icon type="appstore"/>Navigation Two
      </a-menu-item>
      <a-sub-menu>
        <span slot="title" class="submenu-title-wrapper">
          <a-icon type="setting"/>Navigation Three - Submenu
        </span>
        <a-menu-item-group title="Item 1">
          <a-menu-item key="setting:1">Option 1</a-menu-item>
          <a-menu-item key="setting:2">Option 2</a-menu-item>
        </a-menu-item-group>
        <a-menu-item-group title="Item 2">
          <a-menu-item key="setting:3">Option 3</a-menu-item>
          <a-menu-item key="setting:4">Option 4</a-menu-item>
        </a-menu-item-group>
      </a-sub-menu>
      <a-menu-item key="google">
        <a
          href="https://google.com"
          target="_blank"
          rel="noopener noreferrer"
        >Navigation Four - Link</a>
      </a-menu-item>
    </a-menu>
  </div>
</template>

<script>
export default {
  name: "App"
};
</script>

Conclusion

We can add a dropdown and top menu with Ant Design Vue.