【问题标题】:jqgrid - Set the custom_value of edittype: 'custom'jqgrid - 设置edittype的custom_value:'custom'
【发布时间】:2010-08-05 15:51:56
【问题描述】:

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

示例:

place_id foo_name bar_value
   10      blah       abc
   11      blah2      fgr

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

我的代码:

place_id 列如下所示:

{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)。

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

完整代码:

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:

获取站点:

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

【问题讨论】:

  • 你能把myelem函数的代码也发一下吗?
  • 添加了...虽然我真的不知道value 来自哪里...嗯,甚至没有使用options
  • 你能发布getSites函数的代码吗?
  • 我看到了选项的来源......该函数使用 call() 来获取它需要的东西。现在,它为什么要抓住“错误”的东西?
  • 我已添加getSites

标签: javascript jquery jqgrid


【解决方案1】:

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

如果它没有帮助,那么你应该发布一个完整的代码示例来重现你的问题,我会尝试找到一个解决方法。

更新:有时对于复杂问题有一个简单的解决方案。 editGridRow 函数的一个附加参数可以解决您的问题:

recreateForm: true

如果不使用参数,函数myelem在第一次编辑所选行时调用一次。然后表单将被缓存并且不会重新创建。因此,您将始终编辑同一行。包含参数recreateForm: true 后,每次都会创建表单。

顺便说一句,我设置了一些常规设置,我在所有 jqGrids 中使用每个扩展 jQuery.jgrid.defaultsjQuery.jgrid.editjQuery.jgrid.viewjQuery.jgrid.deljQuery.jgrid.nav。例如我总是使用

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

那我以后就不需要设置这个参数了。在编写关于自定义编辑的答案Add multiple input elements in a custom edit type field 期间,我也有设置,所以我没有看到任何问题。

【讨论】:

  • key:true 没有帮助。我添加了一个更完整的代码示例。谢谢你看这个,奥列格。
  • 如果我刷新页面,place_id 可以再次选择,并且对于第一次选择是正确的。但是,在那之后,问题仍然存在。只有重新加载页面才有效。
  • 做到了!你在哪里找到那个参数?!你已经知道了还是什么?我查看了维基,根本没有注意到那个。我正打算用粗鲁的 window.location.reload() 强制页面重新加载;
  • wiki中至少有3个地方有这个参数。属性列表中trirand.com/jqgridwiki/doku.php?id=wiki:form_editing 的主要位置。 dataInit的描述中的其他地方。大约半年的时间里,我度过了一些奇怪的效果。我可以解决设置问题。在调试器中重现您的问题后,我可以看到 myelem 只会被调用一次。然后我记得这个参数。最后重要的是,问题解决了。您可以自己编辑 jqGrid Wiki,推荐使用 recreateForm: true 进行自定义编辑。
  • 你在哪里粘贴扩展代码?比如,你会创建一个名为 grid.extended.js 的文件,将代码粘贴在其中,然后将该文件与其他 jqgrid 文件一起加载吗?
猜你喜欢
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 2013-03-19
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多