Slickgrid - 一键复选框?

发布于 2024-11-27 17:14:14 字数 165 浏览 1 评论 0原文

当我在 Slickgrid 中创建复选框列(通过使用格式化程序/编辑器)时,我注意到需要单击两次才能与其交互(一次单击聚焦单元格,一次单击与复选框交互)。 (这非常有道理)

但是,我注意到我可以通过一键单击与复选框选择器插件(用于选择多行)进行交互。有什么方法可以让我的所有复选框都以这种方式运行吗?

When I create a checkbox column (through use of formatters/editors) in Slickgrid, I've noticed that it takes two clicks to interact with it (one to focus the cell, and one to interact with the checkbox). (Which makes perfect sense)

However, I've noticed that I am able to interact with the checkbox selectors plugin (for selecting multiple rows) with one click. Is there any way I can make ALL of my checkboxes behave this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

<逆流佳人身旁 2024-12-04 17:14:14

对于更多的读者,我通过修改单击事件上的网格数据本身解决了这个问题。将布尔值设置为相反,然后格式化程序将显示单击或未单击的复选框。

grid.onClick.subscribe (function (e, args)
{
    if ($(e.target).is(':checkbox') && options['editable'])
    {
        var column = args.grid.getColumns()[args.cell];

        if (column['editable'] == false || column['autoEdit'] == false)
            return;

        data[args.row][column.field] = !data[args.row][column.field];
    }  
});

function CheckboxFormatter (row, cell, value, columnDef, dataContext)
{
    if (value)
        return '<input type="checkbox" name="" value="'+ value +'" checked />';
    else
        return '<input type="checkbox" name="" value="' + value + '" />';
}

希望有帮助。

For futher readers I solved this problem by modifing the grid data itself on click event. Setting boolean value to opposite and then the formatter will display clicked or unclicked checkbox.

grid.onClick.subscribe (function (e, args)
{
    if ($(e.target).is(':checkbox') && options['editable'])
    {
        var column = args.grid.getColumns()[args.cell];

        if (column['editable'] == false || column['autoEdit'] == false)
            return;

        data[args.row][column.field] = !data[args.row][column.field];
    }  
});

function CheckboxFormatter (row, cell, value, columnDef, dataContext)
{
    if (value)
        return '<input type="checkbox" name="" value="'+ value +'" checked />';
    else
        return '<input type="checkbox" name="" value="' + value + '" />';
}

Hope it helps.

最美不过初阳 2024-12-04 17:14:14

