amis icon indicating copy to clipboard operation
amis copied to clipboard

aside 和 body 如何进行数据交互

Open rodchen-king opened this issue 6 years ago • 5 comments

实现场景:

image

aside 左侧是一个树组件,选择不同的树组件,右侧表格需要根据左侧选择的数据条件查询数据。

存在的问题:

左侧选择的数据不知道如何传递到右侧body里去

rodchen-king avatar Sep 02 '20 10:09 rodchen-king

amis 是做单页的,导航用 react-router 等前端路由,或者链接到新页面。

nwind avatar Sep 02 '20 10:09 nwind

aside内放置form1,body放置form2,form1的target为form2

yuanshengyu avatar Nov 05 '20 11:11 yuanshengyu

请问这个问题有解决方案吗

user-huage avatar Sep 16 '21 01:09 user-huage

Image

你看看这种实现方式是否可以满足你的需求。 整体思路:

  1. aside使用nav组件, body使用tabs组件, nav组件内监听click事件并使用actionType:link 站内跳转。
  2. index.html里面重写jumpTo,处理actionType:link事件。
  3. jumpTo中根据link事件的url操作tabs组件。

代码如下:

index.html

let amis = amisRequire('amis/embed');
        // 通过替换下面这个配置来生成不同页面
        let amisJSON = {
            type: "page",
            className: "w-full h-full",
            asideClassName: "w-auto",
            asideResizor: true,
            aside: {
                type : "service",
                schemaApi: "get:/left.json"
            },
            body: {
                type : "service",
                schemaApi: "get:/right.json"
            }
        };
        let amisScoped = amis.embed(
            '#root', 
            amisJSON,
            {},
            {
                jumpTo: (to, action) => {
                    console.log("jumpTo:" + to);
                    //查找body中的 tabs组件 mainContent
                    const mainContent = amisScoped.getComponentById('mainContent');
                    //获取所有tabs
                    const { localTabs = [] } = mainContent.state;
                    
                    //查找当前to 是否已存在tabs, 如果存在,直接跳转tab.
                    for(tabIndex in localTabs) {
                        if (localTabs[tabIndex].hash == to) {
                            mainContent.switchTo(tabIndex);
                            return;
                        }
                    }

                    let title = `new tab ${localTabs.length + 1}`
                    
                    //新建tab的参数。
                    const newTab = {
                        title: title,
                        body: {
                            "type":"service",
                            "schemaApi": "get:" + to
                        },
                        hash: to
                    };
                   
                    //添加新tab并更新整个tabs组件
                    const updatedTabs = [...localTabs, newTab];
                    mainContent.setState(
                        { localTabs: updatedTabs },
                        () => {
                            // 自动切换到新添加的 tab
                            mainContent.switchTo(updatedTabs.length - 1);
                        }
                    );
                }
            }
        );
      })();

left.json 侧边栏导航组件

{
    "type": "nav",
    "stacked": true,
    "className": "w-md",
    "links": [
      {
        "label": "Nav 1",
        "to": "/tab1.json",
        "icon": "fa fa-user",
        "active": true
      },
      {
        "label": "Nav 2",
        "unfolded": true,
        "children": [
          {
            "label": "Nav 2-1",
            "children": [
              {
                "label": "Nav 2-1-1",
                "to": "/tab2.json"
              }
            ]
          },
          {
            "label": "Nav 2-2",
            "to": "/tab2.json"
          }
        ]
      },
      {
        "label": "Nav 3",
        "to": "/tab4.json"
      }
    ],
    "onEvent": {
        "click": { //点击后进行link跳转,这个link跳转后,会使用jumpTo来完成,上面在index.html中我们已重写了jumpTo.
          "actions": [
            {
              "actionType": "link",
              "args": {
                "url": "${event.data.item.to}"
              }
            }
          ]
        }
      }
  }

right.json 右侧主内容区组件。

{
  "type": "page",
  "body": {
    "type": "tabs",
    "id":"mainContent", //这个ID非常重要,在index.html中需要通过这个ID,找到本组件。
    "closable": true,
    "tabsMode": "strong",
    "tabs": [
            {
        "title": "默认tab",
        "tab": "默认tab 默认tab 默认tab"
      }
    ]
  }
}

tab_${x}.json 跟据业务自行添加

harryhuang2015 avatar Mar 20 '26 13:03 harryhuang2015

另外还有一种思路,在nav组件里click事件里,直接给tabs组件发送广播事件,参数为url,tabs内接收事件后再处理。 这种思路没有写代码测试,有时间的朋友可以试一下。

harryhuang2015 avatar Mar 20 '26 13:03 harryhuang2015