fnUpdate for a single cell

fnUpdate for a single cell

quyenquyen Posts: 4Questions: 0Answers: 0
edited January 2010 in General
Hi Alan,

I want to update a single cell data using fnUpdate. However, the data for single cell is in object type. I checked out the document for fnUpdate function, only allow a string for updating a single cell data and an array of strings for updating a row. So is this possible to update an object for a single cell and if yes, could you please show me how to do it?

Thank you very much,
Quyen

Replies

  • allanallan Posts: 61,775Questions: 1Answers: 10,112 Site admin
    Hi Quyen,

    To use fnUpdate to update the contents of a single cell, you just need to give the right number of parameters to fnUpdate. From the documentation ( http://datatables.net/api#fnUpdate ):

    Input parameters:
    1. string : The data to be added to the table. This array must be of the same length as the number of columns on the original HTML table.
    or
    1. array strings : Data to update the row with<

    2. int : Row to update (based upon aoData - use fnGetPosition())
    3. int : Column number to update - including hidden columns (ignored if the first parameter is an array)
    4. bool (optional) : Redraw the table after inserting the table (optional - default true)

    There is even an example in the code example of how to use the function to update a single cell: oTable.fnUpdate( 'Example update', 0, 0 );

    Regards,
    Allan
  • quyenquyen Posts: 4Questions: 0Answers: 0
    Hi Allan,

    Tks for your answer, I knew how to update a single cell with a string data type, such as your example oTable.fnUpdate( 'Example update', 0, 0 ).

    But I want to update an object for a single cell, for example:
    var oData = new Object;
    oData.name = quyen;
    oData.location = Germany;
    oTable.fnUpdate( oData, 0, 0 );

    The above code doesn't update the data for one cell but a row. What I need is to put the oData into one cell. Any suggestion???

    Thanks,
    Quyen
  • allanallan Posts: 61,775Questions: 1Answers: 10,112 Site admin
    Hi Quyen,

    Sorry for the delay - I had your question open in a tab that I'd completely forgotten about... I'm not sure why would would want to update a single cell (i.e. the text in the cell) with a Javascript object. How would you display an object as a string in a table?

    Regards,
    Allan
  • quyenquyen Posts: 4Questions: 0Answers: 0
    Hi Allan,

    The column I save the javascript object is invisible, I want to save the data there to retrieve them later on. I can store each field of an object in one column, but there are many fields so it's better to but them in an object and store in one column, would be better for maintenance and speed.

    Regards,
    Quyen
  • allanallan Posts: 61,775Questions: 1Answers: 10,112 Site admin
    Hi Quyen,

    Personally I'd say that storing JSON data as a string in the DOM wasn't a particularly good idea. Every time you want to access or change a property you would need to read the string eval() it, change/read the value, serialise the object and then write it back into the DOM. Doesn't sound very efficient :-)

    What you could do instead is tag your object onto to the object that DataTables uses internally for storing row information. The aoData array (a property of the settings object which you can get using fnSettings()) is an array with an entry for each row. You could then do something like aoData[ 10 ].myProperty = { ... };. Using fnGetPosition and possibly some of the plug-in API functions might be useful as well for this (mapping between an aoData index and a TR element).

    Regards,
    Allan
This discussion has been closed.