You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Kendo DataSource渲染JSON时保留字符串开头空格(下拉选项)

Hey there! The issue you're hitting is two-fold: first, while Kendo's DataSource preserves the whitespace in your name field, HTML automatically collapses consecutive whitespace characters (including leading spaces) by default. Second, if you're using a default DropDownList without a custom template, you might not be explicitly telling the component to render the raw text.

Here's how to fix this and keep those leading spaces intact in your dropdown options:

Step 1: Use Custom Templates for Your DropDownList

You need to define explicit templates for both the dropdown items and the selected value, ensuring the raw name text is rendered. Additionally, add a CSS style to preserve whitespace.

Assuming your DropDownList is initialized somewhere in your code (likely tied to the editor template you shared), update its configuration like this:

// Initialize your DropDownList with custom templates
$("#yourCategoryDropdownId").kendoDropDownList({
    dataSource: categoriesDS,
    dataTextField: "name",
    dataValueField: "categoryId",
    // Template for items in the dropdown popup
    itemTemplate: '<span class="preserve-whitespace">#= name #</span>',
    // Template for the selected value shown in the dropdown input
    valueTemplate: '<span class="preserve-whitespace">#= name #</span>'
});

Step 2: Add CSS to Preserve Whitespace

Add this CSS rule to your stylesheet (or a <style> tag) to make sure the browser doesn't collapse the spaces:

.preserve-whitespace {
    white-space: pre; /* Preserves all spaces, tabs, and line breaks */
    /* Use white-space: pre-wrap; if you want text to wrap while keeping spaces */
}

Why This Works

  • The #= name # syntax in Kendo templates outputs the raw value of the name field without any trimming.
  • The white-space: pre CSS property tells the browser to treat the text like it's inside a <pre> tag—all whitespace characters are preserved exactly as they appear in the string.

If You're Using the Editor Template Directly

If your dropdown is defined within the popup_editor template (e.g., as a Kendo UI DropDownList widget), make sure to include the template configuration in its data-template attribute or widget initialization. For example:

Inside your editor template:

<div class="k-edit-label">
    <label for="category">Kategoria</label>
</div>
<div data-container-for="category" class="k-edit-field">
    <input id="categoryDropdown" name="category" 
           data-role="dropdownlist"
           data-bind="value: categoryId"
           data-source="categoriesDS"
           data-text-field="name"
           data-value-field="categoryId"
           data-item-template='<span class="preserve-whitespace">#= name #</span>'
           data-value-template='<span class="preserve-whitespace">#= name #</span>' />
</div>

Don't forget to include the preserve-whitespace CSS class as mentioned earlier!

内容的提问来源于stack exchange,提问作者Sruj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:57:09