2011-04-27 45 views
4

我的頁面上有一個ItemFileWriteStore。當我點擊我的DataGrid中的任意一行時,我可以獲得該行的ID,但是當我嘗試使用該ID執行fetchItemByIdentity時,它始終返回null。任何想法,爲什麼這可能是?fetchItemByIdentity()無法正常工作

我正在使用Dojo 1.5。

function getRemoveFormatter(id) { 
    return '<a href="#" onclick="deleteItem(' + id + ');return false;"><img src="/images/icons/delete.png" /></a>'; 
} 

function deleteItem(id) { 
    console.log(id); 
    window.grid.store.fetchItemByIdentity({ 
    identity: id, 
    onItem: function(item, request) { console.log(item); } 
    }); 
    //window.grid.store.deleteItem(id); 
} 

dojo.ready(function() { 
    var store = new dojo.data.ItemFileWriteStore({data:{items:[]}}); 
    window.grid = new dojox.grid.DataGrid({ 
    store: store, 
    structure: [ 
     { name: "id", field: "id", width: "50px" }, 
     { name: "Stylist", field: "stylist", width: "100px" }, 
     { name: "Service", field: "service", width: "200px" }, 
     { name: "Length", field: "length", width: "50px" }, 
     { name: "Remove", field: "remove", width: "30px", formatter: getRemoveFormatter } 
    ], 
    identifier: "id", 
    label: "id"}); 
    dojo.byId("services_grid").appendChild(grid.domNode); 
    grid.startup(); 
    observeAppointmentServiceAddClick(window.grid); 
    getAppointmentItems(); 
}); 

回答

2

嘗試對聲明商店和網格的方式做一些小改動。標識符和標籤屬性屬於商店旁邊商品的數據部分。

var store = new dojo.data.ItemFileWriteStore({data:{ 
    items:[], 
    identifier: "id", 
    label: "id"}}); 

window.grid = new dojox.grid.DataGrid({ 
store: store, 
structure: [ 
    { name: "id", field: "id", width: "50px" }, 
    { name: "Stylist", field: "stylist", width: "100px" }, 
    { name: "Service", field: "service", width: "200px" }, 
    { name: "Length", field: "length", width: "50px" }, 
    { name: "Remove", field: "remove", width: "30px", formatter: getRemoveFormatter } 
]}); 
+0

這做到了!謝謝!!!!! – 2011-05-03 23:56:14

+0

太棒了!很高興有幫助! – 2011-05-05 08:47:18

1

這是一個簡單的代碼和jsfiddle的答案。

http://jsfiddle.net/martlark/UkKXW/1/

<html> 
    <head><!--dojo stuff--></head> 
    <body> 
     <div id='store'></div> 
     <div id='log'></div> 
    </body> 
</html> 


dojo.require("dojo.data.ItemFileWriteStore"); 
var store = null; 

function getItem(id) { 
store.fetchItemByIdentity({ 
    identity: id, 
    onItem: function (item, request) { 
     var v = store.getValue(item, 'value'); 
     dojo.byId('log').innerHTML = 'getItem(' + id + ') =' + v; 
    } 
}); 
} 

dojo.ready(function() { 
var items = []; 

for (var p = 0; p < 5; p++) { 
    items.push({ 
     id: p, 
     value: 'v ' + p 
    }); 
} 

store = new dojo.data.ItemFileWriteStore({ 
    data: { 
     identifier: 'id', 
     items: items 
    } 
}); 

var gotList = function (items, request) { 
    var itemsList = "<ul>"; 
    dojo.forEach(items, function (i) { 
     itemsList += '<li> id:' + p + ' = ' + store.getValue(i, 
      "value") + "</li>"; 
    }); 
    itemsList += '</ul>'; 
    dojo.byId('store').innerHTML = itemsList; 
} 
var gotError = function (error, request) { 
    alert("The request to the store failed. " + error); 
} 
// Invoke the search 
store.fetch({ 
    onComplete: gotList, 
    onError: gotError 
}); 
getItem('2'); 
});