JavaScript JavaScript: How to pass and reset an incremental value to IndexedDB store get method?

The following code passes an argument variable nextId to the IndexedDB Store get method and the corresponding records are returned if the argument passed matches the key of the record.

Initially I created another method which gets all records into an array, then I returned the array length. However my loop simply gets the last value as it should.

The code looks like this: Note: This method getEmployeeCount() holds the array length and used like below:

    var something;
    for ( var i = 1; i <= getEmployeeCount(); i ++ )  {
        something = i;
    }

Experimental Working Code:

function displaySingleRecord() {

    var output = document.getElementById( 'display' );
    output.textContent = "";

    var store = getObjectStore(DB_STORE_NAME, 'readwrite');

    var  nextId = incre ++;
    var request = store.get(nextId.toString());

    request.onerror = function(e) {
        dbStatusMessage('Cannot Retrieve data.', 1);
    };

    request.onsuccess = function(e) {

        output.innerHTML = '<b>Employee ID:</b> '  + request.result.employeeId  + ' <br> ' +
                           '<b>First Name:</b>  '  + request.result.firstName   + ' <br> ' +
                           '<b>Last Name:</b>   '  + request.result.lastName    + ' <br> ' +
                           '<b>Email:</b>       '  + request.result.email       + ' <br> ' +
                           '<b>Department:</b>  '  + request.result.department;
    };
}

How do I reset the increment to 1 when the records reaches the end or last record. I tried using the output which is a p tag but did not work.

if ( output.textContent === '')
     doReset;

UPDATE: Working solution:

One of the primary issues I faced is getting the total records from the cursor and than using this number as a threshold to reset the increment process. The steps below illustrate what I did to a get working solution:

Step one: I used a hidden field to persist the incremental value and return it back to the method performing the increment: I also added two buttons: next and prev.

HTML:

<!--Hidden Field-->
<input id="currentValue" value="0" type="hidden" />

<a id="next" target='_blank' href="/">Next</a>
<a id="prev" target='_blank' href="/">Previous</a>

<!--Output value here-->
<p id="output"></p>

Next, I need to get the total records from a data store: I do this by getting the length of the array in the getTotalRecordCount method. Remember, when we increment, we need to know if the end has reached, this way we are not clicking next and seeing empty results, we simply reset to one. Note: We are passing the record array to the callback to avoid issues with IndexedDB async architectural:

Method to get total record from store:

function getTotalRecordCount(callback) {

    var IDBOpenRequest = indexedDB.open(Database.Name, Database.Version);

    IDBOpenRequest.onerror = function () {
        dbStatusMessage('Database failed to open.', 1000);
    };

    IDBOpenRequest.onsuccess = function (e) {

        var objectStore = getObjectStore(Database.Store.USGeological, 
            Database.ReadWrite, e.target.result);

        var record = [];
        var cursorRequest = objectStore.openCursor();

        cursorRequest.onsuccess = function (e) {
            var cursor = e.target.result;
            if (cursor) {
                record.push(cursor.value);
                cursor.continue();
            } else {

                callback(record);
            }
        };

        e.target.result.close();
    };
}

Next: we access the record array from the callback in the getNextId method.

Code:

function getNextId(val) {

    getTotalRecordCount(function (record){

    // Get initial or current value from hidden field.    
    var currentValue = document.getElementById('currentValue');

    var newValue = parseInt(currentValue.value,10) + val;

    // Use the record array from the callbck, pass it's length.
    // IMPORTANT: If our current position or count is greater 
    // than array length we go back to 1.   

    if (newValue > parseInt(record.length) || newValue <= 0) {
        newValue = 1;
    }

        currentValue.value = newValue;
        getRecordByRotatingId(newValue);

    });
}

Finally: we display our result to screen: This code simply passes the incremental value to the get method and displays the data in reference to the current id:

