最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to approach creating a SlickGrid with AJAX, calculated fields, sortable columns, paste etc? - Stack Overflow

programmeradmin0浏览0评论

The project I'm working on intends to use a SlickGrid to display filterable and sortable data - up to a maximum of approximately 2 million rows. Other requirements are:

  1. Some columns have editable data but others have reference data that shouldn't be edited.
  2. Bulk copy and paste should be supported in editable fields.
  3. Some field values are calculated by applying formulae to other values in the same row.

Since there could be a large number of rows, data should be loaded using AJAX. Have seen from other questions that some have advised modifying code from the examples (...if so, what is the best fit starting point?). Others have suggested using plugins (e.g. .html provides copy and paste functionality or a fork such as Andrew Child's (maybe more risk as not officially supported?) There's a lot more to say but my question is really where best to start with all this - has anyone else had similar requirements and learnt by experience?

Grateful for any pointers, however small...

The project I'm working on intends to use a SlickGrid to display filterable and sortable data - up to a maximum of approximately 2 million rows. Other requirements are:

  1. Some columns have editable data but others have reference data that shouldn't be edited.
  2. Bulk copy and paste should be supported in editable fields.
  3. Some field values are calculated by applying formulae to other values in the same row.

Since there could be a large number of rows, data should be loaded using AJAX. Have seen from other questions that some have advised modifying code from the examples (...if so, what is the best fit starting point?). Others have suggested using plugins (e.g. http://labs.nereo./slick.html provides copy and paste functionality or a fork such as Andrew Child's (maybe more risk as not officially supported?) There's a lot more to say but my question is really where best to start with all this - has anyone else had similar requirements and learnt by experience?

Grateful for any pointers, however small...

Share Improve this question edited Oct 28, 2016 at 12:41 Steve Chambers asked Nov 10, 2013 at 19:54 Steve ChambersSteve Chambers 39.5k29 gold badges178 silver badges220 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

My thoughts on your bullet points:

Editable/Non-editable Columns

This will be as simple as defining an editor in a column definition or not.

var columns = [
    { id: 'Editable', field: 'EditableData', editor: Slick.Editors.Text },
    { id: 'NonEditable', field: 'NonEditableData' }
];

Editors are fairly easy to create and the ability to create pound editors provides great flexibility.

If you need to apply more business logic to make a single cell in the column non-editable or editable, you have two options:

  • Subscribe to the grid.onBeforeCellEdit event to cancel a cell from switching to edit mode under a certain condition.

  • Extend Slick.Data.DataView.getItemMetadata to provide custom metadata for an item, inside which you can disable an editor for a cell. A simplistic example:

    function getItemMetadata(i) {
        var item = rows[i];
    
        return {
            columns: {
                // disable the editor
                'InitiallyEditableColumn': { editor: null }
            }
        };
    }
    

Bulk copy-paste

There is an example of how to use the Slick.CellCopyManager to handle copy-paste within the grid itself.

Copy paste from an external spreadsheet is possible via the plugin that you mentioned.

Column Formulas

A Slick.Plugin could be created to calculate a defined formula from two operand columns into a result column. Handling the grid.onCellChange event seems to work best for this. The basic structure would look something like the following:

function ColumnFormula(args) {
    this.operands = args.operandColumns;
    this.result = args.resultColumns;
    this.formula = args.formula;
}

ColumnFormula.prototype.init = function (grid) {
    this.grid = grid;
    this.grid.onCellChange.subscribe(this.handleChange.bind(this));
};

ColumnFormula.prototype.handleChange = function (args) {
    if (/* change happened for an operand column */) {
        var dataItem = this.grid.getData().getItems()[args.row];
        // apply a formula to the operand fields in the dataItem
        dataItem[this.resultColumn] = this.formula(this.operands, dataItem);
        this.grid.updateRow(args.row);
    }
};

ColumnFormula.Sum = function (operands, dataItem) {
    return dataItem[operands[0]] + dataItem[operands[1]];
};

// ...

var myColumnFormula = new ColumnFormula({
    operandColumns: ['OperandOne', 'OperandTwo'],
    resultColumn: 'ResultColumn',
    formula: ColumnFormula.Sum
});

grid.registerPlugin(myColumnFormula);

I know this is more than a year later, but I used the puted formula section of @kavun answer for my project. One thing I found was if you used a dataview the puted column would not update because of var dataItem = this.grid.getData().getItems()[args.row], which gives you the row in the dataview not the grid. I changed that part to the function below and it worked for the dataview

handleChange = function (e, args) {
    var dataItem = args.item;        
     // apply a formula to the operand fields in the dataItem
     dataItem[this.result] = this.formula(this.operands, dataItem);
     this.grid.invalidateRow(dataItem);
};

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论