【问题标题】:Show dropdown in cell of kendo grid in template在模板中剑道网格的单元格中显示下拉菜单
【发布时间】:2013-09-24 11:26:16
【问题描述】:

我的剑道网格工作正常,但我想在列的所有单元格中显示下拉列表,即“安全操作”列单元格。无论您是否正在编辑,以便用户知道这是该单元格中的下拉菜单。我不希望在单击单元格时出现下拉菜单,就像在剑道的this 演示站点中提到的那样。

我的代码如下

HTML:

<div id="example" class="k-content">
    <div id="clientsDb">
    <div id="grid" style="top:0px;width:100%;height: 380px">
    </div>              
    </div>

CSS:

<style scoped>
#clientsDb {
     width: 1400px;
         height: 310px;
     margin: 0 auto;
     padding: 0 4px 0 4px;
     background: url('../kendo/content/web/grid/clientsDb.png') no-repeat 0 0;
      }
</style>

JS:

 <script>
        function kendoGridExecute(data){
        $(document).ready(function() {

                                    $("#grid").kendoGrid({
                                        dataSource: {
                                            data: data.items,
                                            autoSync: true,
                                            schema: {
                                                model: {
                                                  fields: {
                                                        Type: { editable: false, nullable: true },
                                                        Penalty:{ editable: false, nullable: true },
                                                        Init_r:{ editable: false, nullable: true },
                                                        Opt_r:{ editable: false, nullable: true },
                                                        Change_r:{editable: false, nullable: true},     
                                                        Action:{defaultValue : {text: "No Action", value: "No Action" } }                               
                                                        }
                                                }
                                            }

                                        },
                                        groupable: true,
                                        sortable: true,
                                        scrollable: true,
                                        filterable: true,
                                        editable : true,

                                        columns: [ {
                                                field : "Type",
                                                width : 90,
                                                title : "Type"                                      
                                            } , {
                                                field : "Penalty",
                                                width : 120,
                                                title : "Penalty"

                                            } , {
                                                width : 50,
                                                field : "Bmk",
                                                title : "Abs. Bmk",
                                                template:"<input class='k-textbox' data-bind='value: Bmk' style='width:50px'>"     
                                            } , {
                                                width : 50,
                                                field : "Init_r",
                                                title : "Rel. Init"
                                            } , {
                                                width : 50,
                                                field : "Opt_r",
                                                title : "Rel. Opt"
                                            } , {
                                                title : "Rel. Chg",
                                                field : "Change_r",
                                                width : 50
                                            },{
                                                title : "Min",
                                                field : "Min",
                                                width: 50,
                                                template:"<input class='k-textbox' data-bind='value: Min' style='width:50px'>" 
                                            },{
                                                title : "Max",
                                                field : "Max",
                                                width : 50,
                                                template:"<input class='k-textbox' data-bind='value: Max' style='width:50px'>" 
                                            },{
                                                field : "Scale",
                                                title : "Scale",
                                                width : 50,
                                                template:"<input class='k-textbox' data-bind='value: Scale' style='width:50px'>" 
                                            },
                                            {
                                                field : "Init",
                                                title : "Abs. Init",
                                                width : 50
                                            },
                                            {
                                                field : "Opt",
                                                title : "Abs. Opt",
                                                width : 50
                                            },
                                            {
                                                field : "Action",
                                                title : "Security Action",
                                                width : 100,
                                                editor: securityActionDropDownEditor,
                                                template: "#=Action#" 
                                            }
                                        ],
                                        batch: true,
                                        dataBound: function() {
                                            var rows = this.tbody.children();
                                            var dataItems = this.dataSource.view();
                                                for (var i = 0; i < dataItems.length; i++)  {
                                                  kendo.bind(rows[i], dataItems[i]);
                                                }
                                            }


                                    });
                                });


                            }
                            function securityActionDropDownEditor(container, options) {

                                $('<input required data-text-field="text" data-value-field="value" data-bind="value:' + options.field + '"/>')
                                    .appendTo(container)
                                    .kendoDropDownList({
                                                dataSource: [ 
                                                              { text: "No Action", value: "No Action" },
                                                              { text: "DNS", value: "DNS" },
                                                              { text: "DNB", value: "DNB" },
                                                              { text: "DNT", value: "DNT" }
                                                 ],
                                                 autoBind: false,
                                                 index:0
                                    });
                                console.log(options);
                                console.log(options.field);
                            }
                            </script>

【问题讨论】:

    标签: jquery kendo-ui kendo-grid


    【解决方案1】:

    当Grid的dataBound事件发生时,可以通过客户端模板和一些自定义逻辑来初始化DropDownLists。

    假设您的专栏有以下模板:

    template: "<input value='#= PositionID #' data-bind='value:PositionID' data-role='dropdownlist' data-source='options' data-text-field='Text' data-value-field='Value' />"
    

    其中 options 是一个全局变量,其中包含您要用作数据源的集合

     var options = [{Text:"T1",Value:"V1"}...]
    

    然后在 Grid 的 dataBound 事件上,您可以将 DropDownList 绑定到该行的底层模型,如下所示:

    function onGridDataBound() {
        var grid = this;
        this.tbody.find('tr').each(function () {
            var item = grid.dataItem(this);
            kendo.bind(this, item);
        })
    }
    

    【讨论】:

    • data-source='options' 不起作用 data-source=' "+options+" ' 根本不起作用 为什么???
    • 这是一个问题,当组合值更改数据绑定事件调用时,请帮助我,它会按照函数中定义的方式迭代所有行,这会产生性能问题。
    • 很好的答案。您知道如何处理更改事件吗?我可以让它开火,但是在尝试通过RowIddropdownId 时失败了。我试过添加这个电话。 onchange='GridDDLChange(#=RowId#,#=DDLId#)' 它可以在没有参数的情况下工作,但不知何故我没有正确传递它们。
    • 不确定,但请尝试将它们放在引号内,以便将它们视为字符串。 onchange='GridDDLChange(\"#=RowId#\",\"#=DDLId#\")' 无论如何,使用委托事件有更方便的方法。
    • 天才啊!它有效,但它通过了之前的ddlID,而不是它被更改的内容。猜猜这是您的“更方便的方法”响应的结果。非常感谢!
    猜你喜欢
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多