簡體   English   中英

fetchItemByIdentity() 沒有按預期工作

[英]fetchItemByIdentity() doesn't work as expected

我的頁面上有一個ItemFileWriteStore 當我單擊DataGrid中的任何行時,我可以獲得該行的 id,但是當我嘗試使用該 id 執行fetchItemByIdentity時,它總是返回null 知道為什么會這樣嗎?

我正在使用 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();
});

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

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 }
]});

這是關於 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');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM