Cart Licenses Contact Support Online Creator Log in/Register
Try
Buy
v 1.9.51

Dropdown with Custom Item Template


                        
        
            Survey.StylesManager.applyTheme("defaultV2");
        



                
                    var json = {
  "elements": [
    {
      "type": "dropdown",
      "name": "car",
      "title": "What car are you driving?",
      "itemComponent": "new-item",
      "isRequired": true,
      "choices": [ "Ford", "Vauxhall", "Volkswagen", "Nissan", "Audi", "Mercedes-Benz", "BMW", "Peugeot", "Toyota", "Citroen" ]
    }
  ]
};

                    window.survey = new SurveyKnockout.Survey(json);
                
        
            survey.onComplete.add(function(sender) {
            document.querySelector('#surveyResult').textContent =
            "Result JSON:\n" + JSON.stringify(sender.data, null, 3);
            });
        
ko.components.register("new-item", {
  viewModel: {
    createViewModel: (params, componentInfo) => {
        const item = params.item;
        item.logoUrl = ("https://www.carlogos.org/car-logos/" + item.title.toLowerCase() + "-logo.png");
        return item;
    }
  },
  template: `
<div class="my-list-item" style="display:flex;">
    <img class="list-item_image" data-bind="attr:{src: logoUrl}" alt="Logo" />
    <span class="list-item_text" data-bind="text: title"></span>
</div>
`
});            
                survey.render("surveyElement");
            

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Dropdown with Custom Item Template, Knockoutjs Survey Library Example</title>
    
<meta name="viewport" content="width=device-width" />
    <script src="https://unpkg.com/knockout@3.5.1/build/output/knockout-latest.js"></script>
    <script src="/DevBuilds/survey-core/survey.core.min.js"></script>
    <script src="/DevBuilds/survey-core/survey.i18n.min.js"></script>
    <script src="/DevBuilds/survey-knockout-ui/survey-knockout-ui.min.js"></script>
    <link href="/DevBuilds/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="./index.css">

</head>
<body style="margin: 0">
    
            <div id="surveyElement" style="display:inline-block;width:100%;">
            </div>
    <div id="surveyResult"></div>

<script type="text/javascript" src="./index.js"></script>

</body>
</html>

.list-item_text {
    padding: 10px;
}

.list-item_image {
    width: 60px;
    height: 45px;
    vertical-align: middle;
}

If you want to customize a drop-down menu item, follow the steps below:

  1. Implement a component that renders item markup.
    The item is an IAction object. You can use component props to access it.

  2. Register the component so that it can be accessed by name.
    In React, register the component in ReactElementFactory as shown in the JavaScript tab.

    In Vue and Knockout, use techniques native to these libraries:

  3. Assign the component name to the Dropdown question's itemComponent property in the survey JSON definition.