简体   繁体   English

带有可编辑复选框列的jqGrid

[英]jqGrid with an editable checkbox column

When using jqGrid how do you force a cell to load in its editable view on page load as well as when it is clicked? 当使用jqGrid时,如何在页面加载以及单击时强制单元格在其可编辑视图中加载?

If you set up 'cell editing' like below, the check box only appears when you click on the cell. 如果您设置如下所示的“单元格编辑”,则仅在单击单元格时才会显示复选框。

{ name: 'MyCol', index: 'MyCol', editable:true, edittype:'checkbox', editoptions: { value:"True:False" },

cellEdit:true,

Also on clicking checkbox, is there a way of sending a AJAX post to server instantly rather than having to rely on the user pressing enter? 另外在点击复选框时,有没有办法立即向服务器发送AJAX帖子而不必依赖用户按Enter键?

To allow the checkboxes to always be click-able, use the checkbox formatter's disabled property: 要允许复选框始终可单击,请使用复选框格式化程序的disabled属性:

{ name: 'MyCol', index: 'MyCol', 
  editable:true, edittype:'checkbox', editoptions: { value:"True:False"}, 
  formatter: "checkbox", formatoptions: {disabled : false} , ...

To answer your second question, you will have to setup an event handler for the checkboxes, such that when one is clicked a function is called to, for example, send an AJAX POST to the server. 要回答第二个问题,您必须为复选框设置一个事件处理程序,这样当单击一个函数时,会调用一个函数,例如,向服务器发送一个AJAX POST。 Here is some example code to get you started. 这里有一些示例代码可以帮助您入门。 You can add this to the loadComplete event: 您可以将其添加到loadComplete事件:

    // Assuming check box is your only input field:
    jQuery(".jqgrow td input").each(function(){
        jQuery(this).click(function(){
            // POST your data here...
        });
    });

This is an old one but has a lot of view so I decided to add my solution here too. 这是一个旧的,但有很多视图所以我决定在这里添加我的解决方案。

I'm making use of the .delegate function of JQuery to create a late binding implementation that will free you from the obligation of using the loadComplete event. 我正在利用JQuery的.delegate函数创建一个后期绑定实现,使您免于使用loadComplete事件的义务。

Just add the following: 只需添加以下内容:

$(document).delegate('#myGrid .jqgrow td input', 'click', function () { alert('aaa'); });

This will late bind that handler to every checkbox that's on the grid rows. 这会将该处理程序延迟绑定到网格行上的每个复选框。 You may have a problem here if you have more than one checkbox column. 如果您有多个复选框列,则可能在此处出现问题。

I had the same problem and I suppose that I found a good solution to handle checkbox click immediately. 我遇到了同样的问题,我想我找到了一个很好的解决方案来立即处理复选框。 The main idea is to trigger editCell method when user clicks on the non-editable checkbox. 主要思想是在用户单击不可编辑的复选框时触发editCell方法。 Here is the code: 这是代码:

        jQuery(".jqgrow td").find("input:checkbox").live('click', function(){
            var iRow = $("#grid").getInd($(this).parent('td').parent('tr').attr('id'));
            var iCol = $(this).parent('td').parent('tr').find('td').index($(this).parent('td'));
            //I use edit-cell class to differ editable and non-editable checkbox
            if(!$(this).parent('td').hasClass('edit-cell')){
                                   //remove "checked" from non-editable checkbox
                $(this).attr('checked',!($(this).attr('checked')));
                        jQuery("#grid").editCell(iRow,iCol,true);
            }
    });

Except this, you should define events for your grid: 除此之外,您应该为网格定义事件:

            afterEditCell: function(rowid, cellname, value, iRow, iCol){
            //I use cellname, but possibly you need to apply it for each checkbox       
                if(cellname == 'locked'){
                //add "checked" to editable checkbox
                    $("#grid").find('tr:eq('+iRow+') td:eq('+iCol+') input:checkbox').attr('checked',!($("#regions").find('tr:eq('+iRow+') td:eq('+iCol+') input:checkbox').attr('checked')));
                            //trigger request
                    jQuery("#grid").saveCell(iRow,iCol);
                }   
            }, 

            afterSaveCell: function(rowid, cellname, value, iRow, iCol){
                if(cellname == 'locked'){
                    $("#grid").find('tr:eq('+iRow+') td:eq('+iCol+')').removeClass('edit-cell');
                }   
            }, 

Then your checkbox will send edit requests every time when user clicks on it. 然后,每次用户点击时,您的复选框都会发送编辑请求。

I have one submit function that sends all grid rows to webserver. 我有一个提交函数,它将所有网格行发送到网络服务器。

I resolved this problem using this code: 我使用此代码解决了这个问题:

var checkboxFix = [];
$("#jqTable td[aria-describedby='columnId'] input").each(function () {
        checkboxFix.push($(this).attr('checked'));
});

Then I mixed with values got from the code below. 然后我混合了从下面的代码中获得的值。

$("#jqTable").jqGrid('getGridParam', 'data');

I hope it helps someone. 我希望它对某人有帮助。

Better solution: 更好的方案:

<script type="text/javascript">
    var boxUnformat = function ( cellvalue, options, cell ) { return '-1'; },
        checkboxTemplate = {width:40, editable:true, 
            edittype: "checkbox", align: "center", unformat: boxUnformat, 
            formatter: "checkbox", editoptions: {"value": "Yes:No"},
            formatoptions: { disabled: false }};
    jQuery(document).ready(function($) {         
        $(document).on('change', 'input[type="checkbox"]', function(e){
            var td = $(this).parent(), tr = $(td).parent(),
                checked = $(this).attr('checked'),
                ids = td.attr('aria-describedby').split('_'),
                grid = $('#'+ids[0]),
                iRow = grid.getInd(tr.attr('id'));
                iCol = tr.find('td').index(td);
            grid.editCell(iRow,iCol,true);
            $('input[type="checkbox"]',td).attr('checked',!checked);
            grid.saveCell(iRow,iCol);
        });
    });
</script>

In your colModel: 在你的colModel中:

...
{name:'allowAccess', template: checkboxTemplate}, 
...

I had shared a full code at the link below, you can take a look if you need it. 我在下面的链接中分享了完整的代码,您可以查看是否需要它。

http://www.trirand.com/blog/?page_id=393/bugs/celledit-checkbox-needs-an-enter-pressed-for-saving-state/#p23968 http://www.trirand.com/blog/?page_id=393/bugs/celledit-checkbox-needs-an-enter-pressed-for-saving-state/#p23968

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM