【问题标题】:Event listener isn't working with the custom cell renderer in ag-grid Angular事件侦听器不适用于 ag-grid Angular 中的自定义单元格渲染器
【发布时间】:2019-09-20 04:23:35
【问题描述】:

我已经设置了一个简单的列定义:

{
          headerName: "Probabilité",
          headerToolName: "Consultez les échelles",
          field: "pbt",
          editable: true,
          cellRenderer: params => {
            return `
            <hr>
            <select class="form-control"  (change)="UpdateRisqueBrut($event.target);"
            >
                <br>
                <option>1- Très improbable</option>
                <option>2- Peu probable</option>
                <option>3- Possible</option>
                <option>4- Probable</option>
  </select>
  <hr>
            `;
          }
        } 

以这种方式呈现:

您可能已经注意到,我在自定义单元格渲染中设置了一个侦听器,称为记录在打字稿列表中选择的选项。

更新RisqueBrut

这是监听器的定义:

 public UpdateRisqueBrut(risque) {
    console.log(risque.value);
  }

选择该选项后,我没有收到任何错误,但控制台中没有任何显示。
我做错什么了吗?

【问题讨论】:

    标签: ag-grid ag-grid-ng2 ag-grid-angular


    【解决方案1】:

    也可以通过AgGrid实现'agSelectCellEditor / agPopupSelectCellEditor'

    headerName: 'Probabilité',
    field: 'make',
    editable: true,
    cellEditor:'agSelectCellEditor',
    cellEditorParams: {
        values: ['1- Très improbable','2- Peu probable','3- Possible','4- Probable']
    }
    

    On Grid 选项监听单元格值的变化

    onCellValueChanged: function(event) {
            console.log('onCellValueChanged: Probabilité' + ' = ' + event.newValue);
        },
    

    我还能够通过以下更改实现您的代码

    {headerName: "Probabilité",
         editable: true, 
         cellRenderer: params => {
                  return `
                  <hr>
                  <select onchange="myFunction(this)">
                      <option value='1- Très improbable'>1- Très improbable</option>
                      <option value='2- Peu probable'>2- Peu probable</option>
                      <option value='3- Possible'>3- Possible</option>
                      <option value='4- Probable'>4- Probable</option>
                  </select>
                  <hr>`;
                }
          }
    
    function myFunction(t)
    {
      console.log(t.value);
    }
    

    工作演示链接是here

    希望这会有所帮助。

    【讨论】:

    • 首先,由于我正在使用 Angular,我必须用 () 包装 onchange,以便侦听器变成这样:(onchange)="myFunction(this)"。在其他地方,Angular 给出了这个错误:Uncaught ReferenceError: myFunction is not defined。 (如果有人愿意解释,我觉得这有点奇怪,欢迎您)
    • 第二,即使我放了括号并且没有出错,我还是回到了第一格。正如我在帖子中所说,我没有收到任何错误,但控制台上没有显示任何内容:(。(我真的不明白为什么即使它在 Plunker 上也能正常工作!)。谢谢你的帮助,无论如何!
    • 你能解释一下为什么你在监听器中使用这个而不是 $event.target
    • 即使我尝试 console.log('RandomString') 这也不起作用。似乎听众 onchange 甚至没有启动可以这么说
    • 如果我将相同的代码放在 html 文件中,它可以工作!所以我认为这与 ag-grid 有关。我想我会在 SO 中发布另一个问题来进一步解释这一点。
    猜你喜欢
    • 1970-01-01
    • 2020-03-22
    • 2021-09-13
    • 2017-10-25
    • 2020-06-11
    • 2018-12-02
    • 2019-01-13
    • 2015-01-05
    • 2018-02-13
    相关资源
    最近更新 更多