Note: This is developmental code, I am not using all of the call back events, for example: In the try catch I am purposely not using the e.message in the catch block.

   function getRecordByRotatingId(value) {

        var output = document.getElementById('output');
        output.textContent = "";

        var IDBOpenRequest = indexedDB.open(Database.Name, Database.Version);

        IDBOpenRequest.onerror = function () {
            dbStatusMessage('Database failed to open.', 1000);
        };

        IDBOpenRequest.onsuccess = function (e) {

            var objectStore = getObjectStore(Database.Store.USGeological,
                Database.ReadOnly, e.target.result);

            var request = objectStore.get(value);

            request.onsuccess = function () {

                try {

                    output.innerHTML = '<b>State ID:</b> ' + 
                        request.result.StateId +  ' <br> ' +
                        '<b>State:</b>  ' + request.result.StateName + 
                        ' <br> ' + '<b>Abbreviation:</b>   ' + 
                        request.result.StateAbbreviation;

                } catch (e) {

                    dbStatusMessage('Data does not exist.', 1000);
                }
            };

            request.onerror = function () {
                dbStatusMessage('Data does not exist.', 1000);
            };

            e.target.result.close();
        };
}

This code will have a performance hit in production with large data result set, that's because it calls the cursor each time when next or previous is clicked. We could place the total record count into memory (local storage) and update each time pages is loaded.

UPDATE: After my initial update above, I discovered you can also get the total record in an object store by using the index count method: With that, I have consolidated the method to just a single method:

IDBIndex.count()

function getRecordByNextId(val) {

    var IDBOpenRequest = indexedDB.open(Database.Name, Database.Version);

    IDBOpenRequest.onerror = function () {
        dbStatusMessage('Database failed to open.', 1000);
    };

    IDBOpenRequest.onsuccess = function (e) {

        var output = document.getElementById('output');
        output.textContent = "";

        var objectStore = getObjectStore(Database.Store.USGeological,
            Database.ReadOnly, e.target.result);

        var myIndex = objectStore.index('StateName');
        var request = myIndex.count();

        request.onsuccess = function () {

            var currentValue = document.getElementById('currentValue');
            var newValue = parseInt(currentValue.value, 10) + val;

           if (newValue > parseInt(request.result) || newValue <= 0) {
                newValue = 1;
            }

            currentValue.value = newValue;

            var dataRequest = objectStore.get(parseInt(currentValue.value));

            dataRequest.onsuccess = function (e) {

                output.innerHTML = '<b>State ID:</b> ' + dataRequest.result.StateId
                    + ' <br> ' + '<b>State:</b>  ' + dataRequest.result.StateName +
                    ' <br> ' + '<b>Abbreviation:</b>   ' +
                    dataRequest.result.StateAbbreviation;
            };
        };


        e.target.result.close();
    };
}
Answer:1

I'm quite new to javascript and can't find the solution. I have the following code. <form class="edit-task-form"> <legend>Create Task</legend> <label>Task</...

I'm quite new to javascript and can't find the solution. I have the following code. <form class="edit-task-form"> <legend>Create Task</legend> <label>Task</...

Any help converting Excel "power of" function to JavaScript? Values are: A1 = 99% A2 = 45 formula is = (A1^A2)*100 results rounded to whole number is 64.

Any help converting Excel "power of" function to JavaScript? Values are: A1 = 99% A2 = 45 formula is = (A1^A2)*100 results rounded to whole number is 64.

  1. excel power of 2
  2. excel power of 10
  3. excel power of e
  4. excel power of 3
  5. excel power of symbol
  6. excel power of 2 formula
  7. excel power of function
  8. excel power of number
  9. excel power of two
  10. excel power of 4
  11. excel power of n
  12. excel power of negative number
  13. excel power of 2 symbol
  14. excel power of
  15. excel power of matrix
  16. excel power of calculation
  17. excel of power bi
  18. excel axis power of 10
  19. excel formula power of x
  20. excel formula power of n

I'm trying to scroll the page down when I click a button with an animation to a certain distance, currently, I'm able to scroll down to a certain element but this is not what I want, I want to scroll ...

I'm trying to scroll the page down when I click a button with an animation to a certain distance, currently, I'm able to scroll down to a certain element but this is not what I want, I want to scroll ...

  1. can't scroll down on certain websites

I have a dropdown selection field and other fields. It goes like this, <div class="form-group"><label class="sr-only" for="id_max_size">Max Size</label><select name="max_size"...

I have a dropdown selection field and other fields. It goes like this, <div class="form-group"><label class="sr-only" for="id_max_size">Max Size</label><select name="max_size"...