我有这个代码:
<script>
$(window).load(function() {
var DataSource = function (options) {
this._formatter = options.formatter;
this._columns = options.columns;
this._data = options.data;
};
DataSource.prototype = {
columns: function () {
return this._columns;
},
data: function (options, callback) {
var self = this;
if (options.search) {
callback({ data: self._data, start: start, end: end, count: count, pages: pages, page: page });
} else if (options.data) {
callback({ data: options.data, start: 0, end: 0, count: 0, pages: 0, page: 0 });
} else {
callback({ data: self._data, start: 0, end: 0, count: 0, pages: 0, page: 0 });
}
}
};
var treeDataSource = new DataSource({
data: [
{ name: 'Folder 1', type: 'folder', additionalParameters: { id: 'F1' },
data: [
{ name: 'Sub Folder 1', type: 'folder', additionalParameters: { id: 'FF1' } },
{ name: 'Sub Folder 2', data: [
{name: 'sub sub folder 1', type: 'folder', additionalParameters: { id: 'FF21' }},
{name: 'sub sub item', type: 'item', additionalParameters: { id: 'FI2' }}
], type: 'folder', additionalParameters: { id: 'FF2' }},
{ name: 'Item 2 in Folder 1', type: 'item', additionalParameters: { id: 'FI2' } }
]
},
{ name: 'Folder 2', type: 'folder', additionalParameters: { id: 'F2' } },
{ name: 'Item 1', type: 'item', additionalParameters: { id: 'I1' } },
{ name: 'Item 2', type: 'item', additionalParameters: { id: 'I2' } }
],
delay: 400
});
$('#MyTree').tree({dataSource: treeDataSource});
$('#tree-selected-items').on('click', function () {
console.log("selected items: ", $('#MyTree').tree('selectedItems'));
});
$('#MyTree').on('loaded', function (evt, data) {
console.log('tree content loaded');
});
$('#MyTree').on('opened', function (evt, data) {
console.log('sub-folder opened: ', data);
});
$('#MyTree').on('closed', function (evt, data) {
console.log('sub-folder closed: ', data);
});
$('#MyTree').on('selected', function (evt, data) {
console.log('item selected: ', data);
});
});
</script>
我正在使用http://exacttarget.github.io/fuelux/#tree 问题是当我打开一个文件夹时,它一直包含其他文件夹,如示例中的。就像它永远不会结束一样。怎么能避免呢?
谢谢。
编辑
我做了这个并且它的工作..你认为它好吗?
var cont = 0;
DataSource.prototype = {
columns: function () {
return this._columns;
},
data: function (options, callback) {
var self = this;
if (options.search) {
callback({ data: 0 , start: 0, end: 0, count: 0, pages: 0, page: 0 });
} else if (options.data) {
callback({ data: options.data, start: 0, end: 0, count: 0, pages: 0, page: 0 });
} else if (cont == 0) {
callback({ data: self._data, start: 0, end: 0, count: 0, pages: 0, page: 0 });
}
else {
callback({ data: 0, start: 0, end: 0, count: 0, pages: 0, page: 0 });
}
cont = cont+1;
}
};
使用 cont 我第一次回调数据(加载树时)。之后它只返回 0。