amis icon indicating copy to clipboard operation
amis copied to clipboard

crud 列表删除操作前确认,确认后不执行api方法

Open xichangzhou opened this issue 6 years ago • 11 comments

以下为实现代码

添加confirmText: "您确认要删除?",这个属性就执行不了,去除该属性就能执行api方法

import React from "react";
import { render as renderAmis } from "amis";
import { message } from "antd";
import http from "../../utils/http";

const Home = () => {
  return (
    <div>
      {renderAmis(
        {
          type: "page",
          title: "增删改查示例",
          remark: "bla bla bla",
          body: {
            type: "crud",
            api: "/api/test/list",
            mode: "cards",
            defaultParams: {},
            filter: {
              title: "条件搜索",
              submitText: "搜索",
              controls: [
                {
                  type: "text",
                  name: "keywords",
                  placeholder: "通过关键字搜索",
                  // addOn: {
                  //   label: "搜索",
                  //   type: "submit",
                  // },
                },
              ],
            },
            filterTogglable: true,
            headerToolbar: ["bulkActions", "filter-toggler"],
            bulkActions: [
              {
                label: "批量删除",
                actionType: "ajax",
                api: "delete:https://houtai.baidu.com/api/sample/${ids|raw}",
                confirmText: "确定要批量删除?",
              },
              {
                label: "批量修改",
                actionType: "dialog",
                dialog: {
                  title: "批量编辑",
                  name: "sample-bulk-edit",
                  body: {
                    type: "form",
                    api: "https://houtai.baidu.com/api/sample/bulkUpdate2",
                    controls: [
                      {
                        type: "hidden",
                        name: "ids",
                      },
                      {
                        type: "text",
                        name: "engine",
                        label: "Engine",
                      },
                    ],
                  },
                },
              },
            ],
            switchPerPage: true,
            columnsCount: 4,
            quickSaveApi: "https://houtai.baidu.com/api/sample/bulkUpdate",
            quickSaveItemApi: "https://houtai.baidu.com/api/sample/$id",
            saveOrderApi: "https://houtai.baidu.com/api/sample",
            draggable: true,
            card: {
              header: {
                title: "$engine",
                subTitle: "$platform",
                subTitlePlaceholder: "暂无说明",
                avatar:
                  '<%= data.avatar || "https://internal-amis-res.cdn.bcebos.com/images/2019-12/1575350573496/4873dbfaf6a5.png" %>',
                avatarClassName: "pull-left thumb b-3x m-r",
              },
              actions: [
                {
                  type: "button",
                  label: "查看",
                  actionType: "dialog",
                  dialog: {
                    title: "查看",
                    body: {
                      type: "form",
                      controls: [
                        {
                          type: "static",
                          name: "engine",
                          label: "Engine",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "static",
                          name: "browser",
                          label: "Browser",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "static",
                          name: "platform",
                          label: "Platform(s)",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "static",
                          name: "version",
                          label: "Engine version",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "static",
                          name: "grade",
                          label: "CSS grade",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "html",
                          html:
                            "<p>添加其他 <span>Html 片段</span> 需要支持变量替换(todo).</p>",
                        },
                      ],
                    },
                  },
                },
                {
                  type: "button",
                  label: "编辑",
                  actionType: "dialog",
                  dialog: {
                    title: "编辑",
                    body: {
                      type: "form",
                      name: "sample-edit-form",
                      api: "https://houtai.baidu.com/api/sample/$id",
                      controls: [
                        {
                          type: "text",
                          name: "engine",
                          label: "Engine",
                          required: true,
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "text",
                          name: "browser",
                          label: "Browser",
                          required: true,
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "text",
                          name: "platform",
                          label: "Platform(s)",
                          required: true,
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "text",
                          name: "version",
                          label: "Engine version",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "text",
                          name: "grade",
                          label: "CSS grade",
                        },
                      ],
                    },
                  },
                },
                {
                  type: "button",
                  label: "删除",
                  actionType: "ajax",
                  confirmText: "您确认要删除?",
                  level: "danger",
                  api: "delete:/api/test/delete?id=${id}",
                },
              ],
              body: [
                {
                  name: "engine",
                  label: "engine",
                  sortable: true,
                  quickEdit: true,
                },
                {
                  name: "browser",
                  label: "Browser",
                  sortable: true,
                },
                {
                  name: "platform",
                  label: "Platform",
                  sortable: true,
                },
                {
                  name: "version",
                  label: "version",
                  sortable: true,
                },
              ],
            },
          },
        },
        {},
        {
          confirm: (msg: string) => {
            console.log(msg);
            return true;
          },
          notify: (_type: string, msg: string) => {
            message.info("success");
          },
          fetcher: (config: any) => {
            console.log(config);

            return (http as any)(config);
          },
        }
      )}
    </div>
  );
};

export default Home;

xichangzhou avatar Aug 28 '20 07:08 xichangzhou

复现不了,fetcher执行了吗

RickCole21 avatar Aug 28 '20 07:08 RickCole21

就是fetch没有执行,但是我不添加confirmText这个属性,就可以直接删除了,添加了就直接执行不了fetch方法了

xichangzhou avatar Aug 28 '20 07:08 xichangzhou

我粘了你的代码,fetcher是可以走进去的,你再自己检查一下吧

image

RickCole21 avatar Aug 28 '20 07:08 RickCole21

麻烦用下面新代码重新执行

import React from "react";
import { render as renderAmis } from "amis";
import { message } from "antd";
import http from "../../utils/http";

