SitePen / dgrid

A lightweight, mobile-ready, data-driven, modular grid widget designed for use with dstore
http://dgrid.io/
Other
628 stars 298 forks source link

Do not update row indices when re-inserting a row "in place" #783

Open JCVoogden opened 10 years ago

JCVoogden commented 10 years ago

If you update a row without changing order, row indices are re-calculated and the calcul is false (it occurs before the row is numbered).

This calcul is not necessary if the row is reinserted at the same position. I think that the row must be ordered before indices calculation (move line 625 before line 621).

In list.js I modified the test at line 621 (dgrid-0.3.12) : < if(previousRow){

if(previousRow && previousRow != row.previousSibling){

JCVoogden commented 10 years ago

To be more precise, the full patched "_insertRow" function (List.js) :

    insertRow: function(object, parent, beforeNode, i, options){
        // summary:
        //      Creates a single row in the grid.

        // Include parentId within row identifier if one was specified in options.
        // (This is used by tree to allow the same object to appear under
        // multiple parents.)
        var parentId = options.parentId,
            id = this.id + "-row-" + (parentId ? parentId + "-" : "") + 
                ((this.store && this.store.getIdentity) ? 
                    this.store.getIdentity(object) : this._autoId++),
            row = byId(id),
            previousRow = row && row.previousSibling;

        if(row){// if it existed elsewhere in the DOM, we will remove it, so we can recreate it
            if(row === beforeNode){
                beforeNode = (beforeNode.connected || beforeNode).nextSibling;
            }
            this.removeRow(row);
        }
        row = this.renderRow(object, options);
        row.className = (row.className || "") + " dgrid-row " +
            (i % 2 == 1 ? oddClass : evenClass) +
            (this.addUiClasses ? " ui-state-default" : "");
        // get the row id for easy retrieval
        this._rowIdToObject[row.id = id] = object;
        parent.insertBefore(row, beforeNode || null);

        row.rowIndex = i;
        if(previousRow && previousRow != row.previousSibling){
            // in this case, we are pulling the row from another location in the grid, and we need to readjust the rowIndices from the point it was removed
            this.adjustRowIndices(previousRow);
        }
        return row;
    }
JCVoogden commented 10 years ago

Afterthought, it is probably not sufficient. Indices should be recalculated from the min position between initial row index and final row index...

    row.rowIndex = i;
    if(previousRow && previousRow != row.previousSibling){
        // in this case, we are pulling the row from another location in the grid, and we need to readjust the rowIndices from the point it was removed
        this.adjustRowIndices((previousRow.rowIndex < row.rowIndex )? previousRow : row);
    }