jQuery jqGrid - 如何在编辑表单中隐藏字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2368051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jqGrid - how to have hidden fields in an edit form
提问by Fragsworth
I want to be able to pass fields into the edit form when a user attempts to edit a row, but I don't want these fields to be editable - I want them to just be hidden so they still get sent to the server.
当用户尝试编辑一行时,我希望能够将字段传递到编辑表单中,但我不希望这些字段可编辑 - 我希望它们只是被隐藏,以便它们仍然被发送到服务器。
For instance:
例如:
colModel :[
{label: 'Game ID', name: 'game_id', editable:true},
{label: 'Component ID', name: 'component_id', editable:true},
{label: 'Table ID', name: 'table_id', editable:true},
],
This will pass them to the edit form (because of editable:true
) but unfortunately they will be editable by the user. I want to hide these fields so the user can't edit them.
这会将它们传递到编辑表单(因为editable:true
),但不幸的是它们将由用户编辑。我想隐藏这些字段,以便用户无法编辑它们。
How can I do this?
我怎样才能做到这一点?
回答by Kinetic
This is now supported within jqGrid via the edithidden attribute:
这现在通过 edithidden 属性在 jqGrid 中得到支持:
colModel: [
{ name: 'optionValue', key: true, index: 'optionValue', width: 55, editable: true, hidden: true, editrules: { edithidden: false } },
Set to false to hide it in the add/edit form.
设置为 false 以将其隐藏在添加/编辑表单中。
回答by cmptrgeekken
EDIT
编辑
Okay, turns out you can define a custom element as an edittype. For your situation, you'd do the following:
好的,事实证明您可以将自定义元素定义为edittype。对于您的情况,您可以执行以下操作:
colModel :[
{label: 'Game ID', name: 'game_id', editable:true, edittype:'custom',editoptions:{custom_element:myelem,custom_value:myval}},
{label: 'Component ID', name: 'component_id', editable:true, edittype:'custom',editoptions:{custom_element:myelem,custom_value:myval} },
{label: 'Table ID', name: 'table_id', editable:true, edittype:'custom',editoptions:{custom_element:myelem,custom_value:myval}}
]
Then you'd define myelem
and myval
like so:
然后你会定义myelem
并myval
喜欢这样:
function myelem(value,options){
return $('<input type="text" value="'+value+'" disabled="disabled"/>');
}
function myval(elem){
return elem.val();
}
This will construct a disabled text field, and seems less hack-ish than afterShowForm
.
这将构建一个禁用的文本字段,并且看起来比afterShowForm
.
ORIGINAL
原来的
If you're using an edit form control (not inline editing), it looks like you have to provide an afterShowFormfunction (scroll down to the Events section). See this question.
如果您使用的是编辑表单控件(不是内联编辑),则看起来您必须提供afterShowForm函数(向下滚动到事件部分)。看到这个问题。
It looks like you want the columns to show up in the view, but not be editable. If you set editable:true
, then the user can edit the field no matter what. What you'll end up having to do is disable / set the form element to hidden so the user can't change it's value. afterShowForm
would then look something like:
看起来您希望列显示在视图中,但不可编辑。如果您设置了editable:true
,则用户无论如何都可以编辑该字段。您最终要做的是禁用/将表单元素设置为隐藏,以便用户无法更改它的值。afterShowForm
然后看起来像:
afterShowForm: function(eparams){
// change the selector appropriately
$('#jqGrid input[name=game_id]').attr('type','hidden');
// or $('#jqGrid input[name=game_id]').attr('disabled','disabled');
}
回答by Steve
i think a cleaner way is onclickSubmit event - you can add extra fields to be submitted.
我认为更简洁的方法是 onclickSubmit 事件 - 您可以添加要提交的额外字段。
var gr = jQuery("#table").jqGrid('getGridParam', 'selrow');
var row = jQuery("#table").getRowData(gr);
return { Id: row.Id };
回答by pphillips001
If it's info to pass to your edit database functions, you could just pass the additional parameters through the URL
如果将信息传递给您的编辑数据库函数,您只需通过 URL 传递附加参数
editurl:"database_edit.asp?user_id="+user_id
Not necessarily a better way - just a different one.
不一定是更好的方法 - 只是不同的方法。
Cheers
干杯
Paul
保罗
回答by marc bertaud
Try this:
尝试这个:
beforeShowForm: function (formid)
{
$("#tr_name",formid).hide();
},
回答by Bagus Putra
try this:
尝试这个:
colModel: [
{ name: 'your_field_name', editable: true, hidden: true, search:false, editoptions: {style:'display:none;'}},
]
回答by Mehdi Souregi
If you can trust the end user,you can simply add the editoptions attribute and set the disabled parameter to disabledlike this :
如果你可以信任的最终用户,你可以简单地添加editoptions属性,并设置残疾人参数来禁用这样的:
colModel: [
{ name: 'your_field_name', editable: true, hidden: true, search:false, editoptions: {'disabled':'disabled'}},
]