简体   繁体   English

如何更改 go.js 中按钮的文本?

[英]how to change text of button in go.js?

I have created a button which expands the node on click.我创建了一个按钮,可以在单击时展开节点。 but after click event the text of button does not change to minus.但点击事件后按钮的文本不会变为减号。
how can i change the text of button on click event for just one node?如何仅更改一个节点的单击事件按钮的文本?

$("Button", {
    cursor: "pointer",
    name: 'TREEBUTTON',
    width: 20, height: 20,
    alignment: go.Spot.TopRight,
    alignmentFocus: go.Spot.Center,
    // customize the expander behavior to
    // create children if the node has never been expanded
    click: function (e, obj) {  // OBJ is the Button
        var node = obj.part;  // get the Node containing this Button
        if (node === null) return;
        e.handled = true;
        expandNode(node);
    }
}, $(go.TextBlock, "+"));

Note you can see the whole implementation of all built-in buttons, like the TreeExpanderButton (which changes its Shape) here: https://gojs.net/latest/extensions/Buttons.js请注意,您可以在此处看到所有内置按钮的完整实现,例如 TreeExpanderButton(更改其形状): https://gojs.net/latest/extensions/Buttons.js

You can modify this to do the extra stuff you want.你可以修改它来做你想要的额外的东西。

// This is a complete Button that you can have in a Node template
// to allow the user to collapse/expand the subtree beginning at that Node.

// Typical usage within a Node template:
//    $('TreeExpanderButton')

go.GraphObject.defineBuilder('TreeExpanderButton', function (args) {
  var button = /** @type {Panel} */ (
    go.GraphObject.make('Button',
      { // set these values for the isTreeExpanded binding conversion
        '_treeExpandedFigure': 'MinusLine',
        '_treeCollapsedFigure': 'PlusLine'
      },
      go.GraphObject.make(go.Shape,  // the icon
        {
          name: 'ButtonIcon',
          figure: 'MinusLine',  // default value for isTreeExpanded is true
          stroke: '#424242',
          strokeWidth: 2,
          desiredSize: new go.Size(8, 8)
        },
        // bind the Shape.figure to the Node.isTreeExpanded value using this converter:
        new go.Binding('figure', 'isTreeExpanded',
          function (exp, shape) {
            var but = shape.panel;
            return exp ? but['_treeExpandedFigure'] : but['_treeCollapsedFigure'];
          }
        ).ofObject()
      ),
      // assume initially not visible because there are no links coming out
      { visible: false },
      // bind the button visibility to whether it's not a leaf node
      new go.Binding('visible', 'isTreeLeaf',
        function (leaf) { return !leaf; }
      ).ofObject()
    )
  );

  // tree expand/collapse behavior
  button.click = function (e, btn) {
    var node = btn.part;
    if (node instanceof go.Adornment) node = node.adornedPart;
    if (!(node instanceof go.Node)) return;
    var diagram = node.diagram;
    if (diagram === null) return;
    var cmd = diagram.commandHandler;
    if (node.isTreeExpanded) {
      if (!cmd.canCollapseTree(node)) return;
    } else {
      if (!cmd.canExpandTree(node)) return;
    }
    e.handled = true;
    if (node.isTreeExpanded) {
      cmd.collapseTree(node);
    } else {
      cmd.expandTree(node);
    }
  };

  return button;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM