Slickgrid复选框事件问题

时间:2014-08-17 14:58:47

标签: javascript jquery slickgrid

我正在使用Slikgrid javascript库,我正在按照本教程创建一个带有复选框的网格:exemple4

使用的代码如下:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../slick.grid.css" type="text/css"/>
<link rel="stylesheet" href="../css/smoothness/jquery-ui-1.8.16.custom.css" type="text/css"/>
<link rel="stylesheet" href="examples.css" type="text/css"/>
<link rel="stylesheet" href="../controls/slick.columnpicker.css" type="text/css"/>
<style>
.slick-cell-checkboxsel {
background: #f0f0f0;
border-right-color: silver;
border-right-style: solid;
}
</style>
</head>
<body>
<div style="position:relative">
<div style="width:600px;">
<div id="myGrid" style="width:100%;height:500px;"></div>
</div>
<div class="options-panel">
<h2>Demonstrates:</h2>
<ul>
<li>Checkbox row select column</li>
</ul>
<h2>View Source:</h2>
<ul>
<li><A href="https://github.com/mleibman/SlickGrid/blob/gh-pages/examples/example-checkbox-row-select.html" target="_sourcewindow"> View the source for this example on Github</a></li>
</ul>
</div>
</div>
<script src="../lib/firebugx.js"></script>
<script src="../lib/jquery-1.7.min.js"></script>
<script src="../lib/jquery-ui-1.8.16.custom.min.js"></script>
<script src="../lib/jquery.event.drag-2.2.js"></script>
<script src="../slick.core.js"></script>
<script src="../plugins/slick.checkboxselectcolumn.js"></script>
<script src="../plugins/slick.autotooltips.js"></script>
<script src="../plugins/slick.cellrangedecorator.js"></script>
<script src="../plugins/slick.cellrangeselector.js"></script>
<script src="../plugins/slick.cellcopymanager.js"></script>
<script src="../plugins/slick.cellselectionmodel.js"></script>
<script src="../plugins/slick.rowselectionmodel.js"></script>
<script src="../controls/slick.columnpicker.js"></script>
<script src="../slick.formatters.js"></script>
<script src="../slick.editors.js"></script>
<script src="../slick.grid.js"></script>
<script>
var grid;
var data = [];
var options = {
editable: true,
enableCellNavigation: true,
asyncEditorLoading: false,
autoEdit: false
};
var columns = [];
$(function () {
for (var i = 0; i < 100; i++) {
var d = (data[i] = {});
d[0] = "Row " + i;
}
var checkboxSelector = new Slick.CheckboxSelectColumn({
cssClass: "slick-cell-checkboxsel"
});
columns.push(checkboxSelector.getColumnDefinition());
for (var i = 0; i < 5; i++) {
columns.push({
id: i,
name: String.fromCharCode("A".charCodeAt(0) + i),
field: i,
width: 100,
editor: Slick.Editors.Text
});
}
grid = new Slick.Grid("#myGrid", data, columns, options);
grid.setSelectionModel(new Slick.RowSelectionModel({selectActiveRow: false}));
grid.registerPlugin(checkboxSelector);
var columnpicker = new Slick.Controls.ColumnPicker(columns, grid, options);
})
</script>
</body>
</html>

我正在尝试回复一个复选框点击事件,但我发现无法解决这个问题。 我想要做的是创建一个数组,保存第一行元素数据,如[“行0”,...],并在选中第一个复选框时显示它。我该如何处理这个事件?

1 个答案:

答案 0 :(得分:0)

您要采取行动的事件是onSelectedRowsChanged。唯一的问题是没有直接链接回触发事件的特定行,就像在收听onMouseEnter等其他事件时使用getCellFromEvent时可用。

如果您将这两种方法结合使用,则可以使用onMouseEnter来“跟踪”当前行。

var context = {};
grid.onMouseEnter.subscribe(function (evt, args) {
    var cell = grid.getCellFromEvent(evt)
    context.rowIndex = cell.row;
    context.row = grid.getDataItem(cell.row);
})

处理onSelectedRowsChanged内的数据操作/渲染。

grid.onSelectedRowsChanged.subscribe(function (evt, args) {

    if (!context.row) {
        var rows = grid.getData();
        for (r in rows) {
            var row = rows[r]
            for (i = 1; i < columns.length; ++i) {
                row[i] = args.rows.length == 0 ? '' : dataContainer[r][i]
            }
        }
        grid.invalidateAllRows();
    } else {

        var display = args.rows.indexOf(context.rowIndex) >= 0;

        for (i = 1; i < columns.length; ++i) {
            context.row[i] = display ? dataContainer[context.rowIndex][i] : ''
        }

        grid.invalidateRow(context.rowIndex);
    }

    grid.render();
})

在此上下文中,dataContainer是“地图”与数组。根据数据的大小,您需要更快的查找。

注意此实施仅在使用RowSelectionModel的{​​{1}}选项时有效。否则,需要更自定义的数据处理实现。

Fiddle