Home Web Front-end JS Tutorial Detailed explanation of examples of vue.js table component development

Detailed explanation of examples of vue.js table component development

Jan 16, 2017 pm 12:53 PM

前言

组件(Component)是 Vue.js 最强大的功能之一。组件可以扩展 HTML 元素,封装可重用的代码。在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能。在有些情况下,组件也可以是原生 HTML 元素的形式,以 is 特性扩展。

组件开发的基础

组件可以扩展 HTML 元素,封装可重用的代码。我理解为功能模块的模板吧。

对于vue来说,组件是这个样子的,我们在html里面写

<div id="example">
 <my-component></my-component>
</div>edx
Copy after login

然后就出来

<div id="example">
<div>A custom component!</div>
</div>
Copy after login

代码

A custom component!
我们只要写一遍就行了 。

所以我们需要定义它,把 my-component的标签和代码关联起来,所以我们要定义它

// 定义
var MyComponent = Vue.extend({
 template: &#39;<div>A custom component!</div>&#39;
})
Copy after login

定义了之后,我们要让页面能够渲染它,让Vue知道它的存在

// 注册
 Vue.component(&#39;my-component&#39;, MyComponent)
// 创建根实例
new Vue({
 el: &#39;#example&#39;
})
Copy after login

以上,是官网一个非常简单的例子 ,其实觉得和一个function的封装也差不多,定义,引入,然后执行。

然后一个组件可以引用别的组件的东西,有点像函数的互相调用啊。

var Child = Vue.extend({ /* ... */ })
var Parent = Vue.extend({
 template: &#39;...&#39;,
 components: {
 // <my-component> 只能用在父组件模板内
 &#39;my-component&#39;: Child
 }
})
Copy after login

一个表格组件的实例

这是官网的例子

Detailed explanation of examples of vue.js table component development

这个是一个可以排序的表格的例子。我们从头开始来制作一个可以排序的表格。

基本结构

首先分成两个部分,一个是搜索框,一个是表格本身,我们可以得到这样的结构

<div id="demo">
 <form id="search">
 Search <input name="query">
 </form>
<table>
 <thead>
 <tr>
 <th>name</th>
 <th>power</th>
 </tr>
 </thead>
 <tbody>
 <tr>
 <td>Jack Chan</td>
 <td>7000</td>
 </tr>
 </tbody>
</table>
</div>
Copy after login

显示效果

Detailed explanation of examples of vue.js table component development

加上基本的css

body {
 font-family: Helvetica Neue, Arial, sans-serif;
 font-size: 14px;
 color: #444;
}
 
table {
 border: 2px solid #42b983;
 border-radius: 3px;
 background-color: #fff;
}
 
th {
 background-color: #42b983;
 color: rgba(255,255,255,0.66);
 cursor: pointer;
 -webkit-user-select: none;
 -moz-user-select: none;
 -user-select: none;
}
 
td {
 background-color: #f9f9f9;
}
 
th, td {
 min-width: 120px;
 padding: 10px 20px;
}
 
#search {
 margin-bottom: 10px;
}
Copy after login

显示效果如下,

Detailed explanation of examples of vue.js table component development

提取组件

我们是想要让表格成为单独的组件,所以我们定义一个叫做 demo-grid的组件,用它来生成表格

<div id="demo">
 <form id="search">
 Search <input name="query" >
 </form>
 <demo-grid>
 </demo-grid>
</div>
Copy after login

代码里面关于表格的那部分给放到组件模板里面,我们定义组件。也就是用script来定义,

<script type="text/x-template" id="grid-template">
 <table>
 <thead>
 <tr>
  <th>name</th>
  <th>power</th>
 </tr>
 </thead>
 <tbody>
 <tr>
  <td>Jack Chan</td>
  <td>7000</td>
 </tr>
 </tbody>
 </table>
</script>
Copy after login

定义完了之后我们要在给Vue注册组件模块,然后进行Vue的渲染

Vue.component(&#39;demo-grid&#39;,{
template:"#grid-template",
});
 
var demo = new Vue({
el:&#39;#demo&#39;
})
Copy after login

然后最后的效果是一样的,这个时候并没有数据流。

组件数据流

我们要让表格知道表格的头部和表格的内容,也就是有一个gridColumns和gridData,这个数据最开始放在Vue的Data里面

