经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
Vue.js 可排序表格 (Sortable & Searchable Table) 组件
来源:cnblogs  作者:yukiwu  时间:2021/3/8 11:51:10  对本文有异议

可排序表格 (Sortable & Searchable Tables) 在网页和表单设计中非常常用。用户可以通过点击表头对将表格以该列做顺序或降序排列,也可以利用 Search Box 对表格内容进行筛选。这个组件曾被运用于 X-Ray Diffraction Analysis App 和 Extract Graph Data App 等等。

preview gif

注册组件


注册 Sortable & Searchable Tables 组件和之前介绍注册其他组件的方法类似, 其实就是复制粘贴已封装好的代码到父级实例中。

  1. <script type="text/x-template" id="sortable-table-template">
  2. ...
  3. </script>
  4. <script>
  5. Vue.component('sortable_table', {
  6. template: '#sortable-table-template’,
  7. });
  8. </script>

调用组件


直接添加自定义标签 <sortable_table></sortable_table> 调用组件。

  1. <sortable_table class="row"
  2. :rows="table_data"
  3. :columns="table_header"
  4. :filter-key="searchQuery"
  5. :selected="selected_rows"
  6. @update-table-data="onUpdateTableData"
  7. @update-table-selected="onUpdateTableSelected">
  8. </sortable_table>

传递数据


利用 v-bind 动态绑定数据,其中searhQuery 为 search box 的默认内容,table_header 为表格的表头,table_data 为表格的数据, select_rows 为勾选的行号。另外 "onUpdateTableData:function" 和 "onUpdateTableSelected" 用于动态刷新表格的内容。

  1. data: function(){
  2. return {
  3. searchQuery: '',
  4. table_header: ['name', 'age', 'height', 'weight', 'color'],
  5. table_data: [
  6. {id: 1, name: 'Alice', age: 12, height: 155, weight: 45, color: '#ffffff'},
  7. {id: 2, name: 'Ben', age: 13, height: 170, weight: 60, color: '#cccccc'},
  8. {id: 3, name: 'Charlie', age: 17, height: 178, weight: 65, color: '#999999'},
  9. {id: 4, name: 'Daniel', age: 14, height: 168, weight: 58, color: '#666666'},
  10. {id: 5, name: 'Ethan', age: 11, height: 150, weight: 50, color: '#333333'},
  11. ],
  12. selected_rows: [],
  13. }
  14. },
  15. ...
  16. methods:{
  17. onUpdateTableData:function(new_table_data) {
  18. this.table_data = new_table_data;
  19. },
  20. onUpdateTableSelected:function(new_table_selected){
  21. this.table_selected = new_table_selected;
  22. },
  23. },

源代码

Github

原文链接:http://www.cnblogs.com/yukiwu/p/14474531.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号