vue2企业级项目(八)

这篇具有很好参考价值的文章主要介绍了vue2企业级项目(八)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

vue2企业级项目(八)

组件封装(二)

4、searchForm
  • 创建components/searchForm/index.js

    import XSearchForm from "./index.vue";
    export default XSearchForm;
    
    
  • 使用案例

    <template>
      <div class="wrap">
        <h3>基础搜索</h3>
        <div class="box">
          <x-search-form :columns="columns1" @search="handleSearch"></x-search-form>
        </div>
    
        <h3>自动布局</h3>
        <div class="box">
          <x-search-form
            :columns="columns2"
            @search="handleSearch"
            :lineNum="4"
          ></x-search-form>
        </div>
    
        <h3>自定义搜索按钮</h3>
        <div class="box">
          <x-search-form
            :columns="columns3"
            :hasSearch="false"
            @search="handleSearch"
            ref="search"
          ></x-search-form>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      name: "Page7",
      data() {
        return {
          searchLoading: false,
          resetLoading: false,
          columns1: [
            { type: "input", label: "姓名", prop: "name" },
            { type: "input", label: "年龄", prop: "age" },
            {
              type: "select",
              label: "性别",
              prop: "sex",
              options: [
                { label: "男", value: "1" },
                { label: "女", value: "0" },
              ],
            },
          ],
          columns2: [
            { type: "input", label: "姓名1", prop: "name1" },
            { type: "input", label: "姓名2", prop: "name2" },
            { type: "input", label: "姓名3", prop: "name3" },
            { type: "input", label: "姓名4", prop: "name4" },
            { type: "input", label: "姓名5", prop: "name5" },
          ],
          columns3: [
            { type: "input", label: "姓名", prop: "name" },
            {
              span: 16,
              align: "right",
              render: () => {
                return (
                  <div>
                    <el-button type="success" icon="el-icon-upload">
                      导入
                    </el-button>
                    <el-button type="warning" icon="el-icon-download">
                      导出
                    </el-button>
                    <el-button
                      type="primary"
                      icon="el-icon-search"
                      loading={this.searchLoading}
                      onClick={this.triggerSearch}
                    >
                      搜索
                    </el-button>
                    <el-button
                      icon="el-icon-refresh"
                      loading={this.resetLoading}
                      onClick={this.triggerReset}
                    >
                      重置
                    </el-button>
                  </div>
                );
              },
            },
          ],
        };
      },
      mounted() {},
    
      methods: {
        handleSearch(searchForm, callback) {
          if (searchForm) {
            console.log(searchForm);
            this.$message.success("搜索成功");
          } else {
            this.$message.success("重置成功");
          }
          setTimeout(() => {
            callback();
            this.searchLoading = false;
            this.resetLoading = false;
          }, 1000);
        },
    
        triggerSearch() {
          this.searchLoading = true;
          this.$refs.search.triggerSearch();
        },
    
        triggerReset() {
          this.resetLoading = true;
          this.$refs.search.triggerReset();
        },
      },
    };
    </script>
    
    <style lang="less" scoped>
    .wrap {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      flex-direction: column;
    }
    
    .box {
      width: 800px;
      height: 200px;
      padding: 10px;
      margin-bottom: 20px;
      background: #1f03034d;
    }
    </style>
    
    
