你必須確保,你的服務器返回有效的JSON。這將是一個平面數組(數字索引),字典樣式的數組作爲項目。這是一個可能的樣本;
<?php
//requestServerData.php
$data = array();
$item_one = array("name" => "state1", "id" => "s1");
$item_two = array("name" => "state2", "id" => "s2");
array_push($item_one);
array_push($item_two);
echo json_encode($data);
?>
與您提供的小提琴將其組合在一起,通過requestServerData
功能模擬XHR,這是怎麼一回事呢:
// global handles for the store and combobox
var combo = null, store = null;
// simulaition of a call to PHP server, which returns a json encoded array
// e.g. "[{name:'state1', id:'s1'},{name:'state2', id:'s2'}]"
function requestServerData() {
var responseText = "[{name:'state1', id:'s1'},{name:'state2', id:'s2'}]";
var data = eval(responseText);
var newStore = new dojo.store.Memory({"data": data});
combo.set('store', newStore);
combo.set('value', data[0].name)
}
// since domReady is in the require, this code block is not run until page loads
require([
"dojo/store/Memory", "dijit/form/ComboBox", "dojo/domReady!"
], function(Memory, ComboBox){
store = new Memory({
data: [
{name:"Loading", id:"LOAD"}
]
});
combo = new ComboBox({
id: "stateSelect",
name: "state",
value: "Loading",
store: store,
searchAttr: "name"
}, "stateSelect");
combo.startup();
requestServerData()
});
你的小提琴,更新:https://jsfiddle.net/fuanzcps/3/
提供源,因此我們可以幫助你 –
@bRIMOsBor請看這裏https://jsfiddle.net/fuanzcps/ – Marcel
您需要創建data.php其返回包含您的數據的JSON數組, 然後進行一個Ajax調用ŧ Ødata.php並創造新的'存儲器({數據:responseServer})',給PHP代碼已經看清楚你的代碼中 –