Search code examples
javascriptgojs

How can I get the row value when edit any text in row of scrolling table in Gojs?


I'm working with scrolling table in Gojs. I've set the property "editable: true" for my table. Now, suppose I've edited a text in any column of a row, then how can I get the data for this entire row or edited text? Please let me know if you know this.

Here is my code:

            var nodeJson;
        var $ = go.GraphObject.make;
        var inputFieldTable = [
            { ID: "001", Name: "Input 1", Text: "Err1" },
            { ID: "002", Name: "Input 2", Text: "Err2" },
            { ID: "003", Name: "Input 3", Text: "Err3" },
            { ID: "004", Name: "Input 4", Text: "Err4" }
        ];

        var outputFieldTable = [
            { ID: "101", Name: "Output 1", Text: "Integer" },
            { ID: "102", Name: "Output 2", Text: "Integer" },
            { ID: "103", Name: "Output 3", Text: "Integer" },
            { ID: "104", Name: "Output 4", Text: "String" },
            { ID: "105", Name: "Output 5", Text: "String" },
            { ID: "106", Name: "Output 6", Text: "Double" }
        ];
        myDiagram =
            $(go.Diagram, "myDiagramDiv",
                {
                    initialContentAlignment: go.Spot.Center,
                    "undoManager.isEnabled": true,
                    allowMove: false,
                    allowDelete: true,
                    allowCopy: false,
                    allowDragOut: false,
                    allowDrop: false
                });

        myDiagram.nodeTemplate =
            $(go.Node, "Vertical",
                {
                    selectionObjectName: "SCROLLING",
                    resizable: false, resizeObjectName: "SCROLLING",
                    portSpreading: go.Node.SpreadingNone
                },
                new go.Binding("location").makeTwoWay(),
                $(go.TextBlock,
                    { font: "bold 14px sans-serif" },
                    new go.Binding("text", "key")),
                $(go.Panel, "Auto",
                    $(go.Shape, { fill: "white" }),
                    $("ScrollingTable",
                        { stretch: go.GraphObject.Fill },
                        new go.Binding("TABLE.itemArray", "items"),
                        new go.Binding("TABLE.column", "left", function (left) { return left ? 2 : 0; }),
                        new go.Binding("desiredSize", "size").makeTwoWay(),
                        {
                            name: "SCROLLING",
                            desiredSize: new go.Size(100, 100),
                            "TABLE.itemTemplate":
                            $(go.Panel, "TableRow",
                                {
                                    defaultStretch: go.GraphObject.Horizontal,
                                    fromSpot: go.Spot.LeftRightSides,
                                    toSpot: go.Spot.LeftRightSides,
                                    fromLinkable: true,
                                    toLinkable: true,
                                },
                                new go.Binding("portId", "Name"),
                                $(go.TextBlock, { column: 1 }, new go.Binding("text", "Name")),
                                $(go.TextBlock, { column: 2 }, new go.Binding("text", "Text"), { editable: true })
                            ),
                            "TABLE.defaultColumnSeparatorStroke": "gray",
                            "TABLE.defaultColumnSeparatorStrokeWidth": 0.5,
                            "TABLE.defaultRowSeparatorStroke": "gray",
                            "TABLE.defaultRowSeparatorStrokeWidth": 0.5,
                            "TABLE.defaultSeparatorPadding": new go.Margin(1, 3, 0, 3)
                        }
                    )
                )
            );

        myDiagram.model = $(go.GraphLinksModel,
            {
                linkFromPortIdProperty: "fromPort",
                linkToPortIdProperty: "toPort",
                nodeDataArray: [
                    {
                        key: "Input", left: true, location: new go.Point(0, 0), size: new go.Size(170, 100),
                        items: inputFieldTable
                    },
                    {
                        key: "Output", location: new go.Point(300, 0), size: new go.Size(170, 100),
                        items: outputFieldTable, editable: true
                    }
                ]
            });

        //Function to handle editing of Scrolling Tables row data
        myDiagram.addDiagramListener("TextEdited",
            function (e) {
               // alert("Text is changed.");
                var part = e.subject.part;                  
                if (part.data.key.toUpperCase() == "INPUT") {
                    myDiagram.rollbackTransaction();
                    return false;
                }
                else if (part.data.key.toUpperCase() == "OUTPUT") {
                    if ((part instanceof go.Node)) {
                        //NEED TO KNOW THE ENTIRE ROW DATA HERE
                    }
                }

            });

Solution

  • Are you asking about how to get to the item data from within your "TextEdited" listener?

    • e.subject will be the edited TextBlock.
    • e.subject.panel will be the containing Panel, which is a "TableRow" in your Panel.itemTemplate.
    • e.subject.panel.data will be the item data -- i.e. the data for that row.

    This applies to all Panels with itemArrays -- not just in a "ScrollingTable".

    It's a little odd for a TextBlock.editable TextBlock not to have a TwoWay Binding, but it can be OK depending on what you do in your "TextEdited" listener.