简体   繁体   English

SlickGrid按钮单击处理程序在滚动事件时停止运行?

[英]SlickGrid button click handler stops functioning on scroll event?

In attempting to use SlickGrid in my page, I have noticed that a jQuery click handler I declare after the grid is rendered only works for approximately 2x the number of rows visible. 在尝试在我的页面中使用SlickGrid时,我注意到我在渲染网格后声明的jQuery单击处理程序仅适用于可见行数的2 SlickGrid

Moreover, when a button is clicked the value property is changed to provide visual confirmation to the user. 此外,当单击按钮时, value属性会更改以向用户提供视觉确认。 However, upon scrolling too far down past one of the previously clicked buttons, scrolling back up appears to reset the value to the original/default value. 但是,在向下滚动超过先前单击的按钮之一时,将显示向上滚动以将value重置为原始/默认值。

As far as I can tell, it looks like the grid is re-rendering at various intervals. 据我所知,看起来网格正在以不同的间隔重新渲染。 If that suspicion is correct, how do I preserve the effect of the click handler and the values in those buttons already clicked? 如果怀疑是正确的,我如何保留单击处理程序的效果以及那些已经单击的按钮中的值?

It may be important to note that I am not using dataView. 需要注意的是,我没有使用dataView。

<script type="text/javascript">
  var j$ = jQuery.noConflict();
  var grid,
      data = [],
      columns = [
          { id: "rownum", name: "Row #", field: "rownum", width: 50 },
          { id: "accname", name: "Account Name", field: "accname", width: 500 },
          { id: "accid", name: "Id", field: "accid", width: 150 },
          { id: "rectypeid", name: "Record Type ID", field: "rectypeid", width: 150 },
          { id: "itemurl", name: "Item URL Link", field: "itemurl", width: 300, formatter: function (r,c,v,def,datactx) { return "<a target='_blank' href='" + v + "'>" + v + "</a>" } },
          { id: "rfrbtn", name: "Notify Staff", field: "rfrbtn", width: 75, formatter: function (r,c,v,def,datactx) { return "<input class='rfrch' value='Yes' type='button' id='" + datactx.accid + "'/>" } }
      ],
      options = {
        enableCellNavigation: false,
        enableColumnReorder: true,
      };

  var acc = new SObjectModel.Acc();

  // Query Accounts
  var count = 1;
  acc.retrieve({ where: {Name: {like: '%Test Acct%'}, RecordTypeId: {eq: '01230000003MJmr'}}, limit: 100 }, function(err, records, event){
    if(err) {
      alert(err.message);
    }
    else {
      records.forEach(function(record) {
        data.push({
          rownum: count++,
          accname: record.get("Name"),
          accid: record.get("Id"),
          rectypeid: record.get("RecordTypeId"),
          itemurl: record.get("itemurl")
        });
      });
    }
    grid.invalidate();
    rfrClickHandler();
  });

  grid = new Slick.Grid("#container", data, columns, options);

  var rfrClickHandler = function() {
    j$(".rfrch").click(function() {
      var rfrbtnClicked = j$(this).prop('id');
      j$(this).prop('value','Cancel');
      console.log(rfrbtnClicked);
    });
  }

</script>

Use .on() so that you do not have to re-bind the click handlers after the rows are re-rendered. 使用.on(),以便在重新渲染行后不必重新绑定单击处理程序。

j$("#container").on('click', '.rfrch', function(){
    //... your handler code
}

In your formatter for the button you can add a property to dataContext for the current value state of the button and also a data property to hold the row index: 在按钮的格式化程序中,您可以为按钮的当前值状态向dataContext添加一个属性,还可以为保存行索引的data属性添加一个属性:

if(datactx.rfrchState === undefined){
    datactx.rfrchState = "Yes";
}
return "<input class='rfrch' value='"+datactx.rfrchState+"' data-row-index='"+r+"' type='button' id='" + datactx.accid + "'/>"

Then in your button handler add code to retrieve the data object and set rfrchState. 然后在按钮处理程序中添加代码以检索数据对象并设置rfrchState。 That way when you scroll up and down and the rows are re-rendered the state will be preserved and set on the button via the formatter. 这样,当您上下滚动并重新渲染行时,状态将被保留并通过格式化程序设置在按钮上。

var rowIndex = j$(this).data('row-index');
var dataItem = grid.getDataItem(rowIndex);
dataItem.rfrchState = dataItem.rfrchState === 'Yes'?'No':'Yes';

Hope this helps. 希望这可以帮助。

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

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