0

我编写了以下代码,但我不确定为什么每个初级级别的二级菜单项总是相同的。我知道这可能与范围变量有关。你能建议一个替代方案吗?``

editor.ui.registry.addMenuButton(insertFields, {
      text: My button,
      fetch: function (callback) {
        var temp = [];

        for(var i = 0; i < k.state.ListItems.length; i++)
        {
          var obj = {};
          var fieldItems = k.state.ListItems[i].description;
          console.log(fieldItems);
          obj["text"] = k.state.ListItems[i].Title;
          obj["type"] = "nestedmenuitem";
          obj["getSubmenuItems"] = () => {
            console.log("fired for submenu");
            var getMenuItem = t => {
              return {
                type: "menuitem",
                text: t,
                onAction: () => {
                  editor.insertContent("{{ " + t + "}}");
                }
              };
            };
            var nestedItems = [];
            for (let k = 0; k < fieldItems.length; k++) {
              nestedItems.push(getMenuItem(fieldItems[k].Title ));
            }
            return nestedItems;
          }

          temp.push(obj);
          console.log(temp);
        }

        console.log(temp);
        var items =  temp;

        callback(items);
      }
    });

            }
         }}
  />
4

1 回答 1

0

能够解决这个问题。不确定是否有更好的方法。

         setup: function (editor) {
           for(let i = 0; i < k.state.ListItems.length;i++)
           {
           // let Fields = k.state.ListItems[i].Fields;
            editor.ui.registry.addNestedMenuItem(k.state.ListItems[i].Id, {
            text: k.state.ListItems[i].Title,
            getSubmenuItems: () => {
              var getMenuItem = (t,r) => {
                return {
                  type: "menuitem",
                  text: t,
                  onAction: () => {
                    editor.insertContent("{{" + r +"|" + t + "}}");
                  }
                };
              };
              var nestedItems = [];
              for (let j = 0; j < k.state.ListItems[i].Fields.length; j++) {
               // console.log(Fields[j].Title);
                nestedItems.push(getMenuItem(k.state.ListItems[i].Fields[j].Title,k.state.ListItems[i].Title));
              }
              return nestedItems;
            }
          });
        }

         }
于 2020-05-26T19:43:37.673 回答