const Home = () => {
  return (
    <div>
      {renderAmis(
        {
          type: "page",
          title: "增删改查示例",
          remark: "bla bla bla",
          body: {
            type: "crud",
            api: "/api/test/list",
            mode: "cards",
            defaultParams: {},
            filter: {
              title: "条件搜索",
              submitText: "搜索",
              controls: [
                {
                  type: "text",
                  name: "keywords",
                  placeholder: "通过关键字搜索",
                  // addOn: {
                  //   label: "搜索",
                  //   type: "submit",
                  // },
                },
              ],
            },
            filterTogglable: true,
            headerToolbar: ["bulkActions", "filter-toggler"],
            bulkActions: [
              {
                label: "批量删除",
                actionType: "ajax",
                api: "delete:https://houtai.baidu.com/api/sample/${ids|raw}",
                confirmText: "确定要批量删除?",
              },
              {
                label: "批量修改",
                actionType: "dialog",
                dialog: {
                  title: "批量编辑",
                  name: "sample-bulk-edit",
                  body: {
                    type: "form",
                    api: "https://houtai.baidu.com/api/sample/bulkUpdate2",
                    controls: [
                      {
                        type: "hidden",
                        name: "ids",
                      },
                      {
                        type: "text",
                        name: "engine",
                        label: "Engine",
                      },
                    ],
                  },
                },
              },
            ],
            switchPerPage: true,
            columnsCount: 4,
            quickSaveApi: "https://houtai.baidu.com/api/sample/bulkUpdate",
            quickSaveItemApi: "https://houtai.baidu.com/api/sample/$id",
            saveOrderApi: "https://houtai.baidu.com/api/sample",
            draggable: true,
            card: {
              header: {
                title: "$engine",
                subTitle: "$platform",
                subTitlePlaceholder: "暂无说明",
                avatar:
                  '<%= data.avatar || "https://internal-amis-res.cdn.bcebos.com/images/2019-12/1575350573496/4873dbfaf6a5.png" %>',
                avatarClassName: "pull-left thumb b-3x m-r",
              },
              actions: [
                {
                  type: "button",
                  label: "查看",
                  actionType: "dialog",
                  dialog: {
                    title: "查看",
                    body: {
                      type: "form",
                      controls: [
                        {
                          type: "static",
                          name: "engine",
                          label: "Engine",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "static",
                          name: "browser",
                          label: "Browser",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "static",
                          name: "platform",
                          label: "Platform(s)",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "static",
                          name: "version",
                          label: "Engine version",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "static",
                          name: "grade",
                          label: "CSS grade",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "html",
                          html:
                            "<p>添加其他 <span>Html 片段</span> 需要支持变量替换(todo).</p>",
                        },
                      ],
                    },
                  },
                },
                {
                  type: "button",
                  label: "编辑",
                  actionType: "dialog",
                  dialog: {
                    title: "编辑",
                    body: {
                      type: "form",
                      name: "sample-edit-form",
                      api: "https://houtai.baidu.com/api/sample/$id",
                      controls: [
                        {
                          type: "text",
                          name: "engine",
                          label: "Engine",
                          required: true,
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "text",
                          name: "browser",
                          label: "Browser",
                          required: true,
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "text",
                          name: "platform",
                          label: "Platform(s)",
                          required: true,
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "text",
                          name: "version",
                          label: "Engine version",
                        },
                        {
                          type: "divider",
                        },
                        {
                          type: "text",
                          name: "grade",
                          label: "CSS grade",
                        },
                      ],
                    },
                  },
                },
                {
                  type: "button",
                  label: "删除",
                  actionType: "ajax",
                  confirmText: "您确认要删除?",
                  api: "delete:/api/test/delete?id=${id}",
                },
              ],
              body: [
                {
                  name: "engine",
                  label: "engine",
                  sortable: true,
                  quickEdit: true,
                },
                {
                  name: "browser",
                  label: "Browser",
                  sortable: true,
                },
                {
                  name: "platform",
                  label: "Platform",
                  sortable: true,
                },
                {
                  name: "version",
                  label: "version",
                  sortable: true,
                },
              ],
            },
          },
        },
        {},
        {
          // confirm: (msg: string) => {
          //   console.log(msg);
          //   return true;
          // },
          notify: (_type: string, msg: string) => {
            message.info("success");
          },
          fetcher: (config: any) => {
            console.log(config);

            return (http as any)(config);
          },
        }
      )}
    </div>
  );
};

export default Home;

xichangzhou avatar Aug 28 '20 08:08 xichangzhou

上诉您说可以执行到fetch,是因为我添加了下面这个,就直接导致没有弹框了弹框了,所有才执行的

confirm: content => {
      // 可以不传,用来实现确认框。
    }

xichangzhou avatar Aug 28 '20 08:08 xichangzhou

试过了,不传confirm也会调用系统的confirm,所以还是可以执行fetcher的,你看看你console报啥错了啊

RickCole21 avatar Aug 28 '20 08:08 RickCole21

如下错误

Uncaught TypeError: env.confirm(...).then is not a function

xichangzhou avatar Aug 28 '20 08:08 xichangzhou

https://baidu.gitee.io/amis/docs/start/getting-started#%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97

你按照这个把confirm实现了吧

RickCole21 avatar Aug 28 '20 09:08 RickCole21

按照官网confirm是不必须实现的,但是现在必须实现confirm,不然就会报错,添加了confirm就好了,感谢!

xichangzhou avatar Aug 28 '20 09:08 xichangzhou

你看的是sdk的文档,但是你使用方法是npm

RickCole21 avatar Aug 28 '20 09:08 RickCole21

好吧,是我的问题,看文档不仔细,麻烦您了

xichangzhou avatar Aug 28 '20 09:08 xichangzhou