amis icon indicating copy to clipboard operation
amis copied to clipboard

CRUD的filter功能,filter参数无法传到API

Open fayeah opened this issue 5 years ago • 5 comments

利用filter功能的时候,如果不使用adaptor,filter的参数可以正常传到API,但是如果使用了adaptor之后,filter无法添加到API的请求参数里面,这种怎么解决呀?

fayeah avatar Mar 27 '21 10:03 fayeah

信息太少

RickCole21 avatar Mar 29 '21 10:03 RickCole21

信息太少

show you the code:

{
      type: "crud",
      api: {
        method: "get",
        url:'',
        data: {
          page_entries: "${page_entries}",
          page_index: "${page|minus:1}",
        },
        requestAdaptor: function (api: ApiObject) {
          return {
            ...api,
          };
        },
      filter: {
        title: "条件搜索",
        mode: "inline",
        controls: [
          {
            label: "通过关键字搜索",
            type: "text",
            name: "keywords",
            placeholder: "通过关键字搜索",
          },
          {
            label: "用户",
            type: "text",
            name: "xx",
            value: 'xx',
          },
          {
            type: "submit",
            label: "搜索"
          }
        ],
      },
    },

这里的Filter 里面的值,我点了“搜索”按钮,传不到request里面。

fayeah avatar Mar 29 '21 10:03 fayeah

get请求的话,参数应该拼接在url上了,就是api.url

RickCole21 avatar Mar 29 '21 10:03 RickCole21

get请求的话,参数应该拼接在url上了,就是api.url

{
      type: "crud",
      api: {
        method: "get",
        api: "xxx",
        filter: {
          title: "条件搜索",
          mode: "inline",
          controls: [
            {
              label: "通过关键字搜索",
              type: "text",
              name: "keywords",
              placeholder: "通过关键字搜索",
            },
            {
              label: "用户",
              type: "text",
              name: "xx",
              value: 'xx',
            },
            {
              type: "submit",
              label: "搜索"
            }
          ],
        },
    },

这种是可以的,api用了object形式之后就不行

fayeah avatar Mar 29 '21 11:03 fayeah

get请求的话,参数应该拼接在url上了,就是api.url

我找到原因了, 是因为"data",如果在api里面设置了data,那filter 参数就无法自动传到api了,但是我这里data有用的,因为pageIndex需要从0开始,而默认的是从1开始。所以我的解决方案是这样的:

api: {
        method: "get",
        url: "xxx",
        requestAdaptor: function (api: ApiObject) {
          const page_index = api.query?.page - 1
          const query = {
            ...api.query,
            page_index,
          }
          const requestApi = {
            ...api,
            query,
            url: api.url + `&page_index=${page_index}`
          }
          return { ...requestApi };
        },
}

fayeah avatar Mar 31 '21 06:03 fayeah