简体   繁体   English

Autodesk forge 查看器表格行单击以隔离元素

[英]Autodesk forge viewer table row click to isolate elements

I am trying to create a dynamic table inside the viewer's page.我正在尝试在查看器页面内创建一个动态表。 each row of the table represents an object from the model and has a couple of different parameters ( name, level, etc..) I want to make a click event on each row, that isolates the element in the viewer's model.表格的每一行代表来自 model 的 object,并且有几个不同的参数(名称、级别等)。我想在每一行上进行单击事件,以隔离查看器的 Z20F35E630534DB88C3F6 中的元素。

I have created the table programmatically using js.我已经使用 js 以编程方式创建了表。 (the table is created after pressing an external command button ) (按下外部命令按钮后创建表)

how can I add an event listener to a row click, that would isolate the element?如何将事件侦听器添加到行单击中,以隔离元素?

this is my code for creating the table after pressing the command button:这是我按下命令按钮后创建表格的代码:

var myTable = $('.my-table');
var myArr = rows; //matrix of object values

for (var i = 0; i < myArr.length; i++)
    myTable.append("<tr id=" + i + ">" +
        " <td>" + myArr[i][0] + "</td>" +
        "<td>" + myArr[i][1] + "</td>" +
        "<td>" + myArr[i][2] + "</td>" +
        "<td>" + myArr[i][3] + "</td>" +
        "<td>" + myArr[i][4] + "</td>" +
        "<td>" + myArr[i][5] + "</td>" +
        "<td>" + myArr[i][6] + "</td>" +
        "<td id=" + "tt" + ">" + myArr[i][7] + "</td>" +
        "</tr>");
const row = document.getElementById(`${i}`);
row.addEventListener('onClick', function(evt, item) { /// we are using a viewer api property to  isolate the 
    _this.viewer.isolate(_this.modelData.getIds(myArrNames[i][1], item[0]._model.label));

});

First, it's not a Forge issue but a JavaScript syntax and indent issue.首先,这不是 Forge 问题,而是 JavaScript 语法和缩进问题。

After re-indenting your code, you can see the codes for adding the click event is out of the for-loop, so you need to add the bracket pair to include the codes for adding the click event into the for-loop.重新缩进你的代码后,你可以看到添加点击事件的代码不在for循环中,所以你需要添加括号对来包含添加点击事件到for循环的代码。

When defining for-loop in JavsScirpt without bracket { and } , it only takes the first line in the count.在没有括号{}的 JavsScirpt 中定义 for 循环时,它只占用计数中的第一行。

In addition, there is no onClick event.此外,没有onClick事件。 When using addEventListener , the event name is click , not onClick .使用addEventListener时,事件名称是click ,而不是onClick

var myTable = $('.my-table');
var myArr = [[1,2,3,4,5,6,7], [1,2,3,4,5,6,7]]; //matrix of object values

for (var i = 0; i < myArr.length; i++) {
    myTable.append("<tr id=" + i + ">" +
        " <td>" + myArr[i][0] + "</td>" +
        "<td>" + myArr[i][1] + "</td>" +
        "<td>" + myArr[i][2] + "</td>" +
        "<td>" + myArr[i][3] + "</td>" +
        "<td>" + myArr[i][4] + "</td>" +
        "<td>" + myArr[i][5] + "</td>" +
        "<td>" + myArr[i][6] + "</td>" +
        "<td id=" + "tt" + ">" + myArr[i][7] + "</td>" +
        "</tr>");

        const row = document.getElementById(`${i}`);
        row.addEventListener('click', function(evt) { /// we are using a viewer api property to  isolate the 
            _this.viewer.isolate(_this.modelData.getIds(myArrNames[i][1], item[0]._model.label));
        
        });
}

To use onClick , it will become:要使用onClick ,它将变为:

const row = document.getElementById(`${i}`);
row.onClick = function(evt) { /// we are using a viewer api property to  isolate the 
    _this.viewer.isolate(_this.modelData.getIds(myArrNames[i][1], item[0]._model.label));

});

Lastly, why not just use jQuery only?最后,为什么不只使用 jQuery 呢? The jquey.on can help bind events to dynamically created items, so that the click event will be delegated to any tr element in the table, even if it's added after you bound the event handler. jquey.on可以帮助将事件绑定到动态创建的项目,以便单击事件将委托给表中的任何tr元素,即使它是在您绑定事件处理程序之后添加的。

var myTable = $('.my-table');
var myArr = [[1,2,3,4,5,6,7], [1,2,3,4,5,6,7]]; //matrix of object values

myTable.on('click', 'tr', function() {
    _this.viewer.isolate(_this.modelData.getIds(myArrNames[i][1], item[0]._model.label));
});

for (var i = 0; i < myArr.length; i++) {
    myTable.append("<tr id=" + i + ">" +
        " <td>" + myArr[i][0] + "</td>" +
        "<td>" + myArr[i][1] + "</td>" +
        "<td>" + myArr[i][2] + "</td>" +
        "<td>" + myArr[i][3] + "</td>" +
        "<td>" + myArr[i][4] + "</td>" +
        "<td>" + myArr[i][5] + "</td>" +
        "<td>" + myArr[i][6] + "</td>" +
        "<td id=" + "tt" + ">" + myArr[i][7] + "</td>" +
        "</tr>");
}

ref: https://stackoverflow.com/a/15420578参考: https://stackoverflow.com/a/15420578

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

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