2016-02-17 40 views
0

我想實現我的網站道場組合框。但我沒有成功。使用Dojo在PHP文件

總之,我得到PHP中的數組,我想這陣要在下拉列表可用,但他們應該能夠輸入他們想要的任何東西,因此,組合框。

我曾試圖按照下面的鏈接進行操作。但這讓我比開始時更糟糕。 任何人都可以提供一個簡單有效的方式來實現它嗎?

鏈接:

https://dojotoolkit.org/reference-guide/1.10/dijit/form/ComboBox.html#examples

How to implement Dojo autocomplete similar to jQuery UI autocomplete?

+0

提供源,因此我們可以幫助你 –

+0

@bRIMOsBor請看這裏https://jsfiddle.net/fuanzcps/ – Marcel

+1

您需要創建data.php其返回包含您的數據的JSON數組, 然後進行一個Ajax調用ŧ Ødata.php並創造新的'存儲器({數據:responseServer})',給PHP代碼已經看清楚你的代碼中 –

回答

0

你必須確保,你的服務器返回有效的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/