سؤال

I'm using the Datatables jQuery plugin. The table is pulling the data from an AJAX source (a SQL Server query, processed by ASP.NET into a JSON object). I want to create a live view of the table so that changes appear in real time. But rather than reloading the entire table every few seconds with fnReloadAjax() (which, from experience, has proven to be quite burdensome on the browser) I'm only updating the records that are new or modified, using fnAddData() and fnUpdate().

After getting a JSON object of just the new or modified records, here's my code to process the object.

        var newData = updatedDataJSON.aaData;

        if (newData[0] != null) {                
            for (i = 0; i < newData.length; i++) {     //Loop through each object
                if (newData[i].bNewCase === true) {    //Process new cases
                    oTable.fnAddData(newData[i]);   
                } else {                              //Process modified cases   
                    var tableArray = oTable.fnGetData();
                    var index;
                    var found = false;
                    var serial = newData[i].serial;  
                    var dataObject = newData[i];

                    //First gotta find the index in the main table for 
                    // the record that has been modified.  This is done 
                    // by matching the serial number of the newData 
                    // object to the original aData set:

                    for (ii = 0; ii < tableArray.length; ii++) {
                        var value = tableArray[ii]['serial'];
                        value = value.replace(/<\/?[^>]+(>|$)/g, ""); 
                        if (value === serial) {
                            index = ii;
                            found = true;                                
                        }
                    }
                    if (found) {
                        oTable.fnUpdate(dataObject, index);
                        console.log('Updated ' + newData[i].serial);
                    }                                         
                }
            }
        }

My problem is that even though the newData.length property of the first for loop could be greater than 1, the for loop exits early (after one iteration). I added the console.log statement at the end and it started passing errors saying that newData[i].serial was undefined. This makes me think that the entire newData array was destroyed or something...

I'm really hoping that I've just made a stupid mistake (though I've checked and checked and checked some more but can't find one). Maybe there's something that I'm overlooking. If anyone has any advice, it would be greatly appreciated.

هل كانت مفيدة؟

المحلول

Credit goes to @elclarnrs for the solution, posted above in the comments. The solution was declaring the values of i and ii in the scope of the function. That got everything working smoothly. Good to know for future reference.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top