// bootstrap the demo
var demo = new Vue({
el: &#39;#demo&#39;,
data: {
 gridColumns: [&#39;name&#39;, &#39;power&#39;],
 gridData: [
 { name: &#39;Chuck Norris&#39;, power: Infinity },
 { name: &#39;Bruce Lee&#39;, power: 9000 },
 { name: &#39;Jackie Chan&#39;, power: 7000 },
 { name: &#39;Jet Li&#39;, power: 8000 }
 ]
}
})
Copy after login

然后我们的组件也要接受这个数据,这里我们通过类似属性的形式给组件模板传入数据,

<demo-grid
  :data="gridData"
  :columns="gridColumns">
 </demo-grid>
Copy after login

然后我们在组件里面把相应的数据继承下来。

Vue.component(&#39;demo-grid&#39;,{
template:"#grid-template",
props: {
 data: Array,
 columns: Array
}
});
Copy after login

然后在模板里面替换掉

<script type="text/x-template" id="grid-template">
 <table>
 <thead>
 <tr>
  <th v-for="key in columns">{{key}}</th>
 </tr>
 </thead>
 <tbody>
 <tr v-for="entry in data">
  <td v-for="key in columns">{{entry[key]}}</td>
 </tr>
 </tbody>
 </table>
</script>
Copy after login

效果如下

Detailed explanation of examples of vue.js table component development

搜索功能增加

这个时候,我们想加入一个交互,也就是在搜索框增加关键词的时候,表格能够相应地变化。

首先我们要绑定搜索框的模型,也就是用searchQuery来绑定Input

<form id="search">
Search <input name="query" v-model="searchQuery">
</form>
Copy after login

在Vue里面增加data的初始化

var demo = new Vue({
el: &#39;#demo&#39;,
data: {
 searchQuery: &#39;&#39;,
 ...
})
Copy after login

同时,在组件模板里面也进行参数传入

<demo-grid
 :data="gridData"
 :columns="gridColumns"
 :filter-key="searchQuery">
</demo-grid>
Copy after login

组件的定义里面要继承模板的数据,也就是在模板里面是filter-key,注意要转化驼峰写法

Vue.component(&#39;demo-grid&#39;, {
 template: &#39;#grid-template&#39;,
 props: {
  data: Array,
  columns: Array,
  filterKey: String
 }
})
Copy after login


这个时候,我们的模板里面要过滤符合filterKey的数据,这里就用到了过滤器,vue提供了一个叫做filterBy的过滤器。|与过滤器,第一个为过滤器的名字,后面的是参数,| filterBy filterKey使用的就是filterBy的过滤器,参数是filterKey

<tr v-for="
 entry in data
 | filterBy filterKey>
  <td v-for="key in columns">
  {{entry[key]}}
  </td>
 </tr>
Copy after login

效果如下,我们这样就有了一个 能够过滤的表格

Detailed explanation of examples of vue.js table component development

表格排序

这部分逻辑比前面稍微复杂一点点。首先我们给表格加一个三角形,三角形有两种,一种是正序的,一种是逆序的,我们用span来作为三角形的容器

<th v-for="key in columns"
@click="sortBy(key)"
:class="{active: sortKey == key}">
{{key | capitalize}}
<span class="arrow">
</span>
</th>
Copy after login

三角形的样式有两种,上升的和下降的

.arrow {
 display: inline-block;
 vertical-align: middle;
 width: 0;
 height: 0;
 margin-left: 5px;
 opacity: 0.66;
}
 
.arrow.asc {
 border-left: 4px solid transparent;
 border-right: 4px solid transparent;
 border-bottom: 4px solid #fff;
}
 
.arrow.dsc {
 border-left: 4px solid transparent;
 border-right: 4px solid transparent;
 border-top: 4px solid #fff;
}
Copy after login

dsc的效果如下

Detailed explanation of examples of vue.js table component development

我们希望能够在不同的状态里面切换,所以我们选择在组件里面有数据存储排序的状态信息,用一个sortOrders的对象来存储排序信息 ,同时用一个sortKey来表示当前用来排序的键,我们设置为name

// register the grid component
Vue.component(&#39;demo-grid&#39;, {
template: &#39;#grid-template&#39;,
...
data: function () {
 var sortOrders = {}
 this.columns.forEach(function (key) {
 sortOrders[key] = 1
 })
 return {
 sortKey: &#39;name&#39;,
 sortOrders: sortOrders
 }
}
Copy after login

然后使用orderBy来排序

<tbody>
<tr v-for="
entry in data
| filterBy filterKey
| orderBy sortKey sortOrders[sortKey]">
 <td v-for="key in columns">
 {{entry[key]}}
 </td>
</tr>
</tbody>
Copy after login

Detailed explanation of examples of vue.js table component development

增加交互

我们希望能够通过点击表格头部来自动升序降序,所以添加鼠标事件,另外把span的样式和sortOrders[key]的值相关联,增加active的样式

<th v-for="key in columns"
  @click="sortBy(key)"
  :class="{active: sortKey == key}">
  {{key | capitalize}}
 <span class="arrow"
  :class="sortOrders[key] > 0 ? &#39;asc&#39; : &#39;dsc&#39;">
 </span>
 </th>
Copy after login

相关active的样式如下

th.active .arrow {
 opacity: 1;
}
Copy after login

对于鼠标事件,我们定义在表格内部,也就是把sortKey定位当前活跃的元素,同时改变sortOrders[key]的值

// register the grid component
Vue.component(&#39;demo-grid&#39;, {
template: &#39;#grid-template&#39;,
props: {
 ...
},
data: function () {
...
},
methods: {
 sortBy: function (key) {
 this.sortKey = key
 this.sortOrders[key] = this.sortOrders[key] * -1
 }
}
});
Copy after login

总结

以上就是关于vue.js组件开发的全部内容了,希望这篇文章对大家学习或者使用vue.js能有一定的帮助,如果有疑问大家可以留言交流。

更多Detailed explanation of examples of vue.js table component development相关文章请关注PHP中文网!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles