Caching Options data.

Caching Options data.

MarkAndersonUKMarkAndersonUK Posts: 44Questions: 17Answers: 0

Hi,

I have a Parent / Child setup, when we change the parent record, we have the child perform a 'mytable.ajax.reload();'
this works fine, but it is slow due to the child datatable having a number of options data that it also reloads. The options data is stored in separate mySQL tables and does not change, it is data used to populate SELECT dropdown, and convert ID values to human meaningful names etc. Without all the options data, the reload takes 340ms, with the options data it takes 3882ms.

Is there anyway to cache the options data so it doesn't reload when simply selecting a new record ? Or is there a way to keep the existing options data and only load the data parts of the table.

I am running using NodeJS, mySQL, ServerSide, and the latest versions of Datatables and Editor.

I know I could either hardcode the options values but I would prefer to keep loading them from the tables, as they
change on occasion (once every month or so).

any ideas would be great,

thanks

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    There are some options for this depending on how your code is organized to fetch the options. Assuming they are returned in the ajax.reload() response for the child you could use a flag to indicate if the options are needed. Use ajax.data as a function, see the last examples in the docs, to send the flag. The server script can use the flag to determine if it needs to return the options. Pseudo code example:

    var options = []; 
    var fetch_options = 1;
    
    $('#example').dataTable( {
      "ajax": {
        "url": "data.json",
        "data": function ( d ) {
            d.fetch_options_flag = fetch_options;
        }
      },
      initComplete: function () {
        fetch_options = 0;
      }
    } );
    

    The fetch_options_flag will be set to 1 which will indicate the server should return the options. After the table is initialized the fetch_options_flag will be 0 indicating not to return the options.

    Another option is to use a separate ajax query to fetch the options on page load and store in a global variable.

    Depending on how your code is setup will determine exactly how to do this. Hope these ideas help.

    Kevin

  • MarkAndersonUKMarkAndersonUK Posts: 44Questions: 17Answers: 0

    hi @kthorngren,

    thanks you have given me some ideas to think about.

    I am using the NodeJS server libraries to automatically create the server side
    scripts, so I am not sure how I can change this easily. I don't want to end up
    creating a nightmare setup that will be difficult to maintain and upgrade later.
    I will play around with your ideas and see if I can get them to work within the NodeJS environment.

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    In some cases I use separate ajax calls to load options. For me it is for the opposite reason though: I need to refresh the options more frequently than the data and they can be individual for each record which Editor doesn't support.

    But also in your case this can help because you can load options only when you need them and get rid of your server side Editor options instances.

    After loading the options you can simply use this: https://editor.datatables.net/reference/api/field().update()
    on success of your ajax call.

Sign In or Register to comment.