5、searchTable
  • 创建components/searchTable/index.js

    import XSearchTable from "./index.vue";
    export default XSearchTable;
    
    
  • 创建components/searchTable/index.vue

    <template>
      <div class="x-search-table">
        <x-search-form
          v-bind="searchConfig"
          :columns="searchFields"
          @search="handleSearch"
          ref="search"
        ></x-search-form>
        <div class="x-search-table-box">
          <x-table
            v-bind="tableConfig"
            :columns="tableFields"
            :api="api"
            :tableData="data"
            ref="table"
          ></x-table>
        </div>
      </div>
    </template>
    
    <script>
    /**
     * @param { Array } columns: 配置
     * @param { Array } data: 数据
     * @param { Function } api: 接口
     * @param { Object } searchForm: search的配置
     * @param { Object } tableForm: table的配置
     *
     * @event { Function } getSearchForm: 获取搜索值
     * @event { Function } setSearchForm: 获取搜索值
     * @event { function } toggleSelection:设置默认多选
     * @event { function } setCurrent: 设置单选
     * @event { function } clearFilter:清空筛选项
     * @event { function } getSelection: 获取选中结果
     * @event { function } getTableData: 获取表格值
     */
    
    export default {
      name: "XSearchTable",
      props: {
        columns: {
          type: Array,
          default: () => [],
        },
        data: {
          type: Array,
          default: () => [],
        },
        api: {
          type: Function,
          default: null,
        },
        searchConfig: {
          type: Object,
          default: () => ({}),
        },
        tableConfig: {
          type: Object,
          default: () => ({}),
        },
      },
      data() {
        return {
          searchFields: [],
          tableFields: [],
          searchForm: null,
        };
      },
      watch: {
        columns: {
          immediate: true,
          deep: true,
          handler() {
            this.handleFields();
          },
        },
      },
      methods: {
        handleFields() {
          this.searchFields = this.columns.map((item) => {
            let searchItem = { ...item, ...item.search };
            delete searchItem.form;
            delete searchItem.table;
            delete searchItem.search;
            delete searchItem.dialog;
            return searchItem;
          });
    
          this.tableFields = this.columns.map((item) => {
            let tableItem = { ...item, ...item.table };
            delete tableItem.form;
            delete tableItem.table;
            delete tableItem.search;
            delete tableItem.dialog;
            return tableItem;
          });
        },
    
        handleSearch(searchForm, callback) {
          this.searchForm = searchForm;
          this.$refs.table.setTableData(searchForm).then(callback);
        },
    
        getSearchForm() {
          return this.$refs.search.getSearchForm();
        },
    
        setSearchForm(form) {
          this.$refs.search.setSearchForm(form);
        },
    
        toggleSelection(rows) {
          this.$refs.table.toggleSelection(rows);
        },
    
        setCurrent(row) {
          this.$refs.table.setCurrent(row);
        },
    
        clearFilter() {
          this.$refs.table.clearFilter();
        },
    
        getSelection() {
          return this.$refs.table.getSelection();
        },
    
        getTableData() {
          return this.$refs.table.getTableData();
        },
      },
    };
    </script>
    
    <style lang="less" scoped>
    .x-search-table {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      flex-direction: column;
    
      .x-search-table-box {
        width: 100%;
        flex: 1;
      }
    }
    </style>
    
    
  • 使用案例

    <template>
      <div class="wrap">
        <!-- 基础搜索 -->
        <h3>基础搜索</h3>
        <div class="box">
          <x-search-table :columns="columns" :data="data"></x-search-table>
        </div>
    
        <!-- 接口搜索 -->
        <h3>接口搜索</h3>
        <div class="box">
          <x-search-table :columns="columns2" :api="getList"></x-search-table>
        </div>
      </div>
    </template>
    
    <script>
    import { getList } from "@/api/mock.js";
    
    export default {
      name: "Page8",
      data() {
        return {
          columns: [
            { type: "input", label: "姓名", prop: "name" },
            { type: "input", label: "年龄", prop: "age" },
            {
              type: "select",
              label: "性别",
              prop: "sex",
              options: [
                { label: "男", value: "1" },
                { label: "女", value: "0" },
              ],
            },
            {
              type: "input",
              label: "地址",
              prop: "address",
              search: { isShow: false },
            },
          ],
          data: [],
    
          columns2: [
            { type: "input", label: "姓名", prop: "name" },
            { type: "input", label: "年龄", prop: "age" },
            {
              type: "select",
              label: "性别",
              prop: "sex",
              options: [
                { label: "男", value: "1" },
                { label: "女", value: "0" },
              ],
            },
            { label: "日期", prop: "date", props: { valueFormat: "yyyy-MM-DD" } },
            {
              type: "input",
              label: "地址",
              prop: "address",
              search: { isShow: false },
            },
          ],
        };
      },
      created() {
        this.init();
      },
      methods: {
        getList: getList,
    
        init() {
          let list = new Array(99).fill({});
          list = list.map((item, index) => {
            return {
              name: index > 20 ? `张三${index}` : "张三",
              age: index.toString(),
              sex: (index % 2).toString(),
              address: `北京市朝阳区${index}号`,
            };
          });
          this.data = list;
        },
      },
    };
    </script>
    
    <style lang="less" scoped>
    .wrap {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      flex-direction: column;
    }
    
    .box {
      width: 800px;
      height: 700px;
      padding: 10px;
      margin-bottom: 20px;
      background: #1f03034d;
    }
    </style>
    
    
