Add row in the middle of table

Add row in the middle of table

yoavs-tyoavs-t Posts: 2Questions: 0Answers: 0
edited May 2009 in General
Hello,
I want to let the user the option to add a row after particular row(specified by index after selection).
The function fnAddData add rows just to the end of the table.

thanks

Replies

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Hi yoavs-t,

    Ordering in DataTables is done by using it's sorting functions. Therefore if you wish to insert a new row just after another one in the table you need to take account of this in your sorting.

    Take a look at this discussion which considers this: http://datatables.net/forums/comments.php?DiscussionID=227

    Allan
  • yoavs-tyoavs-t Posts: 2Questions: 0Answers: 0
    Hi/
    I have a table without sorting(bSort: false)
    Now, I want to add/delete/move rows according to row index of row selection.
    How can I do it?

    yoavs-t
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Hi yoavs-t,

    Okay - without sorting then the order is entirely defined by the 'aiDisplayMaster' (pre filtering) and 'aiDisplay' (post filtering) arrays in DataTables' settings object (typically called oSettings - can be retrieved by fnSettings()). These variables are arrays of indexes which point to the data in the aoData array, and define the order that this data should be output.

    What you could do is move the contents of these arrays around as you require (which is exactly how sorting works in DataTables), and then draw the table (fnDraw()).

    Hope this helps!
    Allan
  • FacundoFacundo Posts: 1Questions: 0Answers: 0
    edited April 2011
    Hi All,
    I know this thread is old but I have code something to add a row in the middle of the table based on this thread. After executing the code I am showing below aiDisplay, aiDisplayMaster and aoData have the correct data on them, the problem is that nothing changes when calling fnDraw. Any thoughts?

    [code]
    /**
    * Inserts the newly created quote after the affected row.
    * @param {Object} iForm form used to create the quote
    * @param {Object} insertedId inserted quote's id
    */
    function insertNewQuoteInTable(iForm, insertedId){
    var oDisplay = oTable.dataTableSettings[0].aiDisplay;
    var oDisplayMaster = oTable.dataTableSettings[0].aiDisplayMaster;
    var oData = oTable.dataTableSettings[0].aoData;
    var affectedRowNbr = -1;
    var COLUMN_WITH_ROW_ID = 4;
    var ind = 0;

    //look for affected row number
    while (affectedRowNbr==-1 && ind','>I'),
    iForm.price, ' ', iForm.quantity, ' ', ' ', ' ', ' ', aData[12], iForm.sequence];

    var newRow = {
    _aData : displayData,
    _iId: oData[affectedRowNbr]._iId + 1,
    _anHidden: oData[affectedRowNbr]._anHidden,
    _sRowStripe: oData[affectedRowNbr]._sRowStripe,
    nTr: oData[affectedRowNbr].nTr
    };

    //insert the new element
    oDisplay.splice(ind, 0, ind);
    oDisplayMaster.splice(ind, 0, ind);
    oData.splice(ind, 0, newRow);

    //update references
    var i = 0;
    for (i = ind+1; i
  • ivyivy Posts: 2Questions: 0Answers: 0
    Hi everyone,

    This plugin is so great, its like I dont have to compromise on anything!! I have the same doubt as above tho, I have disabled sorting and I want to add rows in the middle of the table. Is this possible? Since this post is really old, I want to confirm, that a solution has not already been found so I dont reinvent the wheel.. ! cheers
This discussion has been closed.