create.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <div>
  3. <vxe-input v-model="searchV" placeholder="模糊搜索名称" type="search" @search-click="search"></vxe-input>
  4. <vxe-input v-model="searchDate" placeholder="日期类型" type="date"></vxe-input>
  5. <vxe-input v-model="searchGte" placeholder="日期类型" type="date"></vxe-input>
  6. <vxe-input v-model="searchLte" placeholder="日期类型" type="date"></vxe-input>
  7. <vxe-button type="text" status="primary" @click="qjSera">区间搜索</vxe-button>
  8. <vxe-toolbar perfect>
  9. <template v-slot:buttons>
  10. <vxe-button icon="el-icon-delete" status="perfect" @click="deleteData">删除</vxe-button>
  11. <vxe-button icon="el-icon-position" status="perfect" @click="updateData">编辑</vxe-button>
  12. <vxe-button icon="el-icon-plus" status="perfect" @click="addData">新增</vxe-button>
  13. </template>
  14. </vxe-toolbar>
  15. <vxe-table
  16. border
  17. show-overflow
  18. row-id="id"
  19. size="medium"
  20. :data="tableData"
  21. @radio-change="radioChangeEvent"
  22. >
  23. <vxe-table-column type="radio" width="60" />
  24. <vxe-table-column type="seq" field="序号" width="60" />
  25. <vxe-table-column field="name" title="书名" />
  26. <vxe-table-column field="ISBN" title="编码" />
  27. <vxe-table-column field="type" title="类型"></vxe-table-column>
  28. <vxe-table-column field="author" title="作者">
  29. <template v-slot="{row}">
  30. <el-tag v-for="author in row.author" :key="author.id">{{author.author__name}}</el-tag>
  31. </template>
  32. </vxe-table-column>
  33. <vxe-table-column field="price" title="价格" />
  34. <vxe-table-column field="release_time" title="出版日期" />
  35. </vxe-table>
  36. <vxe-modal
  37. v-model="modelVisi"
  38. width="600"
  39. :title="modelTitle"
  40. show-footer
  41. @close="close"
  42. resize
  43. @confirm="submitEvent"
  44. >
  45. <vxe-form ref="xForm" title-align="right" title-width="100" :data="formData">
  46. <vxe-form-item title="书名" field="name" span="24">
  47. <template v-slot="scope">
  48. <vxe-input
  49. v-model="formData.name"
  50. placeholder="请输入书名"
  51. clearable
  52. @input="$refs.xForm.updateStatus(scope)"
  53. ></vxe-input>
  54. </template>
  55. </vxe-form-item>
  56. <vxe-form-item title="ISBN" field="ISBN" span="24">
  57. <template v-slot="scope">
  58. <vxe-input
  59. v-model="formData.ISBN"
  60. placeholder="请输入ISBN"
  61. clearable
  62. @input="$refs.xForm.updateStatus(scope)"
  63. ></vxe-input>
  64. </template>
  65. </vxe-form-item>
  66. <vxe-form-item title="类型" field="type" span="24">
  67. <template v-slot>
  68. <vxe-select v-model="formData.type" clearable>
  69. <vxe-option v-for="t in types" :key="t.id" :value="t.id" :label="t.name"></vxe-option>
  70. </vxe-select>
  71. </template>
  72. </vxe-form-item>
  73. <vxe-form-item title="作者" field="type" span="24">
  74. <template v-slot>
  75. <vxe-select v-model="formData.author" multiple clearable>
  76. <vxe-option
  77. v-for="user in authors"
  78. :key="user.id"
  79. :value="user.id"
  80. :label="user.name"
  81. ></vxe-option>
  82. </vxe-select>
  83. </template>
  84. </vxe-form-item>
  85. <vxe-form-item title="价格" field="type" span="24">
  86. <template v-slot>
  87. <vxe-input v-model="formData.price" placeholder="请输入价格" type="number"></vxe-input>
  88. </template>
  89. </vxe-form-item>
  90. <vxe-form-item title="出版日期" field="type" span="24">
  91. <template v-slot>
  92. <vxe-input v-model="formData.release_time" placeholder="日期类型" type="date" />
  93. </template>
  94. </vxe-form-item>
  95. </vxe-form>
  96. </vxe-modal>
  97. <!-- <vxe-pager
  98. border
  99. size="medium"
  100. :loading="loading2"
  101. :current-page="tablePage2.currentPage"
  102. :page-size="tablePage2.pageSize"
  103. :total="tablePage2.totalResult"
  104. :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
  105. @page-change="handlePageChange2"
  106. ></vxe-pager>-->
  107. </div>
  108. </template>
  109. <script>
  110. import {
  111. getBook,
  112. postBook,
  113. getAuthor,
  114. deleteBook,
  115. putBook,
  116. getType,
  117. } from "@/api/book";
  118. export default {
  119. name: "book-c",
  120. data() {
  121. return {
  122. tableData: [],
  123. radioId: null,
  124. modelVisi: false,
  125. modelTitle: "",
  126. formData: {
  127. ISBN: "",
  128. author: [],
  129. name: "",
  130. price: 0,
  131. release_time: "",
  132. type: [],
  133. },
  134. authors: [],
  135. types: [],
  136. searchV: "",
  137. searchDate: "",
  138. searchGte: "",
  139. searchLte: "",
  140. };
  141. },
  142. components: {},
  143. created() {
  144. this.init();
  145. },
  146. methods: {
  147. init() {
  148. this.getBookDate({});
  149. getAuthor().then((res) => {
  150. console.log("获取作者信息");
  151. console.log(res.data);
  152. if (res.data.length > 0) {
  153. console.log("getAuthor");
  154. console.log(res.data);
  155. this.authors = res.data;
  156. }
  157. });
  158. getType().then((res) => {
  159. this.types = res.data;
  160. });
  161. },
  162. getBookDate(par) {
  163. getBook(par).then((res) => {
  164. console.log(res.data);
  165. if (res.data.length > 0) {
  166. console.log("getBook");
  167. console.log(res.data);
  168. this.tableData = res.data;
  169. }
  170. });
  171. },
  172. radioChangeEvent(row) {
  173. console.log("单击事件");
  174. console.log(row.rowid);
  175. this.radioId = row.rowid;
  176. },
  177. addData() {
  178. console.log("新增按钮");
  179. this.modelVisi = true;
  180. this.modelTitle = "新增";
  181. },
  182. submitEvent() {
  183. console.log("模态框的按钮");
  184. console.log(this.formData);
  185. if (this.radioId == null) {
  186. postBook(this.formData).then((res) => {
  187. if (res.status === 200) {
  188. this.$message.success("新增图书成功");
  189. this.getBookDate();
  190. }
  191. });
  192. } else {
  193. putBook(this.radioId, this.formData).then((res) => {
  194. if (res.status === 200) {
  195. this.getBookDate();
  196. }
  197. });
  198. }
  199. this.close();
  200. },
  201. deleteData() {
  202. deleteBook(this.radioId).then((res) => {
  203. if (res.status === 204) {
  204. this.$message.success("删除图书成功");
  205. this.getBookDate();
  206. }
  207. });
  208. },
  209. updateData() {
  210. getBook(this.radioId).then((res) => {
  211. if (res.status === 200) {
  212. this.modelVisi = true;
  213. console.log("获取一个图书信息");
  214. console.log(res);
  215. let au = [];
  216. for (let v of res.data.author) {
  217. au.push(v.author_id);
  218. }
  219. res.data.author = au;
  220. this.formData = res.data;
  221. }
  222. });
  223. },
  224. close() {
  225. console.log("关闭");
  226. this.radioId = null;
  227. this.formData = {
  228. ISBN: "",
  229. author: [],
  230. name: "",
  231. price: 0,
  232. release_time: "",
  233. type: 1,
  234. };
  235. },
  236. search() {
  237. this.getBookDate({
  238. name__contains: this.searchV,
  239. });
  240. },
  241. qjSera() {
  242. this.getBookDate({
  243. release_time__gte: this.searchGte,
  244. release_time__lte: this.searchLte,
  245. });
  246. },
  247. },
  248. watch: {
  249. searchDate(vd) {
  250. let d = vd.split("-");
  251. this.getBookDate({
  252. release_time__year: d[0],
  253. release_time__month: d[1],
  254. release_time__day: d[2],
  255. });
  256. },
  257. },
  258. };
  259. </script>