Results 1 to 8 of 8

Thread: update store (changed json)

  1. #1
    Sencha User
    Join Date
    Nov 2014
    Posts
    21
    Answers
    2

    Default Answered: update store (changed json)

    Hello,

    I have a localstore (SQLite) with this structure:
    itemID | description | amount

    Now I want to change in one row the amount of the item. How can I do this?

    I tried:
    Code:
    var selectedItem = Ext.getCmp("item-result-view").selected.items[0];
    var store = Ext.getStore("store");
    
    
    for(var index = 0; index < store.data.length; index++){
    
    
            var dataStore = store.data.all[index].data;   // the structure is a little complicated
    
    
            if(dataStore.itemID === selectedItem.data.itemID){    
    
    
                var oldAmount = dataStore.amount;
                var newAmount = dataStore.amount - Ext.getCmp("item-amount-field").getValue();
    
    
                // does not work:
               // store.remove(dataStore.findRecord('itemID', oldAmount));
               // store.add(dataStore.findRecord('itemID', newAmount));
               // store.sync();
               
               // does also not work:
                dataStore.amount = newAmount;  // here it only changes the json but then not the database
                Ext.getStore('store').sync();
    
    
                // does also not work:
             //     store.set(dataStore.amount, newAmount);
    
    
                // jump out of loop
                break;
    
    
    
    
            }
        }
    I hope someone has another idea how to change the amount in the local database....

  2. Quote Originally Posted by kaddah View Post
    oh sorry, the structure of the store I get is something like that:

    Code:
    store 
         -data
              -all: Array[20]
                       0
                           -id
                          -data
                                -amount        // that is the item I want to change
                                -test
                          -anotherdata
    
                       1
                          -id
                          -data
                                -amount        // or that
                                -test
                          -anotherdata
               
                       2
                          -id
                          -data
                                -amount
                                -test
                          -anotherdata
    ......
         -bla
         -blabla
    ......

    The


    item.set('amount', newAmount);
    does not work
    Ok, then try this. Think this should work.
    Code:
    store.each(
        function(item, index, length) {
    
    
    
    
            if (item.get('itemID') === selectedItem.data.itemID) {
    
    
                var oldDataEntry = item.get('data');
                var modDataEntry = oldDataEntry;
    
    
                var oldAmount = oldDataEntry.amount;
                var newAmount = oldAmount - Ext.getCmp("item-amount-field").getValue();
    
    
                modDataEntry.amount = newAmount
    
    
                item.set('data', modDataEntry);
    
    
            }
        });
    
    
    store.sync();

  3. #2
    Sencha Premium Member u25771's Avatar
    Join Date
    Sep 2012
    Location
    Switzerland :)
    Posts
    253
    Answers
    27

    Default

    I have really no idea what "structure" you're dealing with but this should do it almost...

    Code:
    stores.each(
        function(item, index, length) {
    
    
            if (item.get('itemID') === selectedItem.data.itemID) {
    
    
                var oldAmount = item.get('amount');
                var newAmount = item.get('amount'); - Ext.getCmp("item-amount-field").getValue();
    
    
    
    
                item.set('amount', newAmount);
    
    
            }
        });
    
    
    
    
    store.sync();
    Mark thread as answered if your problem could be solved and leave a vote for helpful answers

    For more help check out my new blog: http://abitofcoding.blogspot.com

    T
    o get in contact with me just send me a message on Google+

  4. #3
    Sencha User
    Join Date
    Nov 2014
    Posts
    21
    Answers
    2

    Default

    oh sorry, the structure of the store I get is something like that:

    Code:
    store 
         -data
              -all: Array[20]
                       0
                           -id
                          -data
                                -amount        // that is the item I want to change
                                -test
                          -anotherdata
    
                       1
                          -id
                          -data
                                -amount        // or that
                                -test
                          -anotherdata
               
                       2
                          -id
                          -data
                                -amount
                                -test
                          -anotherdata
    ......
         -bla
         -blabla
    ......

    The


    item.set('amount', newAmount);
    does not work

  5. #4
    Sencha Premium Member u25771's Avatar
    Join Date
    Sep 2012
    Location
    Switzerland :)
    Posts
    253
    Answers
    27

    Default

    Quote Originally Posted by kaddah View Post
    oh sorry, the structure of the store I get is something like that:

    Code:
    store 
         -data
              -all: Array[20]
                       0
                           -id
                          -data
                                -amount        // that is the item I want to change
                                -test
                          -anotherdata
    
                       1
                          -id
                          -data
                                -amount        // or that
                                -test
                          -anotherdata
               
                       2
                          -id
                          -data
                                -amount
                                -test
                          -anotherdata
    ......
         -bla
         -blabla
    ......

    The


    item.set('amount', newAmount);
    does not work
    Ok, then try this. Think this should work.
    Code:
    store.each(
        function(item, index, length) {
    
    
    
    
            if (item.get('itemID') === selectedItem.data.itemID) {
    
    
                var oldDataEntry = item.get('data');
                var modDataEntry = oldDataEntry;
    
    
                var oldAmount = oldDataEntry.amount;
                var newAmount = oldAmount - Ext.getCmp("item-amount-field").getValue();
    
    
                modDataEntry.amount = newAmount
    
    
                item.set('data', modDataEntry);
    
    
            }
        });
    
    
    store.sync();
    Mark thread as answered if your problem could be solved and leave a vote for helpful answers

    For more help check out my new blog: http://abitofcoding.blogspot.com

    T
    o get in contact with me just send me a message on Google+

  6. #5
    Sencha User
    Join Date
    Nov 2014
    Posts
    21
    Answers
    2

    Default

    thank you. Everything is working fine.

    Last Problem: the store isn't syncing... The item.set('data', modDataEntry); sets the new value in the storecopy. but at store.sync(); nothing happens. No error So the new value isn't set in the store in the end...

  7. #6
    Sencha Premium Member u25771's Avatar
    Join Date
    Sep 2012
    Location
    Switzerland :)
    Posts
    253
    Answers
    27

    Default

    Quote Originally Posted by kaddah View Post
    thank you. Everything is working fine.

    Last Problem: the store isn't syncing... The item.set('data', modDataEntry); sets the new value in the storecopy. but at store.sync(); nothing happens. No error So the new value isn't set in the store in the end...
    In order to apply changes to a store the dirty property needs to be true. Normally the set() function does this automatically. The property, when it's false set in manually true on the record --> LINK
    Mark thread as answered if your problem could be solved and leave a vote for helpful answers

    For more help check out my new blog: http://abitofcoding.blogspot.com

    T
    o get in contact with me just send me a message on Google+

  8. #7
    Sencha User
    Join Date
    Nov 2014
    Posts
    21
    Answers
    2

    Default

    Works, thank you

  9. #8
    Sencha Premium Member u25771's Avatar
    Join Date
    Sep 2012
    Location
    Switzerland :)
    Posts
    253
    Answers
    27

    Default

    Quote Originally Posted by kaddah View Post
    Works, thank you

    you're welcome
    Mark thread as answered if your problem could be solved and leave a vote for helpful answers

    For more help check out my new blog: http://abitofcoding.blogspot.com

    T
    o get in contact with me just send me a message on Google+

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •