fnUpdate doesn't seem to be working for me

fnUpdate doesn't seem to be working for me

leeguthleeguth Posts: 19Questions: 1Answers: 0
edited March 2014 in General
I am trying to use fnUpdate to update a particular column of my table....trying to get around the mRender issue I was having with a second ajax call. This is a really scaled down test code just so I can see how to go about this...


$.ajax({
url:"data.php",
dataType:"json",
data:{
"action": action
},
success:function(data){
oTable = $('#example').dataTable( {
"bAutoWidth":false,
"bProcessing": true,
"bDestroy": true,
"aaData": data,
"aoColumns": [
{ "mData": "prg_guid" },
{ "mData": null,
"defaultContent":" ",
"sName": "ostatus"}
]
} );
$.each(data,function(i,v){
var sData = oTable.fnGetData(i,1); // I get the current row, 2nd column - which is undefined because of above definition
sData = "hi"+i; // just trying to set it with an example text
var succeed = oTable.fnUpdate(sData, i,1) // trying to update the current row, column 1 (2nd column) with the text I put in sData
var vData = oTable.fnGetData(i,1); // trying to see if anything is there
console.log(vData); // the value displayed is null
});
}
} );

I'm sure I am doing this wrong, so any guidance to make this simple test work would be greatly appreciated. I tried to see if I could upgrade to DataTables 1.10 but for right now, I can't, so I can't use your previous suggestion of setting it with the cell.data.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    It isn't immediately obvious why that isn't working I'm afraid. Can you please link to a JSFiddle or http://live.datatables.net example showing the issue so I can debug it.

    Allan
  • leeguthleeguth Posts: 19Questions: 1Answers: 0
    I am not sure if I did this right. :) Also, since the data is coming from a database that is internal to my company, I have a feeling this isn't going to work for you. Hopefully this link works to get you to it....sorry, haven't used it before. http://live.datatables.net/yosadaw/1/edit
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Yeah - that doesn't work for me unfortunately.

    I've refactored it into a trivial test case: http://live.datatables.net/vaveguk/1/edit . And that appears to work correctly.

    The one thing that bothers me about your code the most (although I don't think it is the issue) is that you are using the array index `i` as the data index position in the table. I think you might get away with it here, by fluke. But generally you would want to loop over the nodes (using `oTable.fnGetNodes()` to get them) and then pass the row node into the other functions.

    However, as I say, I think you might just happen to get away with it because of how your table is constructed, so there is something else going on. However, as you can see, I can't reproduce the error. To be able to offer any help, I'd need to be able to do so so I can debug it.

    Allan
  • leeguthleeguth Posts: 19Questions: 1Answers: 0
    thank you. So instead of using fnGetData, I use fnGetNodes? I think I noticed in what you did that you used '0' where I had the i, that would then get the first row, right?

    fnGetNodes will return all nodes for all rows. I'm guessing I shouldn't use the 'i' here either to give me just the current row, so I would get all rows of data with how ever many columns I have. When you say to loop through them do you mean using a 'for' loop? kind of like below?

    $(document).ready( function () {
    var oTable = $('#example').dataTable();

    var nodes = oTable.fnGetNodes();

    for (i=0; i
  • leeguthleeguth Posts: 19Questions: 1Answers: 0
    well, we think we may have figured it out by trial and error...somewhat... I think it has to do with me defining the column with null on the mdata line. if we put another name in there then the column updated. Not sure if that is how it is supposed to work.... thanks again for the help.
This discussion has been closed.