javascript 使用一些默认值在 Kendo Grid 中添加新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20187113/
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
Add new Row in Kendo Grid with some default values
提问by Rahul
I want to add new Row in Kendo Grid which is having Default value in First Cell. How can I set the Default Value in that added Row of Kendo Grid
我想在 Kendo Grid 中添加新行,该行在第一个单元格中具有默认值。如何在添加的 Kendo Grid 行中设置默认值
I am adding New Row in Kendo Grid as::
我在 Kendo Grid 中添加新行如下:
$('#AddSingleSuppliment').click(function () {
grid.addRow();
});
But I want to Set the Value of First cell on the Basis of Value of Clicked DOM element, Like
但是我想根据单击的 DOM 元素的值来设置第一个单元格的值,例如
$('#AddSingleSuppliment').click(function () {
var temVal=$(this).text();
grid.addRow(tempVal);
});
But we can't do it in that Manner. So please help me on this, For adding New Row in Kendo Grid with one Cell having Value of Button clicked.
但我们不能那样做。所以请帮我解决这个问题,用于在 Kendo Grid 中添加新行,其中一个单元格具有单击的按钮值。
Now I am able to Add New Row in Kendo Grid as,
现在我可以在 Kendo Grid 中添加新行,
$("#AddSingleSupplement").click( function(){
var tempSupplement = $(this).val();
//alert(tempSupplement);
grid.addRow(tempSupplement);
grid.dataSource._data[0].Description = $(this).text().trim();
});
But the Value is not Directly Shown while adding new Row. It is Shown after we click on some other element. Please Suggest me wether this one is the Correct way to do this or there is any other way than this.
但是在添加新行时不直接显示值。单击其他元素后显示。请建议我这是正确的方法还是除此之外还有其他方法。
回答by Bishnu Rawal
For dynamic defaults you can wire up your logic on Editevent, something like:
对于动态默认值,您可以在Edit事件上连接逻辑,例如:
<script>
$('#AddSingleSuppliment').click(function () {
grid.addRow();
});
function onEdit(e)
{
//Custom logic to default value
var name = $("#AddSingleSuppliment").text();
// If addition
if (e.model.isNew()) {
//set field
e.model.set("Name", name); // Name: grid field to set
}
}
</script>
回答by Sajjad Ali Khan
As per Kendo team , Default value cannot be changed dynamically.
However, we can make use the Grid editevent to pre-populate the edit form:
根据 Kendo 团队,默认值不能动态更改。
但是,我们可以使用 Grid编辑事件来预填充编辑表单:
edit: function(e) {
if (e.model.isNew() && !e.model.dirty) {
e.container
.find("input[name=ProductName]") // get the input element for the field
.val("MyCustomValue") // set the value
.change(); // trigger change in order to notify the model binding
}
}
}