maca88 / datatables.plugins

MIT License
21 stars 5 forks source link

Extract save state info from your plugins? #12

Open marianopeck opened 10 years ago

marianopeck commented 10 years ago

Hi Maca,

You plugins....are soooo cool that my users are not happy for "memorize" their changes only during their web sessions. I saw something about your RemoteStateSave, but I may want kind of implement that in my own app (because of some particular requirements like preferences etc). So...it would love so much if there could be a general API/function/extension whatsover, that allow me to ask to the table I have rendered the following info (or for each column, the following info):

Basically...what I wonder is if I could get a list with that info, so that I can have all the info to persist user changes in my app/database and then re-use them later on and get back the "changes" of the user.

Would that be possible?

maca88 commented 10 years ago

Basically RemoteState is a general plugin for saving state to a remote location. Here is an example:

Table initialization

$('example').DataTables(
{
    remoteState: {
        storeId: 'OrderGrid',
        getStatesFromServer: true,
        ajax: {
            'getAll': {
                url: 'url/getAll'
            },
            'save': {
                url: 'url/save'
            },
            'delete': {
                url: 'url/delete'
            },
            'setDefault': {
                url: 'url/setDefault'
            }
        }
    },
    dom: 'lfrBtip'
});

With the above example the RemoteState plugin will create a request to the getAll url ('url/getAll') for getting the state data. The following data will be sent:

{
    action: 'getAll',
    data: null,
    stateName: null,
    storeId: 'OrderGrid'
}

The response should contain something like this:

{
    defaultState: 'Default',
    states: {
        state1: {...}
        state2: {...}
    }
}

When saving the current state the plugin will create a request to the save url ('url/save') The following data will be sent:

{
    action: 'save',
    data: {...},
    stateName: 'state1',
    storeId: 'OrderGrid'
}

When deleting a state let say 'state1', the plugin will create a request to the delete url ('url/delete') The following data will be sent:

{
    action: 'delete',
    data: null,
    stateName: 'state1',
    storeId: 'OrderGrid'
}

When setting the default state let say 'state1', the plugin will create a request to the setDefault url ('url/setDefault') The following data will be sent:

{
    action: 'setDefault',
    data: null,
    stateName: 'state1',
    storeId: 'OrderGrid'
}

On save/delete/setDefault requests you have to save/delete/edit the data on your server. How you will handle that is up to you. For example you can store the states data in a file and the file content should look like this:

{
    OrderGrid: {
        defaultState: 'Default',
        states: {
            state1: {...}
            state2: {...}
        }
    },
    CustomerGrid: {
        defaultState: 'state1',
        states: {
            state1: {...}
            state2: {...}
        }
    },
}

Note: Each table should have a different storeId as it is the key for getting the right table states.

For a live example you can look HERE

maca88 commented 10 years ago

If you dont want to use the RemoteState plugin, you have to use load/save callback provided by DataTables.

marianopeck commented 10 years ago

Hi Maca, quick question...the request that would be generated, is an ajax call or a HTTP request?

maca88 commented 10 years ago

Ajax request.