我的做法非常简单。

  1. 第一步是您必须禁用复选框的编辑器处理程序。
    在我的项目中它看起来像这样。我有一个 slickgridhelper.js 来注册插件并使用它们。

    函数attachPluginsToColumns(columns) {
        $.each(列, 函数 (索引, 列) {
            if (列.强制) {
                列.validator = requiredFieldValidator;
            }
            if (列.可编辑) {
                if (column.type == "text" && column.autocomplete) {
                    column.editor = Slick.Editors.Auto;
                }
                else if (column.type == "复选框") {
                    //编辑器已被禁用。   
                    //column.editor = Slick.Editors.Checkbox;
                    column.formatter = Slick.Formatters.Checkmark;
                }
            }
        });    
    
  2. 下一步是在您正在开发的自定义 js 页面中注册 onClick 事件处理程序。下一步

grid.onClick.subscribe(function (e, args) {

        var row = args.grid.getData().getItems()[args.row];
        var column = args.grid.getColumns()[args.cell];

        if (column.editable && column.type == "checkbox") {
            row[column.field] = !row[column.field];
            refreshGrid(grid);
        }
    });

现在,只需单击一次即可更改复选框的值并保留它。

The way I have done it is pretty straight forward.

  1. First step is you have to disable the editor handler for your checkbox.
    In my project it looks something like this. I have a slickgridhelper.js to register plugins and work with them.

    function attachPluginsToColumns(columns) {
        $.each(columns, function (index, column) {
            if (column.mandatory) {
                column.validator = requiredFieldValidator;
            }
            if (column.editable) {
                if (column.type == "text" && column.autocomplete) {
                    column.editor = Slick.Editors.Auto;
                }
                else if (column.type == "checkbox") {
                    //Editor has been diasbled.   
                    //column.editor = Slick.Editors.Checkbox;
                    column.formatter = Slick.Formatters.Checkmark;
                }
            }
        });    
    
  2. Next step is to register an onClick event handler in your custom js page which you are developing.

grid.onClick.subscribe(function (e, args) {

        var row = args.grid.getData().getItems()[args.row];
        var column = args.grid.getColumns()[args.cell];

        if (column.editable && column.type == "checkbox") {
            row[column.field] = !row[column.field];
            refreshGrid(grid);
        }
    });

Now a single click is suffice to change the value of your checkbox and persist it.

远昼 2024-12-04 17:14:14

为“onClick”事件注册一个处理程序,并对其中的数据进行更改。
请参阅http://mleibman.github.com/SlickGrid/examples/example7-events。 html

grid.onClick.subscribe(function(e, args) {
    var checkbox = $(e.target);
    // do stuff
});

Register a handler for the "onClick" event and make the changes to the data there.
See http://mleibman.github.com/SlickGrid/examples/example7-events.html

grid.onClick.subscribe(function(e, args) {
    var checkbox = $(e.target);
    // do stuff
});
止于盛夏 2024-12-04 17:14:14

我发现解决这个问题的唯一方法是编辑 slick.checkboxselectcolumn.js 插件。我喜欢订阅方法,但它没有为我附加任何单选按钮的侦听器。

所以我所做的就是编辑函数handleClick(e, args) & handleHeaderClick(e, args)
我添加了函数调用,并且在我的 js 文件中我只是用它做了我想做的事情。

function handleClick(e, args) {
    if (_grid.getColumns()[args.cell].id === _options.columnId && $(e.target).is(":checkbox")) {
        ......
        //my custom line
        callCustonCheckboxListener();
        ......
    }
}

function handleHeaderClick(e, args) {
    if (args.column.id == _options.columnId && $(e.target).is(":checkbox")) {
        ...
        var isETargetChecked = $(e.target).is(":checked");
        if (isETargetChecked) {
            ...
            callCustonHeaderToggler(isETargetChecked);
        } else {
            ...
            callCustonHeaderToggler(isETargetChecked);
        }
        ...
    }
}

代码

pastebin.com/22snHdrw

在评论中搜索我的用户名

The only way I found solving it is by editing the slick.checkboxselectcolumn.js plugin. I liked the subscribe method, but it haven't attached to me any listener to the radio buttons.

So what I did is to edit the functions handleClick(e, args) & handleHeaderClick(e, args).
I added function calls, and in my js file I just did what I wanted with it.

function handleClick(e, args) {
    if (_grid.getColumns()[args.cell].id === _options.columnId && $(e.target).is(":checkbox")) {
        ......
        //my custom line
        callCustonCheckboxListener();
        ......
    }
}

function handleHeaderClick(e, args) {
    if (args.column.id == _options.columnId && $(e.target).is(":checkbox")) {
        ...
        var isETargetChecked = $(e.target).is(":checked");
        if (isETargetChecked) {
            ...
            callCustonHeaderToggler(isETargetChecked);
        } else {
            ...
            callCustonHeaderToggler(isETargetChecked);
        }
        ...
    }
}

Code

pastebin.com/22snHdrw

Search for my username in the comments

两人的回忆 2024-12-04 17:14:14

我使用 onBeforeEditCell 事件为我的布尔字段“can_transmit”实现此目的

基本上捕获编辑单元格,单击所需的列,自己进行更改,然后返回 false停止单元格编辑事件。

grid.onBeforeEditCell.subscribe(function(row, cell) {                
            if (grid.getColumns()[cell.cell].id == 'can_transmit') {
                if (data[cell.row].can_transmit) {
                   data[cell.row].can_transmit = false;
                }
                else {
                    data[cell.row].can_transmit = true;
                }
                grid.updateRow(cell.row);
                grid.invalidate();
                return false;
            }

这对我有用。但是,如果您使用 DataView 功能(例如过滤),则需要执行额外的工作来通过此更改更新数据视图。 我还没想出如何做到这一点......

I used the onBeforeEditCell event to achieve this for my boolean field 'can_transmit'

Basically capture an edit cell click on the column you want, make the change yourself, then return false to stop the cell edit event.

grid.onBeforeEditCell.subscribe(function(row, cell) {                
            if (grid.getColumns()[cell.cell].id == 'can_transmit') {
                if (data[cell.row].can_transmit) {
                   data[cell.row].can_transmit = false;
                }
                else {
                    data[cell.row].can_transmit = true;
                }
                grid.updateRow(cell.row);
                grid.invalidate();
                return false;
            }

This works for me. However, if you're using the DataView feature (e.g. filtering), there's additional work to update the dataview with this change. I haven't figured out how to do that yet...

℉服软 2024-12-04 17:14:14

我设法让单击编辑器与 DataView 一起工作,相当黑客。

setTimeout(function(){ $("theCheckBox").click(); },0);

通过调用我的 CheckBoxCellEditor 函数,并调用 Slick.GlobalEditorLock.commitCurrentEdit();, 当单击 CheckBoxCellEditor 创建的复选框时(通过 setTimeout)。

问题是,单击 CheckBoxCellFormatter 复选框,然后该事件会生成 CheckBoxCellEditor 代码,该代码会用新的复选框替换该复选框。如果您只是在该选择器上调用 jquery 的 .click() ,您将再次触发 CheckBoxCellEditor 事件,因为 slickgrid 尚未取消绑定最初​​让您到达那里的处理程序。 setTimeout 在删除该处理程序后触发单击(我担心计时问题,但我无法在任何浏览器中生成任何问题)。

抱歉,我无法提供任何示例代码,我拥有的代码是特定于实现的,可用作通用解决方案。

I managed to get a single click editor working rather hackishly with DataView by calling

setTimeout(function(){ $("theCheckBox").click(); },0);

in my CheckBoxCellEditor function, and calling Slick.GlobalEditorLock.commitCurrentEdit(); when the CheckBoxCellEditor created checkbox is clicked (by that setTimeout).

The problem is that the CheckBoxCellFormatter checkbox is clicked, then that event spawns the CheckBoxCellEditor code, which replaces the checkbox with a new one. If you simply call jquery's .click() on that selector, you'll fire the CheckBoxCellEditor event again due because slickgrid hasn't unbound the handler that got you there in the first place. The setTimeout fires the click after that handler is removed (I was worried about timing issues, but I was unable to produce any in any browser).

Sorry I couldn't provide any example code, the code I have is to implementation specific to be useful as a general solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文