【问题标题】:How to call a function on click of a checkbox which is a popup on click of d3 element like rect如何在单击复选框时调用函数,该复选框是单击 d3 元素(如 rect)时的弹出窗口
【发布时间】:2019-04-17 09:31:20
【问题描述】:

我有一个 d3 元素的矩形,我想点击那个矩形,应该会弹出一个带有复选框的弹出窗口。单击该复选框时,应调用一个函数。
截至目前,当我在 html 中调用一个函数时,该特定函数没有被调用

mini.append("g").selectAll("miniItems")
            .data(data)
            .enter().append("rect")
            .attr("id", "rect")
            .attr("x", 200)
            .attr("y", function(d,i) {return y2(i*1.2)+5;})
            .attr("width", windowWidth - 700)
            .attr("height", 15)
            .attr("stroke", "grey")
            .on("click", onClick);/*on click of d3 element this function is invoked*/

  function onClick(d){
     let content;
    content = "<div style='background: white;width:70px; box-shadow:3px 3px 5px #797878;height:30px; padding:8%'><input type='checkbox' id='myCheck' (click)='onClaim(d.executionId)'></input>"+ "claim" + "</div>";
     div.html(content)
       .style("left", (d3.event.pageX) + "px") 
        .style("top", (d3.event.pageY) + "px")
        .style("display", "block");
   }

 function onClaim(value){/*this function is not executed*/
        console.log(value);
}

我没有得到任何解决方案来调用该函数
任何帮助表示赞赏。提前谢谢你:)

【问题讨论】:

    标签: javascript html angular d3.js


    【解决方案1】:

    您的代码中的引号存在一些问题。试试这个:

     content = "<div style='background: white;width:70px; box-shadow:3px 3px 5px #797878;height:30px;'>
           <input type='checkbox' onClick='onClaim(d.data);'>
         </div>"
    

    您在 30px; 结尾处关闭了周围的 "。 我用单引号替换了它,并在最后添加了双引号。

    【讨论】:

    • 嗨@sean Leroy,感谢您的回复:) 但实际上在发布带有引号的问题时出现了一些错误......所以现在我对其进行了编辑,但它仍然无法正常工作......你能请指出我犯错的地方
    • 您的控制台中是否有错误,或者它只是没有执行而没有说明原因?
    • 当我将鼠标悬停在特定函数 onClaim 上时,我得到“'onClaim' 已声明但它的值从未被读取'”我收到此消息。所以这意味着首先不会调用特定的函数。我不知道如何调用它以便可以执行它。
    【解决方案2】:

    Hlo dkshaaa,

    你可以试试这个。

    由于您使用的是复选框而不是 onClick,因此您可以使用 onChange。

    所以不是这个

    &lt;input type='checkbox' id='myCheck' (click)='onClaim(d.executionId)'&gt;

    你可以使用

    &lt;input type="checkbox" id="isCheckBox" (change)="onClaim($event, d.executionId)"/&gt;

    在角度组件中,您可以将事件用作

    function onClaim(event, execId) {
        if(event.target.checked){
           //DO SOMETHING
        }
    }
    

    希望这会有所帮助。 ;-)

    【讨论】:

    • 您好 DP,感谢您的回复,但正如我所说,onClaim 函数根本没有被调用。当我将鼠标悬停在该函数上时,它显示为 ''onClaim' 已声明,但其值从未被读取' "...
    • @DeekshaMulgaonkar 我认为您必须为该弹出窗口创建动态组件。我不确定是否有更简单的解决方案。
    【解决方案3】:

    为什么不使用d3 方式添加事件,因为您已经在使用d3

    onClick 函数中,您可以将click 事件绑定到输入#myCheck。

    div.select('input#myCheck').on('click', function () {
        if(this.checked) // checking if the checkbox is checked
            onClaim(d.executionId);
    });
    

    请发布一个可以更容易回答的有效 sn-p(因为您的帖子缺少 divmini 等的定义。无论如何,我在这里使用示例代码创建了一个 sn-p:

    var svg = d3.select('svg#chart');
    var div = d3.select('div.tooltip');
    
    var data = [{id: 1, executionId: 1}, {id: 2, executionId: 2}];
    
    svg.append("g").selectAll("miniItems")
                .data(data)
                .enter().append("rect")
                .attr("id", "rect")
                .attr("x", 100)
                .attr("y", function(d,i) {return (i*100);})
                .attr("width", 100)
                .attr("height", 15)
                .style("stroke", "grey")
                .on("click", onClick);/*on click of d3 element this function is invoked*/
    
      function onClick(d){
         let content;
        content = "<div style='background: white;width:70px; box-shadow:3px 3px 5px #797878;height:30px; padding:8%'><input type='checkbox' id='myCheck'/>"+ "claim" + "</div>";
         div.html(content)
           .style("left", (d3.event.pageX) + "px") 
            .style("top", (d3.event.pageY) + "px")
            .style("display", "block");
         div.select('input#myCheck').on('click', function () {
         	if(this.checked)
    	     	onClaim(d.executionId);
         })
       }
    
     function onClaim(value){/*this function is not executed*/
            console.log(value);
    }
    div.tooltip {
          position: absolute;
    }
    <script src="https://d3js.org/d3.v4.min.js"></script>
    
    <svg id="chart" width="400" height="400"></svg>
    
    <div class="tooltip">
    
    </div>

    希望这会有所帮助。

    【讨论】:

    • 嗨@Shashank 非常感谢您的回复:) 现在可以使用了。唯一的问题是“this.checked”有一些问题(在 IDE 中它带有红色下划线),这不会影响输出,但是 ng build 会因此而失败。我认为这个问题是因为我使用的是 typeScript。悬停在“this.checked”上显示的消息是“'BaseType' 类型上不存在“已检查属性”。“元素”类型上不存在“已检查”属性。检查复选框是否被选中而不是使用上述方法的任何替代方法
    • 很高兴我能帮上忙!我不确定 typescript 是如何转换的,但我找到了 this 并认为它可能会有所帮助:const input = this as HTMLInputElement; if(input.checked) { onClaim(d.executionId); }。它基本上是获取输入元素并检查它的 checked 属性,所以我相信您可以弄清楚如何在 typescript 中获取 HTML 元素的属性。
    猜你喜欢
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 2020-08-24
    • 1970-01-01
    相关资源
    最近更新 更多