jqgrid-设置edittype的custom_value:'custom' [英] jqgrid - Set the custom_value of edittype: 'custom'

查看:945
本文介绍了jqgrid-设置edittype的custom_value:'custom'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

place_id的custom_value设置为我首先单击的那一行.后续单击的行将使用相同的值,而不管其实际值如何.为什么?

The custom_value of place_id is set to whichever row I click first. Subsequent clicked rows will all use that same value, regardless of their actual value. Why?

示例:

place_id foo_name bar_value
   10      blah       abc
   11      blah2      fgr

单击place_id为10的行,然后单击编辑",出现的表单的place_id值将为10.进行更改并保存,然后单击下一行.尽管所有其他值都是正确的,但表单仍将具有10的place_id.

click the row with the place_id of 10 and click "edit" and the form that appears will have 10 for the place_id value. Make a change and save it then click the next row down. The form will still have the place_id of 10 though all other values will be correct.

我的代码:

place_id列如下所示:

The column place_id looks like this:

{name:'place_id', index:'place_id', editable: true, edittype:'custom',
 editoptions: { custom_element:myelem,custom_value:myval }}

myval函数是:

function myval(elem){
    return elem.val();
}

我需要将myval设置为要编辑的行的正确place_id.我查看了Firebug中的elem对象,发现它始终具有第一个单击的行的值,但是我不明白为什么,也看不到可以从中获取正确值的地方.任何建议都值得赞赏(我尝试在jqgrid论坛上提问,但没有任何帮助,所以我转向stackoverflow).

What I need is for the myval to be set to the correct place_id for the row being edited. I looked at the elem object in Firebug and I see that it always has the value of whichever row was first clicked but I don't understand why nor do I see where I can grab the correct value from. Any suggestions are appreciated (I tried asking on the jqgrid forums but nothing came of it so I'm turning to stackoverflow).

*如果我使用edittype:'text'而不是edittype:'custom',则会显示并传递正确的值,但是该列是可编辑的,因此它应该仅可见而不是可编辑.

* If I use edittype:'text' rather than edittype:'custom' I get the correct values displayed and passed but then the column is editable and it should only be visible but not editable.

完整代码:

jQuery(document).ready(function(){ 
    jQuery("#list").jqGrid({
        url:'/foo/bar/results',
        datatype: 'json',
        mtype: 'POST',
        colNames:['Place ID', 'Site ID', 'Site Name', 'API ID', 'M Type'],
        colModel :[ 
            {name:'place_id', index:'place_id', key: true, sorttype:'integer',
             width:70, editable: true, edittype:'custom',
             editoptions: {custom_element:myelem,custom_value:myval }},
            {name:'site_id', index:'site_id', sorttype:'integer', width:70,
             editable: true, edittype: 'select', editrule: {required: true},
             editoptions:{value:getSites(), size:30}},
            {name:'site_name', index:'site_name', width:150, editable: false,
             editrule: {required: true}, editoptions: {size:30}},
            {name:'api_id', index:'api_id', sorttype:'integer', width:75,
             editable: true, edittype: 'text', editrules: {required: true},
             editoptions:{size:30}},
            {name:'m_type', index:'m_type', width:150, editable: true,
             edittype: 'select', editrules: {required: true},
             editoptions:{value:{'one':'one','two':'two','three':'three',
                                 'four':'four'},size:30}} 
        ],
        editurl:'/foo/bar/editinfo',    
        height: 'auto',
        width: 'auto',
        pager: '#pager',
        viewrecords: true,
        loadonce: true,
        rowNum:20,
        rowList:[20,40,60,80,100],
        caption: 'Meter Listing'
    }); 

    // Edit functionality
    $("#editfields").click(function(){
        var gr = jQuery("#list").jqGrid('getGridParam','selrow');
        if( gr != null ) { 
            jQuery('#list').setGridParam({datatype:'json'});
            jQuery("#list").jqGrid('editGridRow',gr,
                           {editCaption: 'Edit Place Details',height:550,
                            closeAfterEdit:true,width:600,reloadAfterSubmit:true});
        } else {
            alert("Please select a row");
        }
    });
});

function myelem(value,options){
    return $('<input type="text" value="'+value+'" disabled="disabled"/>');
}

function myval(elem){
    return elem.val();
}

edit2:

getSites:

function getSites(){
    <?php
    $sites = "0:Select";
    foreach ($this->sites as $k => $v){
        $sites = $sites . ";" . $v['site_id'] . ":" . $v['site_name'];
    }
    ?>
    return "<?= $sites ?>";
}

推荐答案

您是否可以尝试在place_id列的定义中包括key: true.您可以从服务器发送的数据中删除id.

Could you try to include key: true in the definition of the place_id column. You can remove id from the data which you send from the server.

如果这没有帮助,那么您应该发布完整的代码示例,以重现您的问题,我将尝试找到解决方法.

If it will not help, then you should post a full code example which reproduce your problem and I will try to find a workaround.

更新:有时候,有一个简单的解决方案可以解决复杂的问题. editGridRow函数的另一个参数可以解决您的问题:

UPDATED: Sometime there are a simple solution for complex problems. One additional parameter of editGridRow function solve your problem:

recreateForm: true

在不使用该参数的情况下,在所选行的第一次编辑中,函数myelem将仅被调用一次 .然后,该表单将被缓存,并且不重新创建.因此,您将始终编辑同一行.包含参数recreateForm: true后,将每次创建表单.

Without using of the parameter the function myelem will be called only one time at the first edit of the selected row. Then the form will be cached and not recreated. So you will be edit always the same row. After including of the parameter recreateForm: true the form will be created every time.

通过我设置一些通用设置的方式,这些通用设置将在扩展jQuery.jgrid.defaultsjQuery.jgrid.editjQuery.jgrid.viewjQuery.jgrid.deljQuery.jgrid.nav的所有jqGrid中使用.例如,我始终使用

By the way I set some general settings which I use in all jqGrids per extending jQuery.jgrid.defaults, jQuery.jgrid.edit, jQuery.jgrid.view, jQuery.jgrid.del and jQuery.jgrid.nav. For example I use always

jQuery.extend(jQuery.jgrid.edit, {
    closeAfterAdd: true,
    closeAfterEdit: true,
    recreateForm: true,
   //...
});

然后,我以后不需要设置此参数.在编写答案的过程中添加多个输入关于自定义编辑的自定义编辑类型字段中的元素,我也有设置,所以我没有看到任何问题.

Then I don't need set this parameters later. During writing of the answer Add multiple input elements in a custom edit type field about custom editing I has also the settings, so I didn't see any problems.

这篇关于jqgrid-设置edittype的custom_value:'custom'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