0
我目前正試圖讓kendo ColorPalette
在我的網格上進行內聯編輯。除了我在將選定的顏色值發佈到我的控制器時遇到了一些困難之外,我幾乎全都明白了。Kendo ColorPalette不向控制器發佈價值
劍道網:
$("#ContactTagsGrid").kendoGrid({
dataSource: new kendo.data.DataSource({
transport: {
read: {
url: "/Admin/Tag/GetTagsByOrg",
dataType: "json",
data: {
orgId: OrganizationId
}
},
create: {
url: "/Admin/Tag/Create",
dataType: "json",
type: "POST",
data: function() {
return kendo.antiForgeryTokens();
}
}
},
schema: {
model: {
id: "Id",
fields: {
Id: { type: "number", nullable: true },
OrgId: { type: "number" },
Name: { type: "string" },
Color: { type: "string", defaultValue: "#f20000", validation: { required: true } }
}
}
},
pageSize: 20
}),
pageable: true,
sortable: true,
filterable: {
extra: false
},
scrollable: false,
columns: [
{
field: "Id",
hidden: true
},
{
field: "Name"
},
{
field: "Color",
editor: colorEditor,
template: function(dataItem) {
return "<div style='width: 25px; background-color: " + dataItem.Color + ";'> </div>";
},
width: "500px"
},
{
command: [
{
name: "Edit",
template:
"<a href='\\#' class='small btn btn-link k-grid-edit edit-text'><span class='fa fa-pencil'></span>Edit</a>",
text: "",
className: "fa fa-pencil"
},
{
template:
"<a href='\\#' class='small btn btn-link danger delete-text k-grid-delete'><span class='fa fa-trash-o'></span>Delete</a>",
name: "Delete",
text: " Delete",
className: "fa fa-trash-o"
}
],
width: "170px"
}
],
editable: {
mode: "inline",
destroy: false // don't use the kendo destroy method since we're using bootbox
},
// Custom save/cancel buttons
edit: function (e) {
var command = e.container.find("td:last");
command.html("<a href='\\#' class='small btn btn-primary k-grid-update'>Save</a><a href='\\#' class='small btn btn-default k-grid-cancel' style='margin-left: 5px'>Cancel</a");
}
});
Javascript來替換默認電網在線編輯器與Kendo ColorPalette
:
function colorEditor(container, options) {
// create an input element
var div = $("<div></div>");
var input = $("<input />");
input.attr("name", "Color");
// append it to the container
div.appendTo(container);
input.appendTo(div);
// initialize a Kendo UI ColorPicker
div.kendoColorPalette({
palette: [
"#f20000", "#c60000", "#337a00"
],
columns: 3,
change: function() {
var color = this.value();
$("input[name=Color]").val(color);
}
});
}
當我點擊Save
按鈕,唯一的值被貼到我的控制器是Name
和OrgId
。
如果我在我的模型的架構設置defaultValue
就像我在上面的代碼做了,那麼Color
默認值總是如果我選擇不同的顏色或不貼不管。
如果我沒有在架構中設置defaultValue
,則發佈的Color
的值爲null
。
所以基本上,我只需要幫助更新我的模型,以便在發佈到控制器時它是正確的。我可以看到,我的輸入值<input name="Color" />
在每次選擇新顏色時都會正確更新,但是實際上並沒有發佈它包含的值。
不知道這是否是需要的,但這裏是我的模型是什麼樣子:
public class TagCreateViewModel
{
public int OrgId { get; set; }
public string Name { get; set; }
public string Color { get; set; }
}
你有沒有理由避免Kendo拾色器?此外,網格將數據項發佈到控制器,而不是該行中的控件。您需要將行數據項綁定到輸入以便將其發佈。 – Failwyn
@Failwyn我更喜歡調色板的外觀而不是顏色選擇器,所以這就是我使用它的原因。但是,是的,我做了一些更多的研究,並偶然發現了一個有助於解決我的問題的不同答案。你是對的,但我需要將行數據綁定到輸入。謝謝! – Quiver