6、dialogForm
  • 创建components/dialogForm/index.js

    import XSearchTable from "./index.vue";
    export default XSearchTable;
    
    
  • 创建components/dialogForm/index.vue

    <template>
      <x-dialog
        submitText="提交"
        :submitLoading="true"
        :title="title"
        v-bind="dialogConfig"
        ref="dialog"
      >
        <x-form
          labelWidth="100px"
          v-bind="formConfig"
          :columns="formFields"
          :defaultValue="defaultForm"
          :ctrl="false"
          ref="form"
        ></x-form>
      </x-dialog>
    </template>
    
    <script>
    /**
     * @param { Array } columns: 配置
     * @param { String } defaultTitle: 默认标题
     * @param { String } addTitle: 新增标题
     * @param { String } editTitle: 编辑标题
     * @param { Boolean } saveAdd: 是否暂存新增的内容
     * @param { Object } dialogConfig: dialog的配置
     * @param { Object } formConfig: form的配置
     *
     * @event { Function } open: 打开dialogform
     * @event { Function } triggerSubmit: 手动触发表单校验,校验通过后关闭dialog
     * @evnet { Function } triggerReset: 手动触发表单重置,完成后关闭dialog
     *
     */
    
    export default {
      name: "XDialogForm",
      props: {
        columns: {
          type: Array,
          default: () => [],
        },
        defaultTitle: {
          type: String,
          default: "弹框",
        },
        addTitle: {
          type: String,
          default: "新增",
        },
        editTitle: {
          type: String,
          default: "编辑",
        },
        saveAdd: {
          type: Boolean,
          default: false,
        },
        dialogConfig: {
          type: Object,
          default: () => ({}),
        },
        formConfig: {
          type: Object,
          default: () => ({}),
        },
      },
      data() {
        return {
          title: "",
          formFields: [],
          defaultForm: {},
          handleResolve: null,
          handleReject: null,
          handleCallback: null,
        };
      },
      watch: {
        columns: {
          immediate: true,
          deep: true,
          handler() {
            this.init();
          },
        },
      },
      methods: {
        init() {
          this.formFields = this.columns.map((item) => {
            const formItem = { ...item, ...item.dialog };
            delete formItem.form;
            delete formItem.table;
            delete formItem.search;
            delete formItem.dialog;
    
            return formItem;
          });
    
          this.formFields = this.formFields.filter((item) => item.isShow !== false);
        },
    
        open(form) {
          if (!this.saveAdd || form) {
            this.defaultForm = form || {};
          }
          if (form) this.title = this.editTitle || this.defaultTitle;
          else this.title = this.addTitle || this.defaultTitle;
    
          this.$refs.dialog
            .open()
            .then((callback) => {
              this.handleCallback = callback;
              this.triggerSubmit();
            })
            .catch(this.triggerReset.bind(this));
    
          return new Promise((resolve, reject) => {
            this.handleResolve = resolve;
            this.handleReject = reject;
          });
        },
    
        triggerSubmit() {
          this.$refs.form
            .submit()
            .then(({ form, valid }) => {
              console.log(form, valid);
              if (valid) {
                this.handleResolve(form);
                this.handleCallback();
                this.closeBefore();
              } else {
                this.handleCallback(false);
              }
            })
            .finally(() => {
              this.handleCallback = null;
            });
        },
    
        triggerReset() {
          if (this.addTitle && this.title === this.addTitle) {
            this.close();
            return true;
          }
    
          this.closeBefore();
        },
    
        closeBefore() {
          this.defaultForm = {};
          this.$refs.form.reset().then(() => {
            this.handleReject();
            this.close();
          });
        },
    
        close() {
          this.title = "";
          this.handleResolve = null;
          this.handleReject = null;
          this.handleCallback = null;
        },
      },
    };
    </script>
    
    
  • 使用案例文章来源地址https://www.toymoban.com/news/detail-621101.html

    <template>
      <div>
        <el-button type="primary" @click="openDialog()">新增</el-button>
        <el-button type="primary" @click="openDialog(data)">编辑</el-button>
    
        <x-dialog-form :columns="columns" ref="dialogForm"></x-dialog-form>
      </div>
    </template>
    
    <script>
    export default {
      name: "Page9",
      data() {
        return {
          columns: [
            { type: "input", label: "用户", prop: "name", required: true },
            { type: "input", label: "年龄", prop: "age" },
            {
              type: "radio",
              label: "性别",
              prop: "sex",
              options: [
                { label: "男", value: "1" },
                { label: "女", value: "0" },
              ],
            },
          ],
          data: {
            name: "张三",
            age: "20",
            sex: "1",
          },
        };
      },
      methods: {
        openDialog(data) {
          this.$refs.dialogForm
            .open(data)
            .then((form) => {
              console.log(form, "------编辑后的值");
            })
            .catch(() => {
              console.log("-----------取消");
            });
        },
      },
    };
    </script>
    
    

