简体   繁体   English

dashcode:如何添加/删除列表中的项目? 列表无法直观刷新

[英]dashcode: how to add/remove items in list? List does not refresh visually

I'm working on some widget. 我正在处理一些小部件。 It doesn't use any web service for data source. 它不使用任何Web服务作为数据源。 I parse some data using jQuery from a normal web page, and want to display this data as a dashcode list. 我使用普通网页上的jQuery解析一些数据,并希望将这些数据显示为破折号列表。 I followed the listDataSource sample code in Dashcode. 我遵循了Dashcode中的listDataSource示例代码。

var listDataSource = {
  _rowData: ['Item 1', 'Item 2', 'Item 3'],
  numberOfRows: function() {
      alert("Row data now is: "+this._rowData);
      alert("number of rows: "+this._rowData.length);
      return this._rowData.length;
},
prepareRow: function(rowElement, rowIndex, templateElements) {
    if (templateElements.rowLabel) {
        templateElements.rowLabel.innerText = this._rowData[rowIndex];
    }
    rowElement.ondblclick = function(event) {
        // Do something interesting
        alert("Row "+rowIndex);
    };
}
};

In attributes inspenctor I selected Dynamic Data Type and chosen this listDataSource as Data Source. 在属性检查器中,我选择了“动态数据类型”,然后选择此listDataSource作为数据源。 Then, I add one more item to listDataSource's _rowData this way: 然后,我通过以下方式将另一个项目添加到listDataSource的_rowData中:

listDataSource._rowData.push("New Item 4");

and reload my list this way: 并以这种方式重新加载我的列表:

var mylist = document.getElementById("list").object;    
mylist.reloadData();

I know, that functions numberOfRows and prepareRow gets called after I add new item, (by alert messages). 我知道,在我添加新项之后(通过警报消息)将调用numberOfRows和prepareRow函数。 Alert prints _rowData with new item added as expected. 警报将打印_rowData并按预期添加新项目。 But a list in my widget do not refresh! 但是我的窗口小部件中的列表不会刷新! Why? 为什么? I really can't figure it out. 我真的不知道。 Is there some function reloadUI() or something? 是否有一些功能reloadUI()或其他? Please, put me on the way if you now the solution for my problem. 请,如果您现在解决我的问题,请把我放在路上。 Thanks! 谢谢!

Found an ugly hack by myself. 自己发现了一个丑陋的骇客。 if reloadData() is called twice - list in UI refreshes as expected. 如果两次调用reloadData()-UI中的列表将按预期刷新。

Another solution is to use global var and set it just before reloadData() 另一种解决方案是使用全局var并将其设置在reloadData()之前

var myData['Item 1', 'Item 2', 'Item 3'];

var listDataSource = {
  _rowData: myData,
  numberOfRows: function() {
  alert("Row data now is: "+this._rowData);
  alert("number of rows: "+this._rowData.length);
  return this._rowData.length;
},
prepareRow: function(rowElement, rowIndex, templateElements) {
if (templateElements.rowLabel) {
    templateElements.rowLabel.innerText = this._rowData[rowIndex];
}
rowElement.ondblclick = function(event) {
    // Do something interesting
    alert("Row "+rowIndex);
};
}
};

then 然后

myData.push("Item 4");

and set new data array to list and then reload it: 并设置新的数据数组以列出并重新加载它:

var mylist = document.getElementById("list").object;    
mylist.setDataArray(myData);
mylist.reloadData();

but this solution disables callback function on rowElement 但是此解决方案禁用rowElement上的回调函数

alert("Row "+rowIndex);

doesn't work after setting new dataArray, so I choose double reloadData hack: 设置新的dataArray后无法正常工作,因此我选择double reloadData hack:

var mylist = document.getElementById("list").object;    
mylist.reloadData();
mylist.reloadData();

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

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