DataTables logo DataTables

via Ad Packs
fnDelete with third parameter set to true messes up the table
  • Hello,

    I'm working with a completely dynamic datatable (version 1.5.1). I need to add and remove rows on the fly. After using fnDelete(pos, null, true) for the first time, I get errors like the following:

    When I try to call fnGetPosition

    oSettings.aoData[i] is null
    (line 1292)

    fnGetNodes:

    oSettings.aoData[i] is null
    (line 3677)

    My code is something like:

            var added_items_table = $j('#graph_added_items_table');
            added_items_table.dataTable();
    
            $j('#graph_button_add_serie,#graph_button_add_group_serie').click(function(event) {
                    var nodes = items_table.fnGetNodes();
                    var selected = fnGetSelected(nodes);
                    var added_nodes_indexes = [];
                    if (this.id == 'graph_button_add_serie') {
                            for (key in selected) {
                                    var pos = items_table.fnGetPosition(selected[key]);
                                    var data = items_table.fnGetData(pos);
                                    // I get a error here after using fnDelete
                                    var table_length = added_items_table.fnGetNodes().length;
                                    var radio_check = table_length == 0  ? 'checked' : '';
                                    var radio_ranking_str = '<input type="radio" name="graph_ranking_serie" value="'+ table_length +'"'+ radio_check +'>';
                                    key_json = $j.toJSON([data[1]]);
                                    var delete_image = '<div class="ss_delete">&nbsp;</div>';
                                    var index = added_items_table.fnAddData([delete_image, radio_ranking_str, data[0], data[0], 'avg', '', key_json], 0);
                                    if (index >= 0)
                                            added_nodes_indexes.push(index);
                            }
                    } else if (this.id == 'graph_button_add_group_serie') {
                            if (selected.length > 0) {
                                    var key_array = [];
                                    var name_array = [];
                                    for (key in selected) {
                                            var pos = items_table.fnGetPosition(selected[key]);
                                            var data = items_table.fnGetData(pos);
                                            key_array.push(data[1]);
                                            name_array.push(data[0]);
                                    }
                                    var key_json = $j.toJSON(key_array);
                                    var name_str = name_array.join(', ');
                                    // I get a error here after using fnDelete
                                    var table_length = added_items_table.fnGetNodes().length;
                                    var radio_check = table_length == 0  ? 'checked' : '';
                                    var radio_ranking_str = '<input type="radio" name="graph_ranking_serie" value="'+ table_length +'"'+ radio_check +'>';
                                    var delete_image = '<div class="ss_delete">&nbsp;</div>';
                                    var index = added_items_table.fnAddData([delete_image, radio_ranking_str, name_str, name_str, 'avg', '', key_json], 0);
                                    if (index >= 0)
                                            added_nodes_indexes.push(index);
                            }
                    }
                    $j(nodes).filter('.row_selected').removeClass('row_selected');
                    added_items_elements = [];
                    // only apply the below setting for the added items
                    for (key in added_nodes_indexes)
                            added_items_elements.push(added_items_table.fnGetNodes(added_nodes_indexes[key]));
                    var added_items_nodes = $j(added_items_elements);
    
                    added_items_nodes.find('.ss_delete').parents('td').click(function(event) {
                            $j(this).parents('tr').fadeOut('fast', function() {
                                    // I get an error here after the first delete
                                    var pos = added_items_table.fnGetPosition(this);
                                    added_items_table.fnDeleteRow(pos, null, true);
                            });
                    });
    
                   added_items_table.fnDraw();
            });
    
    
  • allanallan
    Posts: 15,874
    Hi douglaz,

    Nice catch - thanks for that. A bug in 1.5.1 (and everything before it!).

    To fix the fnGetNodes issue, you can replace the function _fnGetTrNodes with:

    		/*
    		 * Function: _fnGetTrNodes
    		 * Purpose:  Return an array with the TR nodes for the table
    		 * Returns:  array array:aData - TR array
    		 * Inputs:   object:oSettings - dataTables settings object
    		 */
    		function _fnGetTrNodes ( oSettings )
    		{
    			var aNodes = [];
    			var iLen = oSettings.aoData.length;
    			for ( var i=0 ; i<iLen ; i++ )
    			{
    				if ( oSettings.aoData[i] == null )
    					aNodes.push( null );
    				else
    					aNodes.push( oSettings.aoData[i].nTr );
    			}
    			return aNodes;
    		}
    

    And fnGetPosition with:

    		/*
    		 * Function: fnGetPosition 		 * Purpose:  Get the array indexes of a particular cell from it's DOM element
    		 * Returns:  int: - row index, or array[ int, int ]: - row index and column index
    		 * Inputs:   node:nNode - this can either be a TR or a TD in the table, the return is
    		 *             dependent on this input
    		 */
    		this.fnGetPosition = function( nNode )
    		{
    			var oSettings = _fnSettingsFromNode( this[_oExt.iApiIndex] );
    			var i;
    			
    			if ( nNode.nodeName == "TR" )
    			{
    				for ( i=0 ; i<oSettings.aoData.length ; i++ )
    				{
    					if ( oSettings.aoData[i] != null && oSettings.aoData[i].nTr == nNode )
    					{
    						return i;
    					}
    				}
    			}
    			else if ( nNode.nodeName == "TD" )
    			{
    				for ( i=0 ; i<oSettings.aoData.length ; i++ )
    				{
    					var iCorrector = 0;
    					for ( var j=0 ; j<oSettings.aoColumns.length ; j++ )
    					{
    						if ( oSettings.aoColumns[j].bVisible )
    						{
    							//$('>td', oSettings.aoData[i].nTr)[j-iCorrector] == nNode )
    							if ( oSettings.aoData[i] != null &&
    								oSettings.aoData[i].nTr.getElementsByTagName('td')[j-iCorrector] == nNode )
    							{
    								return [ i, j-iCorrector, j ];
    							}
    						}
    						else
    						{
    							iCorrector++;
    						}
    					}
    				}
    			}
    			return null;
    		};
    

    I'm sort of not sure about my replacement _fnGetTrNodes() function. It puts nulls into the return array, but retains the fnGetPosition() results... Getting a little nervous about having nulls in aoData at all...

    It would be great to hear how you get on with this.

    Thanks,
    Allan
  • Hello Allan,

    I only will be able to test the new code tomorrow. But now I must warn you that fnGetData isn't working either (a problem on line 3660 I think). Please take a look at it too.

    Thanks for all,

    douglaz
  • allanallan
    Posts: 15,874
    Another function needed then:

    		/*
    		 * Function: _fnGetDataMaster
    		 * Purpose:  Return an array with the full table data
    		 * Returns:  array array:aData - Master data array
    		 * Inputs:   object:oSettings - dataTables settings object
    		 */
    		function _fnGetDataMaster ( oSettings )
    		{
    			var aData = [];
    			var iLen = oSettings.aoData.length;
    			for ( var i=0 ; i<iLen; i++ )
    			{
    				if ( oSettings.aoData[i] == null )
    					aData.push( null );
    				else
    					aData.push( oSettings.aoData[i]._aData );
    			}
    			return aData;
    		}
    

    Allan
  • Allan,

    Now it works. Thanks!

    --
    douglaz
This discussion has been closed.
← All Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Support

Get useful and friendly help straight from the source.

In this Discussion