【问题标题】:Remove underscore from Kendo dropdownlist从 Kendo 下拉列表中删除下划线
【发布时间】:2013-10-28 10:27:12
【问题描述】:

这个看起来很简单,只是不知道在哪里处理它。我正在通过 REST JSON 从 SQL 数据库中检索列名值。下拉列表中的值按预期带有下划线 (Customer_Name)。我想显示一个不带下划线的“友好”(客户名称)版本,但仍将我的值(客户名称)发送到我的 REST 服务。我查看了 Underscore.js,但不确定从哪里开始,这可能比我想象的要简单。谢谢!

【问题讨论】:

    标签: javascript kendo-ui underscore.js kendo-dropdown


    【解决方案1】:

    我不知道您是如何从您的休息服务接收数据的。

    但基本上,您只需映射 REST 服务接收到的数据,根据需要更改值。

    这里是一些示例代码:

    // Call to the REST service and when done call the callback function loadDDL
    $.ajax({
      type: "POST",
      url: "my-rest-service"
    }).done(loadDDL);
    
    
    // Callback function when returning from the REST service
    // -> load data in the DDL (here, it has the id "my-ddl")
    function loadDDL(data) { 
      $("#my-ddl").kendoDropDownList({
        dataTextField: "text",
        dataValueField: "value",
        dataSource: _.map(data, makeFriendlyName),
        index: 0
      }); 
    }
    
    
    // Function used by the _.map function in order 
    // change dynamically the labels in the DDL
    function makeFriendlyName(obj) {
      return { 
        text: obj.text, 
        value: obj.value.replace("_", " ") 
      };
    }
    

    编辑:

    基于 OP 的小提琴,这是一个使用模板而不是直接更改数据源的示例代码:

    function loadDDL(data) { 
      $("#my-ddl").kendoDropDownList({
        autoBind: true,
        dataTextField: "DOMAINQUERY",
        dataValueField: "COLUMN_NAME",
        dataSource: dataSourceSearch1,
        template: "${DOMAINQUERY.replace(/_/g, ' ')}"
      }); 
    }
    

    编辑 2:

    为了直接翻译数据源,我再次通过动态更改数据源的change 事件中的文本来重新映射数据源:

    var dataSourceSearch1 = new kendo.data.DataSource({
        transport: {
            read: {
                url: "http://demos.kendoui.com/service/Customers",
                dataType: "jsonp"
            }
        },
        change: changeDS // <-- Here add a change event : each time the datasource changes, this event is being raised
    });
    
    
    // This is the function call when the DS changes
    // the data stuff is in the `items` property which is the object send via the REST service
    function changeDS(datasource) {
        _.map(datasource.items, makeFriendlyName);
    }
    
    
    // Function to apply to each data -> here I just replace all spaces in the 
    // `ContactName` field by `_`
    function makeFriendlyName(data) {
        data.ContactName = data.ContactName.replace(/ /g, '_');
        return data;
    }
    
    
    // Fill the DDL with the previous datasource
    var cboSearchField1 = $("#cboSearchField1").kendoDropDownList({
        dataTextField: "ContactName",
        dataValueField: "ContactName",
        filter: "contains",
        autobind: true,
        select: cboSearchField1_Selected,
        change: cboSearchField1_onChange,
        dataSource: dataSourceSearch1
    }).data("kendoDropDownList");
    

    【讨论】:

    • 塞缪尔,谢谢。我正在尝试关注并喜欢您使用 .done(loadDDL); 的方式。但是,我的做法有点不同,也许您可​​以指出我需要在哪里插入 _.map。我通过你有_.map的变量来引用数据源。不确定这是否提供了足够的信息?
    • @Scott 你能详细说明一下你的代码吗?在这里,我只是使用_.map,以便根据您的休息服务创建一个友好的名称并将其发送到 DDL 的数据源,正如您在我的代码中看到的那样......
    • 谢谢@Samuel,如果这不是尽可能雄辩,我很抱歉,但在这里你可以看到我正在使用的东西。如果您有建议,我也可以让它变得不那么复杂,那就太棒了。感谢您的帮助,期待您的回复。 JSFiddle link
    • @Scott 我更新了你的fiddle。由于您的 REST 服务是本地的,我已为 Kendo UI 的演示更改为一个,但原理相同:在 {} 中使用模板 ans 您可以使用 javascript 代码,所以这里我将所有空格替换为 @ 987654330@(因为他们的数据中没有_)。
    • 嘿,这是我见过的最简单的方法!下拉列表打开时不显示下划线,但关闭时所选项目仍显示下划线。
    猜你喜欢
    • 2022-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2017-03-17
    • 2019-11-25
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多