2013-04-03 89 views
1

我试图根据所选项改变Kendo UI组合框的颜色。Kendo UI组合框事件

我已经把显示的问题[http://jsfiddle.net/PeWPE/]

我需要做的是确定所选择的项目在页面加载时什么小提琴。我可以捕获onDataBound事件,但在这一点上找不到选定的项目 - 我怀疑它不可用。

当用户从列表中选择一个新的项目选择事件给了我一切,我需要改变ComboBox的颜色数据,虽然颜色没有真正改变:(

所以,总之,有没有办法改变一个剑道UI组合框的颜色时,初始值设置(与固定我的语法任何帮助将是一件好事!)。

感谢您的帮助。

这里的代码...

$(document).ready(function() { 
// Create some data - including a color 
var data = [{ 
    text: "12 Angry Men", 
    value: "1", 
    color: "White" 
}, { 
    text: "Il buono, il brutto, il cattivo.", 
    value: "2", 
    color: "White" 
}, { 
    text: "Inception", 
    value: "3", 
    color: "Green" 
}, { 
    text: "One Flew Over the Cuckoo's Nest", 
    value: "4", 
    color: "Green" 
}, { 
    text: "Pulp Fiction", 
    value: "5", 
    color: "Blue" 
}, { 
    text: "Schindler's List", 
    value: "6", 
    color: "Blue" 
}, { 
    text: "The Dark Knight", 
    value: "7", 
    color: "Red" 
}, { 
    text: "The Godfather", 
    value: "8", 
    color: "Red" 
}, { 
    text: "The Godfather: Part II", 
    value: "9", 
    color: "Yellow" 
}, { 
    text: "The Shawshank Redemption", 
    value: "10", 
    color: "Yellow" 
}, { 
    text: "The Shawshank Redemption 2", 
    value: "11", 
    color: "Orange" 
}]; 

// Create the combo 
$("#movies").kendoComboBox({ 
    dataTextField: "text", 
    dataValueField: "value", 
    dataSource: data, 
    height: 100, 
    change: onChange, 
    dataBound: onDataBound, 
    select: onSelect 
}); 

// Select a value - Note no event is raised when doing this(!) 
var combo = $("#movies").data("kendoComboBox"); 
combo.value("9"); 

function onChange(e) { 
    alert('onChange Called'); 
    ctrl = this.element.attr("id"); 
    var dataItem = this.dataItem(e.item.index()); 
} 

// This is called - but the color is not being set 
function onSelect(e) { 
    alert('onSelect Called'); 
    var ctrl = this.element.attr("id"); 
    var dataItem = this.dataItem(e.item.index()); 
    alert('Control Id: ' +ctrl);  // Check we've got the control 
    alert('Color selected: ' + dataItem.color); 
    $('input[name="' + ctrl + '_input"]').css({ 
     backgroundColor: dataItem.color 
    }); 
    $('#movies').css({ 
     backgroundColor: dataItem.color 
    }); 

} 

function onDataBound(e) { 
    alert('onDataBound Called'); 
} 

}) 

回答

2

Kendo UI用自己的装饰HTML元素。这是他们必须使浏览器和平台在视觉上兼容的方式。

你应该定义你的组合框为:

$("#movies").kendoComboBox({ 
    dataTextField : "text", 
    dataValueField: "value", 
    dataSource : data, 
    height  : 100, 
    select  : onSelect, 
    dataBound  : onDataBound, 
    value   : 9 
}); 

注意:您可能会在定义设置的值,你不需要事后做。

然后定义两个事件处理程序为:

function onSelect(e){ 
    var dataItem = this.dataItem(e.item.index()); 
    this.input.css({ 'background-color' : dataItem.color }); 
} 

function onDataBound(e) { 
    var dataItem = this.dataItem(this.value()); 
    this.input.css({ 'background-color' : dataItem.color }); 
} 

onSelect当您更改值,而onDataBound用于使用初始值。

的工作小提琴这里:http://jsfiddle.net/OnaBai/PeWPE/4/

+0

感谢@OnaBai的帮助。不幸的是,我不能在kendoComboBox()标记中设置初始值,它由knockout设置,并且在绑定时没有使用this.value。任何想法如何在这些情况下在绑定时间设置颜色? – user2208192

+0

检查这个修改后的版本:http://jsfiddle.net/OnaBai/PeWPE/8/。在设置值之后,我调用trigger(“select”);'。这是一个稍微低一点的水平,但是当我们执行'value(9)'时,调用KendoUI不会调用的内容。你可能会意识到,我已经删除了'onSelect'上的'e'的引用,因为我没有将它作为参数传递给'trigger'。如果你需要它,你需要生成一个兼容版本的'e'并把它传递给'trigger'。我也删除了'onDataBound',因为如果你不设置初始值就不需要。 – OnaBai

+0

感谢您的帮助,Kendo文档中似乎有很多不是! – user2208192

2

当您在#movies输入中创建KendoUI组合框时,它实际上会隐藏该输入并创建一个新的输入。所以唯一的问题是你使用的选择器不正确。我已经更新了你的jsFiddle引用,但是如果你将onSelect方法更改为以下内容,它将解决你的问题。

function onSelect(e){ 
    var ctrl = this.element.attr("id"); 
    var dataItem = this.dataItem(e.item.index()); 
    var combobox = $("#movies").data("kendoComboBox"); 
    combobox.input.css({ 'background-color' : dataItem.color }); 
    combobox.list.css({ 'background-color' : dataItem.color }); 
} 

它应该解决您的问题(它在jsFiddle中添加颜色)。

+0

进一步发表评论在这里 - 你使用$(“#电影”)的数据(“kendoComboBox”),以获得一个参考回到您创建的组合框对象。这允许您引用它在运行中创建的列表和输入控件! – Avisra

+0

谢谢,这解决了手动选择项目时颜色的设置。我还在挣扎着dataBind上颜色的初始设置。 – user2208192