UNPKG

@zeit-ui/vue

Version:

A popular UI framework for Vue

2 lines 9.11 kB
(function(t,e){"object"===typeof exports&&"object"===typeof module?module.exports=e(require("vue")):"function"===typeof define&&define.amd?define([],e):"object"===typeof exports?exports["table"]=e(require("vue")):t["table"]=e(t["Vue"])})("undefined"!==typeof self?self:this,(function(t){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"===typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t["default"]}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s="fb15")}({"8bbf":function(e,n){e.exports=t},a731:function(t,e,n){},f6fd:function(t,e){(function(t){var e="currentScript",n=t.getElementsByTagName("script");e in t||Object.defineProperty(t,e,{get:function(){try{throw new Error}catch(r){var t,e=(/.*at [^\(]*\((.*):.+:.+\)$/gi.exec(r.stack)||[!1])[1];for(t in n)if(n[t].src==e||"interactive"==n[t].readyState)return n[t];return null}}})})(document)},fb15:function(t,e,n){"use strict";var r;(n.r(e),"undefined"!==typeof window)&&(n("f6fd"),(r=window.document.currentScript)&&(r=r.src.match(/(.+\/)[^/]+\.js(\?.*)?$/))&&(n.p=r[1]));var i=function(){var t=this,e=t.$createElement,n=t._self._c||e;return n("div",{staticClass:"zi-table",style:{width:t.bodyWidth}},[n("div",{ref:"hiddenColumns",staticClass:"hidden"},[t._t("default")],2),n("div",{ref:"headerWrapper",staticClass:"header-wrapper"},[n("table-header",{ref:"tableHeader",staticClass:"header-table",style:{width:t.layout.bodyWidth+"px"},attrs:{store:t.store}})],1),n("div",{ref:"bodyWrapper",staticClass:"body-wrapper"},[n("table-body",{ref:"tableBody",staticClass:"body-table",style:{width:t.layout.bodyWidth+"px"},attrs:{store:t.store,"empty-text":t.emptyText,isHover:t.hover}})],1)])},o=[],a=(n("a731"),function(t){if(!t)throw new Error("table is required");this.table=t,this.states={columns:[],data:null,width:null,currentRow:null}});a.prototype.mutations={setData:function(t,e){t.data=e},insertColumns:function(t,e,n){var r=t.columns;"undefined"===typeof n?r.push(e):r.splice(n,0,e)},setCurrentRow:function(t,e){var n=t.currentRow;t.currentRow=e,n!==e&&this.table.$emit("current-change",e,n)}},a.prototype.commit=function(t){var e=this.mutations;if(!e[t])throw new Error("action not found: ".concat(t));for(var n=arguments.length,r=new Array(n>1?n-1:0),i=1;i<n;i++)r[i-1]=arguments[i];e[t].apply(this,[this.states].concat(r))};var u=a,s={name:"ElTableHeader",props:{store:{required:!0}},render:function(){var t=arguments[0];return t("table",{class:"zi-table"},[t("colgroup",[this._l(this.columns,(function(e){return t("col",{attrs:{width:e.realWidth||e.width}})}))]),t("thead",[t("tr",[this._l(this.columns,(function(e){return t("th",[e.label||"#"])}))])])])},computed:{columns:function(){return this.store.states.columns},table:function(){return this.$parent}}},l={name:"zi-table-body",props:{store:{required:!0},isHover:[Boolean],emptyText:[String,Boolean]},render:function(t){var e=this;return t("table",{class:"zi-table"},[t("colgroup",[this._l(this.columns,(function(e){return t("col",{attrs:{width:e.realWidth||e.width}})}))]),t("tbody",[this.data.length?this._l(this.data,(function(n,r){return[t("tr",{class:e.isHover?"table-row":"",on:{click:function(t){return e.handleClick(t,n)}}},[e._l(e.columns,(function(i){return t("td",[i.renderCell.call(e._renderProxy,t,{row:n,column:i,$index:r})])}))])]})):t("tr",[t("td",{style:"color:#ccc; text-align:center"},[this.emptyText||"没有数据"])])])])},computed:{table:function(){return this.$parent},data:function(){return this.store.states.data},columns:function(){return this.store.states.columns}},methods:{getCell:function(t){var e=t.target;while(e&&"HTML"!==e.tagName.toUpperCase()){if("TD"===e.tagName.toUpperCase())return e;e=e.parentNode}return null},handleClick:function(t,e){var n=this.table,r=this.getCell(t);r&&n.$emit("cell-click",r,e,t),this.store.commit("setCurrentRow",e),n.$emit("row-click",e,t)}}},c=n("8bbf"),d=n.n(c);function f(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function h(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function p(t,e,n){return e&&h(t.prototype,e),n&&h(t,n),t}var m=function(){function t(e){var n=this;if(f(this,t),this.table=null,this.store=null,this.columns=null,this.bodyWidth=null,Object.keys(e).forEach((function(t){Object.prototype.hasOwnProperty.call(e,t)&&(n[t]=e[t])})),!this.table)throw new Error("table is required for Table Layout");if(!this.store)throw new Error("store is required for Table Layout")}return p(t,[{key:"updateColumnWidth",value:function(){if(!d.a.prototype.$isServer){var t=this.table.$el.clientWidth,e=this.table.columns,n=e.filter((function(t){return"number"!==typeof t.width})),r=e.filter((function(t){return"number"===typeof t.width})),i=r.reduce((function(t,e){return t+e.width}),0);if(n.length>0){var o=n.reduce((function(t,e){return t+e.minWidth}),i);if(i>t)return void(this.bodyWidth=i+n.reduce((function(t,e){return t+e.minWidth}),0));var a=t-o,u=Math.floor(a/n.length);return n.forEach((function(t,e){t.realWidth=e?u+t.minWidth:a-u*(n.length-1)+t.minWidth})),void(this.bodyWidth=Math.max(t))}this.bodyWidth=i}}}]),t}(),b=m,y={name:"zi-table",components:{TableHeader:s,TableBody:l},props:{data:{type:Array,default:function(){return[]}},width:[String,Number],emptyText:[String,Boolean],hover:[Boolean]},computed:{columns:function(){return this.store.states.columns},bodyWidth:function(){return this.width?Number.isNaN(Number(this.width))?this.width:this.width+"px":"100%"}},watch:{data:{immediate:!0,handler:function(t){this.store.commit("setData",t)}}},data:function(){var t=new u(this),e=new b({store:t,table:this});return{layout:e,store:t,resizeState:{width:null}}},methods:{doLayout:function(){this.layout.updateColumnWidth()},bindEvent:function(){var t=this.$refs,e=t.bodyWrapper,n=t.headerWrapper;e.addEventListener("scroll",(function(){n.scrollLeft=this.scrollLeft}))}},mounted:function(){this.resizeState={width:this.$el.offsetWidth},this.doLayout(),this.bindEvent()}},v=y;function w(t,e,n,r,i,o,a,u){var s,l="function"===typeof t?t.options:t;if(e&&(l.render=e,l.staticRenderFns=n,l._compiled=!0),r&&(l.functional=!0),o&&(l._scopeId="data-v-"+o),a?(s=function(t){t=t||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,t||"undefined"===typeof __VUE_SSR_CONTEXT__||(t=__VUE_SSR_CONTEXT__),i&&i.call(this,t),t&&t._registeredComponents&&t._registeredComponents.add(a)},l._ssrRegister=s):i&&(s=u?function(){i.call(this,this.$root.$options.shadowRoot)}:i),s)if(l.functional){l._injectStyles=s;var c=l.render;l.render=function(t,e){return s.call(e),c(t,e)}}else{var d=l.beforeCreate;l.beforeCreate=d?[].concat(d,s):[s]}return{exports:t,options:l}}var g=w(v,i,o,!1,null,null,null),C=g.exports,_=function(t){var e=Object.keys(t).reduce((function(e,n){var r=Object.prototype.hasOwnProperty.call(t,n);return r&&"undefined"!==typeof t[n]&&(e[n]=t[n]),e}),{});return e.minWidth||(e.minWidth=80),e.realWidth=void 0===e.width?e.minWidth:e.width,e},W=function(t,e){if(void 0===t)return t;var n=~~t;return Number.isNaN(n)&&(n=e?80:null),n},x={name:"zi-table-column",props:{prop:String,label:String,width:[String,Number],minWidth:[String,Number]},data:function(){return{defaultConfig:{}}},watch:{label:function(t){this.defaultConfig&&(this.defaultConfig.label=t)},prop:function(t){this.defaultConfig&&(this.defaultConfig.prop=t)},width:function(t){this.defaultConfig&&(this.defaultConfig.width=W(t,!1))},minWidth:function(t){this.defaultConfig&&(this.defaultConfig.width=W(t,!0))}},render:function(){var t=arguments[0];return t("div")},computed:{isOwner:function(){var t=this.$parent;while(t&&"zi-table"!==t.$options.name)t=t.$parent;return t}},created:function(){var t=this,e=(this.$createElement,W(this.width,!1)),n=W(this.minWidth,!0),r=_({label:this.label,prop:this.prop,width:e,minWidth:n}),i=function(t,e){return t("div",{class:"table-cell"},[e.row[e.column.prop]])};r.renderCell=function(e,n){return t.$scopedSlots.default&&(i=function(){return t.$scopedSlots.default(n)}),i(e,n,t._renderProxy)},this.defaultConfig=r},mounted:function(){var t=this.isOwner.store;t.commit("insertColumns",this.defaultConfig)}};C.install=function(t){t.component(C.name,C),t.component(x.name,x)};var S=C;e["default"]=S}})["default"]})); //# sourceMappingURL=table.umd.min.js.map