本文主要介紹了中繼資料中序列号功能,用到了q-pagination分頁控件,并且封裝成自定義元件cpage, 然後實作了序列号的crud增删改查功能。
基于Vue和Quasar的前端SPA項目實戰之序列号(四)
回顧
通過上一篇文章 基于Vue和Quasar的前端SPA項目實戰之布局菜單(三)的介紹,我們已經完成了布局菜單,本文主要介紹序列号功能的實作。
簡介
MySQL資料庫沒有單獨的Sequence,隻支援自增長(increment)主鍵,但是不能設定步長、開始索引、格式等,最重要的是一張表隻能由一個字段使用自增,但有的時候我們需要多個字段實作序列号功能或者需要支援複雜格式,MySQL本身是實作不了的,是以封裝了複雜序列号,支援字元串和數字,自定義格式,也可以設定為時間戳。可以用于産品編碼、訂單流水号等場景!
UI界面
序列号清單
建立序列号
編輯序列号
API
序列号API包括基本的CRUD操作,通過axios封裝api,名稱為metadataSequence
import { axiosInstance } from "boot/axios";
const metadataSequence = {
create: function(data) {
return axiosInstance.post("/api/metadata/sequences",
data
);
},
update: function(id, data) {
return axiosInstance.patch("/api/metadata/sequences/" + id,
data
);
},
list: function(page, rowsPerPage, search, query) {
if (!page) {
page = 1
}
if (!rowsPerPage) {
rowsPerPage = 10
}
return axiosInstance.get("/api/metadata/sequences",
{
params: {
offset: (page - 1) * rowsPerPage,
limit: rowsPerPage,
search: search,
...query
}
}
);
},
count: function(search, query) {
return axiosInstance.get("/api/metadata/sequences/count",
{
params: {
search: search,
...query
}
}
);
},
get: function(id) {
return axiosInstance.get("/api/metadata/sequences/" + id,
{
params: {
}
}
);
},
delete: function(id) {
return axiosInstance.delete("/api/metadata/sequences/" + id);
},
batchDelete: function(ids) {
return axiosInstance.delete("/api/metadata/sequences",
{data: ids}
);
}
};
export { metadataSequence };
增删改查
通過清單頁面,建立頁面和編輯頁面實作了序列号基本的crud操作,其中建立和編輯頁面類似,普通的表單送出,這裡就不詳細介介紹了,直接檢視代碼即可。對于清單查詢頁面,用到了自定義元件,這裡重點介紹了一下自定義元件相關知識。
自定義component
序列号清單頁面中用到了分頁控件,因為其它清單頁面也會用到,是以适合封裝成component, 名稱為CPage。主要功能包括:設定每頁的條目個數,切換分頁,統一樣式等。
核心代碼
首先在components目錄下建立檔案夾CPage,然後建立CPage.vue和index.js檔案。
CPage/CPage.vue
用到了q-pagination控件
<q-pagination
unelevated
v-model="pagination.page"
:max="Math.ceil(pagination.count / pagination.rowsPerPage)"
:max-pages="10"
:direction-links="true"
:ellipses="false"
:boundary-numbers="true"
:boundary-links="true"
@input="onRequestAction"
>
</q-pagination>
CPage/index.js
實作install方法
import CPage from "./CPage.vue";
const cPage = {
install: function(Vue) {
Vue.component("CPage", CPage);
}
};
export default cPage;
CPage使用
全局配置
首先,建立boot/cpage.js檔案
import cPage from "../components/CPage";
export default async ({ Vue }) => {
Vue.use(cPage);
};
然後,在quasar.conf.js裡面boot節點添加cpage,這樣Quasar就會自動加載cpage。
boot: [
'i18n',
'axios',
'cpage'
]
應用
在序列号清單中通過标簽CPage使用
<CPage v-model="pagination" @input="onRequestAction"></CPage>
當切換分頁的時候,通過@input回調,傳遞目前頁數和每頁個數給父頁面,然後通過API擷取對應的序列号清單。
小結
本文主要介紹了中繼資料中序列号功能,用到了q-pagination分頁控件,并且封裝成自定義元件cpage, 然後實作了序列号的crud增删改查功能,下一章會介紹中繼資料中表定義功能。