自定义ag-grid以设置可选行的最大数量

时间:2016-03-17 15:28:44

标签: angularjs datatable row selection ag-grid

我正在尝试使用基于Angular 1.5的项目中的ag-grid自定义数据表。自定义是允许用户在表中选择最大行数,例如,最大值为2.

我使用文档页面here中找到的node.setSelected(false)来获得以下代码,但是当选择超过最大值2时,我收到错误:node.setSelected is not a function

var gridOptions = {
    columnDefs: columnDefs,
    rowSelection: 'multiple',
    onRowSelected: onRowSelected
};


function onRowSelected(event) {
                var curSelectedNode = event.node;
                var selectionCounts = vm.gridOptions.api.getSelectedNodes().length;
                if (selectionCounts > 2) {
                    var oldestNode = vm.gridOptions.api.getSelectedNodes()[0]; // get the first node, to be popped out
                    oldestNode.setSelected(false); // causes the above 'not a function' error
                 }       
            }

是否有人知道其setSelected() API的ag-grid可能出现的问题?或者更好的方法来进行这种定制?

2 个答案:

答案 0 :(得分:2)

事实证明setSelected(false)方法在其当前ag-grid API中已过时,我发现我可以使用deselectIndex()方法取消选择最旧的节点:

if (selectionCounts > 2) {                        
                   vm.gridOptions.api.deselectIndex(0, true); // This works!
                 }

希望这将有助于其他人!

答案 1 :(得分:1)

var columnDefs =[{
                       headerName: 'Name',
                       field: 'name',
                       width: 108,
                       minLength: 1,
                       maxLength: 20,
                       editable: true
}]

- 修改文件.js中的原型

TextCellEditor.prototype.init = function (params) {
        var eInput = this.getGui();
        var startValue;

        // Set min & max length
        if (params.column.colDef.maxLength)
            eInput.maxLength = params.column.colDef.maxLength;
        if (params.column.colDef.minLength)
            eInput.minLength = params.column.colDef.minLength;

        // cellStartedEdit is only false if we are doing fullRow editing
        if (params.cellStartedEdit) {
            this.focusAfterAttached = true;
            var keyPressBackspaceOrDelete = params.keyPress === constants_1.Constants.KEY_BACKSPACE
                || params.keyPress === constants_1.Constants.KEY_DELETE;
            if (keyPressBackspaceOrDelete) {
                startValue = '';
            }
            else if (params.charPress) {
                startValue = params.charPress;
            }
            else {
                startValue = params.value;
                if (params.keyPress !== constants_1.Constants.KEY_F2) {
                    this.highlightAllOnFocus = true;
                }
            }
        }
        else {
            this.focusAfterAttached = false;
            startValue = params.value;
        }
        if (utils_1.Utils.exists(startValue)) {
            eInput.value = startValue;
        }
        this.addDestroyableEventListener(eInput, 'keydown', function (event) {
            var isNavigationKey = event.keyCode === constants_1.Constants.KEY_LEFT || event.keyCode === constants_1.Constants.KEY_RIGHT;
            if (isNavigationKey) {
                event.stopPropagation();
            }
        });
    };