一套基於 Vue.js 的高質量UI 組件庫,此篇介紹Modal(對話框)
<button class="btn btn-warning" @click="isShowModal1 = true">Show Modal</button>
<modal v-model="isShowModal1" title="Standard"
@on-ok="ok" @on-cancel="cancel"
ok-text="Ok" cancel-text="Cancel">
<p>Hello, this is iView Modal!</p>
</modal>
v-model
綁定"是否顯示對話框"(Boolean value)on-ok
和on-cancel
分別指定按下確認和取消的事件ok-text
和cancel-text
分別指定確認和取消按鈕顯示的文字(預設是"确定"和"取消")new Vue({
el: "#app",
data: {
isShowModal1: false,
},
methods: {
ok() {
console.info("Confirmed!");
},
cancel() {
console.info("Cancelled!");
}
}
})
<modal v-model="isShowModal2">
<p slot="header" style="background-color:lightyellow;text-align:center">
<icon type="ios-information-circle"></icon>
<span>Custom Modal</span>
</p>
<div style="text-align:center">
<h3>Hello, this is iView Modal!</h3>
</div>
<div slot="footer">
<button class="btn btn-default" @click="isShowModal2=false">Close</button>
</div>
</modal>
適用於點選確認按鈕後座非同步傳輸的情景;
<modal v-model="isShowModal3" loading title="Ok with loading" @on-ok="asyncOk" @on-cancel="cancel">
<p>Hello, this is iView Modal!</p>
</modal>
asyncOk() {
setTimeout(() => {
this.isShowModal3 = false;
}, 2500);
},
closable
: 是否啟用關閉按鈕mask-closable
: 是否啟用點選遮罩關閉按鈕<modal v-model="isShowModal4" title="Disable Closing" :closable="false" :mask-closable="false">
<p>Hello, this is iView Modal!</p>
</modal>
<modal v-model="isShowModal5" title="Draggable" draggable>
<p>Hello, this is iView Modal!</p>
</modal>
iView預設定義了以下幾組API來開啟對應的對話框
Modal Type | API |
---|---|
一般訊息 | this.$Modal.info({ options...}) |
成功訊息 | this.$Modal.success({ options...})` |
警示訊息 | this.$Modal.warning({ options...}) |
錯誤訊息 | this.$Modal.error({ options...}) |
包含確認/取消按鈕的訊息 | this.$Modal.confirm({ options...}) |
<button class="btn" @click="openModal('info')">Info</button>
<button class="btn" @click="openModal('success')">Success</button>
<button class="btn" @click="openModal('warning')">Warning</button>
<button class="btn" @click="openModal('error')">Error</button>
<button class="btn" @click="openModal('confirm')">Confirm</button>
var app = new Vue({
el: "#app",
methods: {
openModal(type) {
let msg = "Hello, this is iView Modal!";
switch (type) {
case "info":
this.$Modal.info({
title: "Info",
content: msg
});
break;
case "success":
this.$Modal.success({
title: "Success",
content: msg
});
break;
case "warning":
this.$Modal.warning({
title: "Warning",
content: msg
});
break;
case "error":
this.$Modal.error({
title: "Error",
content: msg
});
break;
case "confirm":
this.$Modal.confirm({
title: 'Confirm',
content: msg,
onOk: () => { console.log("Confirmed")},
onCancel: () => { console.log("Cancelled")}
});
break;
}
}
}
})
傳入的參數設定(Options)可參考此表;
另可透過this.$Modal.remove()
關閉對話框。
也可以透過在Modal Instance回傳Virtual DOM的方式客製對話框;
<button class="btn btn-success" @click="customContent()">Custom content</button>
var app = new Vue({
el: "#app",
data: {
value: ""
},
methods: {
customContent(){
this.$Modal.confirm({
render: (h) => {
return h('input', {
props: {
placeholder: 'Who is this?'
},
on: {
blur: (event) => {
this.value = event.target.value;
}
}
})
},
onOk: () => { alert(`Hello, ${this.value}`)}
})
}
}
})
以上範例請參考Sample code。
更多API請參考官方文件的
--
另外小弟自己寫的一個簡單的pop-up modal,也在這邊獻醜一下。
支援兩種顯示方式
- 依據內容決定的非固定長度
- 當內容超過時顯示卷軸的固定長度
Prop | 描述 | 型態 | 是否必要 | 預設值 |
---|---|---|---|---|
isFix | "true":固定長度,"false":非固定長度 | String | "false" | |
disableOk | 是否取消顯示OK按鈕 | Boolean | false |
事件 | 描述 |
---|---|
close | 當使用者點選OK按鈕時將觸發的事件,通常用於儲存使用者輸入於Modal中的內容 |
cancel | 當使用者點選非作用區域(遮罩區域)將觸發的事件,通常用於直接關閉該Modal |
<pop-modal v-if="showModal" v-on:close="closeModal" v-on:cancel="showModal=false">
<p slot="header">
{{ myHeader }}
</p>
<div slot="body">
{{ myContent }
</div>
<p slot="header">
{{ myFooter }}
</p>
</pop-modal>
<pop-modal v-if="showModal" is-fix="true" v-on:close="closeModal" v-on:cancel="showModal=false">
<p slot="header">
{{ myHeader }}
</p>
<div slot="body">
{{ myContent }
</div>
<p slot="header">
{{ myFooter }}
</p>
</pop-modal>