0

In relation to this question: Cannot edit/create/delete elements in table using Datatables and free editor

I'm trying to rewrite some of the datatables free editor to suit my needs, however i have encoutered an error that i can't wrap my head around.

console output

As you can see in the bottom of the picture (Outputs:) of the console.logs i can get the value of an array (the selected row) by hardcoding the property like this:

adata.data()[0].ntp_server 

But when changing the property of the array to the value from another array:

adata.data()[0].newaData[j].name  

I get undefined, even though the value of newaData[j].name is ntp_server

I've searched the web and stack, but cant find an explaination as to why. I've checked the types, tried assigning the newAdata value to a variable and a bunch of other stuff, but none of it works.

The script

I'm getting the columnheaders and row properties and pushing them to an array like this:

var dt = this.s.dt;

var columnDefs = [];

var newaData = [];

for( var i = 0; i < dt.context[0].aoColumns.length; i++ )
{
    columnDefs.push({ title: dt.context[0].aoColumns[i].sTitle });
    newaData.push({ name: dt.context[0].aoColumns[i].data});
}

I then wanna show the column headers and row values in a modal, which is done here:

var adata = dt.rows({
  selected: true
});

var data = "";

data += "<form name='altEditor-form' role='form'>";    

for(var j = 0; j < columnDefs.length; j++){
  data += "<div class='form-group'><div class='col-sm-3 col-md-3 col-lg-3 text-right' style='padding-top:7px;'><label for='" + columnDefs[j].title + "'>" + columnDefs[j].title + ":</label></div><div class='col-sm-9 col-md-9 col-lg-9'><input type='text'  id='" + columnDefs[j].title + "' name='" + columnDefs[j].title + "' placeholder='" + columnDefs[j].title + "' style='overflow:hidden'  class='form-control  form-control-sm' value='" + adata.data()[0][j] + "'></div><div style='clear:both;'></div></div>";           
}

The line where I encounter the problem is:

data += "<div class='f..... 
Community
  • 1
  • 1
BaconPancakes
  • 375
  • 7
  • 21
  • Please try `adata.data()[0][newaData[j]].name` – haipham23 Aug 15 '16 at 10:25
  • I modified it to `adata.data()[0][newaData[j].name]` and it works! thank you so much. Can you explain why it has to be written this way? – BaconPancakes Aug 15 '16 at 11:27
  • let assum new‌​aData[j] = 'string-name'. When you do `adata.data()[0][new‌​aData[j]].name`, it means `adata.data()[0]['string-name'].name`. You can find the explanation here: http://stackoverflow.com/questions/4255472/javascript-object-access-variable-property-by-name-as-string – haipham23 Aug 15 '16 at 15:35

0 Answers0