iRunner
iRunner

Reputation: 1532

Saving Record in grid after inserting in Ext-js

I have grid with store and i add the record to the store and data being added to the store gets reflected on the grid.but after refresh it vanishes.

here is my code for inserting the record

handler: function() {

    grid.getStore().insert(
        0,
        new ds_model({
            id:0,
            name:'',
            dob:''
        })
    );

    grid.startEditing(0,0);
    grid.store.commitChanges();

    } 
}) 

EDIT:

var store = new Ext.data.Store({
    data: [
        [ 11849,'ABC','29/03/90'],
        [ 11456,'CDE','17/03/90'],
        [ 11344,'EFG','17/07/90'],
        [ 11343,'IJK','Trainee','02/06/90'],
...

Upvotes: 3

Views: 2777

Answers (2)

Ryan
Ryan

Reputation: 1557

As per your comments in the initial question, you defined your store as follows:

    new Ext.data.Store({
        data: [
            [ 11849,'ABC','29/03/90'],
            [ 11456,'CDE','17/03/90'],
            [ 11344,'EFG','17/07/90'],
            [ 11343,'IJK','Trainee','02/06/90']
        ]
    });

This will not set up any automatic persisting to any backend server. Also, you are hard-coding data into the store, which means that when you reload, you are literally putting that data in the store every time. To make it easy to save and load to/from backend, you just need to modify it as such:

    new Ext.data.Store({
        autoSync: true,  // this will tell Ext to persist your changes
                         // to the backend when you change an entry in the store
        autoLoad: true,  // this will tell Ext to load the store with data from
                         // the backend
        url: // put your url to save
    });

Also note that you will need a url that will expect a call from this. This will most likely be a php file. Based on what you are inserting in the grid, you will need something like this in a php file:

myFile.php
==========

    <?php
        $id = $_POST['id'];  // get id from client
        $name = $_POST['name'];  // get name from client
        $dob = $_POST['dob'];  // get date of birth from client

        // do any saving of this data, e.g, to your database here
    ?>

Also, you'll need a php file that will do the loading of the store.

Read this and this for more clarification.

Upvotes: 1

sra
sra

Reputation: 23975

You need to end your editing also otherwise there are no changes the store can commit. use firebug to check, that your store is saving. You also may use autosave: TRUE to spare the commit

Upvotes: 1

Related Questions