dashcode:如何在列表中添加/删除项目?列表不会直观刷新

时间:2012-10-07 21:05:19

标签: jquery dashcode

我正在制作一些小部件。它不使用任何Web服务作为数据源。我使用来自普通网页的jQuery解析一些数据,并希望将此数据显示为破折号列表。我按照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);
    };
}
};

在属性inspenctor中,我选择了动态数据类型,并将此listDataSource选为数据源。 然后,我以这种方式向listDataSource的_rowData添加一个项目:

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

以这种方式重新加载我的列表:

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

我知道,函数numberOfRows和prepareRow在我添加新项目后被调用(通过警报消息)。警报打印_rowData,并按预期添加新项目。 但我的小部件中的列表不刷新!为什么?我真的无法弄明白。是否有一些函数reloadUI()或什么? 如果你现在解决我的问题,请把我带到路上。谢谢!

1 个答案:

答案 0 :(得分:0)

我自己发现了一个丑陋的黑客。 如果调用reloadData()两次 - UI中的列表按预期刷新。

另一个解决方案是使用global 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);
};
}
};

然后

myData.push("Item 4");

并将新数据数组设置为list,然后重新加载:

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

但此解决方案禁用了rowElement上的回调函数

alert("Row "+rowIndex);
设置新的dataArray后

不起作用,所以我选择double reloadData hack:

var mylist = document.getElementById("list").object;    
mylist.reloadData();
mylist.reloadData();
相关问题