gpt4 book ai didi

javascript - 如何将树数据加载到 ExtJS 树面板中?

转载 作者:行者123 更新时间:2023-11-28 01:36:24 25 4
gpt4 key购买 nike

我有一个 ExtJS 4.2 TreePanel,但是当我调用 PHP 代码来获取数据时,数据未加载到树中。

数据调用成功,我收到响应,但树数据未加载到树中。

function onGetData(data) {
alert('onGetData');
alert(data);
};
Ext.onReady(function() {
Ext.create('Ext.tree.Panel', {
title: 'Tree Refresh Example',
width: 300,
height: 350,
listeners: {
afterRender: function() {
this.store.getData(onGetData, this);
}
},
tbar: ['->', {
xtype: 'button',
text: 'Add Banana',
value: 1,
margin: '0 30 0 0',
listeners: {
click: function(comp) {
sendData(comp.value);
}
}
}, {
xtype: 'button',
text: 'Add Cabbage',
value: 2,
margin: '0 30 0 0',
listeners: {
click: function(comp) {
sendData(comp.value);
}
}
}, {
xtype: 'button',
text: 'Add Yogurt',
value: 3,
listeners: {
click: function(comp) {
sendData(comp.value);
}
}
}, '->'],
renderTo: 'content',
store: new SampleTreeData()
});
});



Ext.define('SampleTreeData', {
extend: 'Ext.data.TreeStore',
proxy: {
type: 'ajax',
url : '',
reader: {
type: 'json',
root: 'root'
}
},
getData: function(callBack, scope) {
alert('getData');
var store = this;
store.proxy.url = 'data.php?mode=getData';
scope.collapseAll();
store.load({
scope : scope,
callback : function(records, operation, success) {
if (Ext.isFunction(callBack)) {
callBack(store, scope, records, success);
}
}
});
}
});


<?php
$data = 'root: {' .
'text: "Tree Root",' .
'expanded: true,' .
'children: [' .
'{' .
'text: "Fruits",' .
'children: [' .
'{ leaf:true, text: "apple" },' .
'{ leaf:true, text: "orange" }' .
']' .
'},' .
'{' .
'text: "Vegetables",' .
'expanded: false,' .
'children: [' .
'{ leaf:true, text: "carrot" },' .
'{ leaf:true, text: "beet" }' .
']' .
'},' .
'{' .
'text: "Dairy",' .
'children: [' .
'{ leaf:true, text: "milk" },' .
'{ leaf:true, text: "cheese" }' .
']' .
'}' .
']' .
'}';
$logref = fopen('data.log', 'w');
fwrite($logref, "Entered script.\n");


if(isset($_POST['data'])){
fwrite($logref, "sendData\n");
return 'sendDataSuccess';
}else {
fwrite($logref, "getData\n");
echo json_encode(utf8_encode($data));
}
fclose($logref);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../../../ext-4.2.2.1144/resources/css/ext-all.css" />
<script type="text/javascript" src="../../../../ext-4.2.2.1144/ext-all-debug.js"></script>
<script type="text/javascript" src="refresh1.js"></script>
<script type="text/javascript" src="SampleTreeData.js"></script>
</head>
<body style="width: 100%; height: 100%; margin: 0px; padding: 0px; background-color: #89E5BD;">
<div id="content" style="margin: auto; width: 500px; height: 500px;"/>
</body>

最佳答案

已解决

通过更改我的 PHP 并在 TreePanel 上将 rootVisible 设置为 false 使其正常工作。

我还对树存储做了一些更改。

Ext.define('SampleTreeData', {
extend: 'Ext.data.TreeStore',
autoLoad: false,
autoSync: false,
proxy: {
type: 'ajax',
url : '',
reader: {
type: 'json',
root: ''
}
},
root: {
expanded: true
},
getData: function(callBack, scope) {
var store = this;
store.proxy.url = 'data.php?mode=getData';
store.load({
scope : scope,
callback : function(records, operation, success) {
if (Ext.isFunction(callBack)) {
callBack(store, scope, records, success);
}
}
});
}
});

<?php
$data = '[{' .
'text: "Tree Root",' .
'expanded: true,' .
'children: [' .
'{' .
'text: "Fruits",' .
'children: [' .
'{ leaf:true, text: "apple" },' .
'{ leaf:true, text: "orange" }' .
']' .
'},' .
'{' .
'text: "Vegetables",' .
'expanded: false,' .
'children: [' .
'{ leaf:true, text: "carrot" },' .
'{ leaf:true, text: "beet" }' .
']' .
'},' .
'{' .
'text: "Dairy",' .
'children: [' .
'{ leaf:true, text: "milk" },' .
'{ leaf:true, text: "cheese" }' .
']' .
'}' .
']' .
'}]';
$logref = fopen('data.log', 'w');
fwrite($logref, "Entered script.\n");


if(isset($_POST['data'])){
fwrite($logref, "sendData\n");
return 'sendDataSuccess';
}else {
fwrite($logref, "getData\n");
echo $data;
}
fclose($logref);
?>

关于javascript - 如何将树数据加载到 ExtJS 树面板中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21489950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com