Home  >  Q&A  >  body text

Vue js - Add text and dropdown box to editable HTML content box

<p>I'm using Vue.js. I'm trying to add text and a dropdown box in an editable HTML div. </p><p>I want to add a dropdown box using a button. This dropdown box can be added anywhere in the text, just like where I place my cursor. </p><p>Now, it almost works, but I can't find a problem. </p><p>When I enter and add multiple dropdown boxes and then press the "Get Data Model" button, it always shows that the dropdown box option I selected is incorrect. I mean it always shows the first option. </p><p>Using this "Get Data Model" button, I am trying to get all the text selected options. </p><p>Here is my code:</p><p><br /></p> <pre class="brush:php;toolbar:false;"><template> <div> <div class="content-editable" contenteditable="true" @input="onInput" ref="contentEditable"></div> <button @click="addDropdown">Add Dropdown</button> <button @click="getDataModel">Get Data Model</button> <div>{{ dataModel }}</div> </div> </template> <script> export default { data() { return { content: '', dropdowns: [], dropdownOptions: ['Option 1', 'Option 2', 'Option 3'], dataModel: '', }; }, methods: { onInput(event) { this.content = event.target.innerHTML.replace(/<div><br></div>/g, ''); }, addDropdown() { const dropdown = { selectedOption: this.dropdownOptions[0], }; this.dropdowns.push(dropdown); const editableDiv = this.$refs.contentEditable; const dropdownSelect = document.createElement('select'); dropdownSelect.style.width = '100px'; this.dropdownOptions.forEach((option) => { const dropdownOption = document.createElement('option'); dropdownOption.value = option; dropdownOption.text = option; dropdownSelect.appendChild(dropdownOption); }); editableDiv.appendChild(dropdownSelect); }, getDataModel() { const editableDiv = this.$refs.contentEditable; const clonedDiv = editableDiv.cloneNode(true); const selectElements = clonedDiv.querySelectorAll('select'); this.dropdowns.forEach((dropdown, index) => { const selectedOption = dropdown.selectedOption; const selectedOptionText = Array.from(selectElements[index].options).find((option) => option.value === selectedOption)?.text; const selectedOptionTextNode = document.createTextNode(` ${selectedOptionText}`); selectElements[index].replaceWith(selectedOptionTextNode); }); this.dataModel = clonedDiv.textContent; }, }, }; </script> <style scoped> .content-editable { border: 1px solid #ccc; padding: 10px; min-height: 100px; margin-bottom: 10px; } </style></pre> <p><br /></p>
P粉288069045P粉288069045423 days ago479

reply all(1)I'll reply

  • P粉321584263

    P粉3215842632023-07-28 10:46:58

    Vue.js is data-driven and adopts MVVM thinking. If you want to create multiple "input" tags, it makes more sense to use v-for instead of dynamically creating the DOM.

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <style scoped>
            .content-editable {
                border: 1px solid #ccc;
                padding: 10px;
                min-height: 100px;
                margin-bottom: 10px;
            }
        </style>
    </head>
    
    <body>
        <div id="app">
            <template>
                <div>
                    <button @click="addDropdown">creatSelect</button>
                    <div v-for="(dropdown, index) in dropdowns" :key="index">
                        <select v-model="dropdown.selectedValue">
                            <option v-for="option in dropdown.options" :value="option.value" :key="option.value">{{
                                option.label }}</option>
                        </select>
                    </div>
                </div>
                <button @click="getAllSelectedValues">getValue:</button>
                <div>valueList:{{ allSelectedValues }}</div>
            </template>
    
        </div>
    
    
    
    </body>
    
    </html>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                dropdowns: [],
    
                dropdownOptions: [
                    { value: 'option1', label: 'option1' },
                    { value: 'option2', label: 'option2' },
                    { value: 'option3', label: 'option3' },
                    { value: 'option4', label: 'option4' },
                ],
                allSelectedValues: [],
            },
            methods: {
                addDropdown() {
                    this.dropdowns.push({
                        selectedValue: null,
                        options: this.dropdownOptions,
                    });
                },
                getAllSelectedValues() {
                    this.allSelectedValues = this.dropdowns.map((dropdown) => dropdown.selectedValue);
                },
            },
    
        })
    
    
    </script>

    hope this helps!

    reply
    0
  • Cancelreply