到了这里,关于vue2企业级项目(八)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Vue3.0 项目启动(打造企业级音乐App)

    内容 参考链接 Vue3.0 项目启动 Vue3.0 项目启动(打造企业级音乐App) Vue3.0项目——打造企业级音乐App(一) Tab栏、轮播图、歌单列表、滚动组件 Vue3.0项目——打造企业级音乐App(二) 图片懒加载、v-loading指令的开发和优化 2020年09月18日,vue3.0 版本正式发布。这意味着在未来

    2023年04月08日
    浏览(33)
  • Vue.js与ASP.NET的结合,实现企业级应用的开发和部署

    在当今快速发展的互联网技术领域,企业级应用的开发和部署变得越来越重要。Vue.js和ASP.NET是两个在前端和后端开发中广泛使用的技术,将它们结合起来可以为企业级应用的开发和部署带来诸多优势。本文将通过代码示例介绍如何使用Vue.js和ASP.NET进行企业级应用的开发和部

    2024年02月14日
    浏览(33)
  • Vue.js 与 ViewDesign:为企业级 Web 应用提供高效可靠的解决方案

    在当今瞬息万变的商业环境中,企业需要高效、稳定且易于维护的 Web 应用程序来支持其日常运营和业务发展。幸运的是,Vue.js 和 ViewDesign 的强大组合为开发人员提供了构建复杂企业级 Web 应用程序的完美解决方案。 Vue.js 是一个开源的渐进式 JavaScript 框架,专为构建用户界面而生

    2024年03月11日
    浏览(30)
  • Vue3 + Vite2 + TypeScript4搭建企业级项目框架

    1. 创建项目 使用命令行工具进入到你想要创建项目的目录,然后执行以下命令: 这将会创建一个新的项目文件夹和一个 package.json 文件。 2. 安装依赖 接下来你需要在项目中安装 Vue、Vite、TypeScript 和其他需要的依赖。执行以下命令: 以上命令会安装最新的 Vue、Vite 和 TypeSc

    2024年02月08日
    浏览(43)
  • java项目分享 - 基于SpringCloud+Hadoop+Vue的企业级网盘系统设计与实现

    基于SpringCloud+Hadoop+Vue的企业级网盘系统设计与实现 提示:适合用于课程设计或毕业设计,工作量达标,源码开放 前端:vue-projectManage 后台:mycloud-admin 提供前端服务:mycloud 文件在线预览服务:file-online-preview 编程语言:Java、Mybatis、Spring、SpringBoot、SpringCloud、Node、Vue 开发环

    2024年02月05日
    浏览(46)
  • 毕业设计项目 基于SpringCloud+Hadoop+Vue的企业级网盘系统设计与实现

    基于SpringCloud+Hadoop+Vue的企业级网盘系统设计与实现 提示:适合用于课程设计或毕业设计,工作量达标,源码开放 前端:vue-projectManage 后台:mycloud-admin 提供前端服务:mycloud 文件在线预览服务:file-online-preview 编程语言:Java、Mybatis、Spring、SpringBoot、SpringCloud、Node、Vue 开发环

    2024年04月24日
    浏览(42)
  • Vue3.0项目——打造企业级音乐App(一)Tab栏、轮播图、歌单列表、滚动组件

    内容 参考链接 Vue3.0 项目启动 Vue3.0 项目启动(打造企业级音乐App) Vue3.0项目——打造企业级音乐App(一) Tab栏、轮播图、歌单列表、滚动组件 Vue3.0项目——打造企业级音乐App(二) 图片懒加载、v-loading指令的开发和优化 vue3.0-music 该项目为移动端的项目,我们要设置缩放

    2023年04月11日
    浏览(37)
  • 前端食堂技术周刊第 85 期:5 月浏览器更新、TypeScript 5.1、Rspack 0.2.0、Parcel v2.9.0、Next.js 企业级模板

    美味值:🌟🌟🌟🌟🌟 口味:龙井酥 食堂技术周刊仓库地址:https://github.com/Geekhyt/weekly 5 月登陆浏览器的新功能 TypeScript 5.1 Rspack 0.2.0 Parcel v2.9.0 Next.js 企业级模板 SupportsCSS useHooks 大家好,我是童欧巴。欢迎来到前端食堂技术周刊,我们先来看下上周的技术资讯。 Firefox 1

    2024年02月08日
    浏览(39)
  • 【实战】 TS 应用:JS神助攻 - 强类型 —— React17+React Hook+TS4 最佳实践,仿 Jira 企业级项目(三)

    学习内容来源:React + React Hook + TS 最佳实践-慕课网 相对原教程,我在学习开始时(2023.03)采用的是当前最新版本: 项 版本 react react-dom ^18.2.0 react-router react-router-dom ^6.11.2 antd ^4.24.8 @commitlint/cli @commitlint/config-conventional ^17.4.4 eslint-config-prettier ^8.6.0 husky ^8.0.3 lint-staged ^13.1.2 p

    2024年02月09日
    浏览(36)
  • 毕业设计项目:基于SpringBoot+Hadoop+Vue企业级网盘分布式系统的设计与实现

    2.1 运行环境 2.2 基本处理流程 企业网盘系统的使用者分为企业普通员工和企业管理员,所以进行的基本处理流程是不一样的。企业普通员工进入本系统前台主界面后看到的是首页数据大盘,系统右上角有用户的头像和系统公告通知。在首页顶部的位置有个欢迎用户功能,此模

    2024年02月05日
    浏览(49)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包