tags:

views:

268

answers:

3

Hi, I have a Javascript object that basically represents a Row in an .NET GridView. When a user clicks on any row in the grid, all the input elements in that row are 'enabled'.(ie 'Edit' mode).

I run this code depending on which row is selected

$(":input", this._row).attr('disabled', true);
or
$(":input", this._row).removeAttr('disabled');

So far so good. Now, I want to keep track of the values in that row before a user enters the 'Edit Mode', so i can restore the original values if they decide to click out of that row without saving any changes that they made.

So i capture the original values in an array by doing this:

var $inputs = $(":input", this._row);
var values = {}; 
$inputs.each(function(i, el) { values[el.name] = $(el).val(); });

the 'values' array now looks like this:

ctl00$ContentPlaceHolder1$resultsGrid$ctl04$COMPONENT1    "56" 
ctl00$ContentPlaceHolder1$resultsGrid$ctl04$COMPONENT2    "98" 
ctl00$ContentPlaceHolder1$resultsGrid$ctl04$COMPONENT3        "08"
ctl00$ContentPlaceHolder1$resultsGrid$ctl04$COMPONENT4    "200"

Great so far. The user may then modify these values, but decide not to save the changes. So i need to restore this row back to it's orignal values from the 'values' array. Can someone tell me the best way to do this? Im hoping it's something simple, but i'm no jquery expert, yet..

thanks

A: 

I haven't had the time to set up a test, but off of the top of my head I would try:

$inputs.each(function(i, el){ $(el).val(values[el.name]); });

Assuming you have already checked that values have been stored.

A: 

Could you not copy the row html and store it, and then if they changed there mind you could just restore the original row?

RubbleFord
well i stored the original innerHTML values fine, but when I grab that row later and try to set this._row.innerHTML to the old value, I get an 'Unknown runtime error'. any thoughts?Thanks
29er
+1  A: 

You can use the 'data method' (http://docs.jquery.com/Internals/jQuery.data) to store relevant data along with the element.

Something like:

//Call this function for each row after the page has loaded. 
function storeOriginalData(row)
{        
    var $inputs = $(":input", row);
    //For each input element inside the table row, we store it's original value 
    //using the 'data' method of jQuery.
    $inputs.each(function(i, el) { $(el).data('oldValue', el.val()); });    
}

//Call this function from the reset button code. 
function resetOriginalData(row)
{
    var $inputs = $(":input", row);
    //Now we get the original value using the data method and store it in the input element.
    $inputs.each(function(i, el) { $(el).val($el.data('oldValue')); 
};

This way, you will avoid the maintenance of the 'values' object for each row.

EDIT:

Modified code with more details and how it works.

SolutionYogi
I am trying to use/understand this but how exactly do i store the original data , to reset it later ?what does the first 'inputs.each' function actually create ?could you give a little more detail ?thanks!
29er
The 'data method' lets you associate any kind of data with a jQuery element. This means that you don't need to use that 'values' object in your code and maintain its state. I have modified the code with comments, let me know if you have further questions.
SolutionYogi
Thanks sooo much for taking that extra step in explaining. works great!! much appreciate
29er