我正在为引导程序使用燃料树插件。如果数据是硬编码的(如下),树看起来是正确的
var treeDataSource = new DataSource({
data: [
{ name: 'Test Folder 1', type: 'folder', additionalParameters: { id: 'F1' },
data: [
{ name: 'Test Sub Folder 1', type: 'folder', additionalParameters: { id: 'FF1' } },
{ name: 'Test Sub Folder 2', type: 'folder', additionalParameters: { id: 'FF2' } },
{ name: 'Test Item 2 in Folder 1', type: 'item', additionalParameters: { id: 'FI2' } }
]
},
{ name: 'Test Folder 2', type: 'folder', additionalParameters: { id: 'F2' } },
{ name: 'Test Item 1', type: 'item', additionalParameters: { id: 'I1' } },
{ name: 'Test Item 2', type: 'item', additionalParameters: { id: 'I2' } }
],
delay: 400
});
请帮助我了解如何通过调用服务(ajax 调用)动态设置数据源。我有一个返回 json 字符串的服务。
var treeDataSource = new DataSource({
data: //how to pull data from service call
});
下面是完整的代码片段
$(document).ready(
function ()
{
var DataSource = function (options) {
this._data = options.data;
};
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;
}
}
var treeDataSource = new DataSource({
data: //how to pull data from service call and assign (how do i call a service here)
});
$('#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) {
if(data.moduleId != 0)
{
SetModuleInfoInSession(data.moduleId,data.moduleName,data.url);
}
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);
});
});