SearchField 组件具有按钮行为和复选框问题

huangapple go评论67阅读模式
英文:

SearchField Component with Button Behavior and Checkbox Issue

问题

I am using Vue 3, Vuetify, and TypeScript. In my searchField component, I have two buttons named FreeItemMaster and PatternMaster. When I click these buttons, I want to change their background color and display their respective content below. But, my code doesn't work as expected. Also, the checkbox functionality is not working. What could be the issue?
这是我希望屏幕在点击每个按钮时看起来的方式。

<template>
    <div class="search-fields">
        <div class="search-condition mt-4">
            <div class="d-flex ml-6">
                <button :class="{ 'dark-grey-button': isFreeItemMasterBtnClicked, 'light-grey-button': !isFreeItemMasterBtnClicked }" @click="isFreeItemMasterBtnClicked = true; isPatternMasterBtnClicked = false">FreeItemMaster</button>
                <button :class="{ 'dark-grey-button': isPatternMasterBtnClicked, 'light-grey-button': !isPatternMasterBtnClicked }" @click="isPatternMasterBtnClicked = true; isFreeItemMasterBtnClicked = false">PatternMaster</button>
            </div>
            <div class="d-flex" v-if="isFreeItemMasterBtnClicked">
                <!-- FreeItemMasterBtn content -->
                <div class="ml-3 mt-5">
                    <v-checkbox
                        dense
                        hide-details
                        label="FreeItemMaster content"
                    />
                </div>
                <div class="ml-5 mt-7">
                    <v-btn rounded small style="background-color: #1ea0dc; color: #ffffff">Search</v-btn>
                </div>
            </div>
            <div class="d-flex" v-if="isPatternMasterBtnClicked">
                <!-- PatternMasterBtn content -->
                <div class="ml-3 mt-5">
                    <v-checkbox
                        dense
                        hide-details
                        label="PatternMaster content"
                    />
                </div>
                <div class="ml-5 mt-7">
                    <v-btn rounded small style="background-color: #1ea0dc; color: #ffffff">Search</v-btn>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
const isFreeItemMasterBtnClicked = ref(false);
const isPatternMasterBtnClicked = ref(true);
</script>

<style lang="scss" scoped>
.search-fields {
    display: flex;
    justify-content: space-between;
    .search-condition {
        p {
            font-size: 14px;
            font-weight: bold;
        }
    }
}

.dark-grey-button {
    background-color: #51565b;
    color: white;
    padding: 10px 10px;
    border: none;
    text-align: center;
    display: inline-block;
    font-size: 14px;
    margin-right: 2px;
}

.light-grey-button {
    background-color: #f0f3f4;
    color: black;
    padding: 10px 10px;
    border: none;
    text-align: center;
    display: inline-block;
    font-size: 14px;
    margin-right: 10px;
}
</style>
英文:

I am using Vue 3, Vuetify, and TypeScript. In my searchField component, I have two buttons named FreeItemMaster and PatternMaster. When I click these buttons, I want to change their background color and display their respective content below. But, my code doesn't work as expected. Also, the checkbox functionality is not working. What could be the issue?
This how I want my screen to look when each button is clicked.
(https://i.stack.imgur.com/Ih3bS.png)(https://i.stack.imgur.com/Gsg7N.png)

&lt;template&gt;
&lt;div class=&quot;search-fields&quot;&gt;
&lt;div class=&quot;search-condition mt-4&quot;&gt;
&lt;div class=&quot;d-flex ml-6&quot;&gt;
&lt;button :class=&quot;{&#39;dark-grey-button&#39;: isFreeItemMasterBtnClicked, &#39;light-grey-button&#39;: !isFreeItemMasterBtnClicked}&quot; @click=&quot;isFreeItemMasterBtnClicked = true; isPatternMasterBtnClicked = false&quot;&gt;FreeItemMaster&lt;/button&gt;
&lt;button :class=&quot;{&#39;dark-grey-button&#39;: isPatternMasterBtnClicked, &#39;light-grey-button&#39;: !isPatternMasterBtnClicked}&quot; @click=&quot;isPatternMasterBtnClicked = true; isFreeItemMasterBtnClicked = false&quot;&gt;PatternMaster&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;d-flex&quot; v-if=&quot;isFreeItemMasterBtnClicked&quot;&gt;
&lt;!-- FreeItemMasterBtn content --&gt;
&lt;div class=&quot;ml-3 mt-5&quot;&gt;
&lt;v-checkbox
dense
hide-details
label=&quot;FreeItemMaster content&quot;
/&gt;
&lt;/div&gt;
&lt;div class=&quot;ml-5 mt-7&quot;&gt;
&lt;v-btn rounded small style=&quot;background-color: #1ea0dc; color: #ffffff&quot;
&gt;Search&lt;/v-btn
&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;d-flex&quot; v-if=&quot;isPatternMasterBtnClicked&quot;&gt;
&lt;!-- PatternMasterBtn content --&gt;
&lt;div class=&quot;ml-3 mt-5&quot;&gt;
&lt;v-checkbox
dense
hide-details
label=&quot;PatternMaster content&quot;
/&gt;
&lt;/div&gt;
&lt;div class=&quot;ml-5 mt-7&quot;&gt;
&lt;v-btn rounded small style=&quot;background-color: #1ea0dc; color: #ffffff&quot;
&gt;Search&lt;/v-btn
&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/template&gt;
&lt;script setup lang=&quot;ts&quot;&gt;
import { ref } from &#39;vue&#39;;
const isFreeItemMasterBtnClicked = ref(false);
const isPatternMasterBtnClicked = ref(true);
&lt;/script&gt;
&lt;style lang=&quot;scss&quot; scoped&gt;
.search-fields {
display: flex;
justify-content: space-between;
.search-condition {
p {
font-size: 14px;
font-weight: bold;
}
}
}
.dark-grey-button {
background-color: #51565b;
color: white;
padding: 10px 10px;
border: none;
text-align: center;
display: inline-block;
font-size: 14px;
margin-right: 2px;
}
.light-grey-button {
background-color: #f0f3f4;
color: black;
padding: 10px 10px;
border: none;
text-align: center;
display: inline-block;
font-size: 14px;
margin-right: 10px;
}
&lt;/style&gt;

答案1

得分: 0

尝试为按钮点击处理程序添加函数:

// 在setup脚本内部
const onFreeItemMasterClick = () => {
    isFreeItemMasterBtnClicked.value = true;
    isPatternMasterBtnClicked.value = false;
};

const onPatternMasterClick = () => {
    isFreeItemMasterBtnClicked.value = false;
    isPatternMasterBtnClicked.value = true;
};

如果只有两个按钮要处理,单个布尔变量应该足够。复杂性会更低。

英文:

Try adding functions for the button click handlers:

&lt;div class=&quot;d-flex ml-6&quot;&gt;
&lt;button :class=&quot;{&#39;dark-grey-button&#39;: isFreeItemMasterBtnClicked, &#39;light-grey-button&#39;: !isFreeItemMasterBtnClicked}&quot; @click=&quot;onFreeItemMasterClick()&quot;&gt;FreeItemMaster&lt;/button&gt;
&lt;button :class=&quot;{&#39;dark-grey-button&#39;: isPatternMasterBtnClicked, &#39;light-grey-button&#39;: !isPatternMasterBtnClicked}&quot; @click=&quot;onPatternMasterClick()&quot;&gt;PatternMaster&lt;/button&gt;
&lt;/div&gt;
// inside setup script
  const onFreeItemMasterClick = () =&gt; {
      isFreeItemMasterBtnClicked.value = true;
      isPatternMasterBtnClicked.value = false;
  };

  const onPatternMasterClick = () =&gt; {
      isFreeItemMasterBtnClicked.value = false;
      isPatternMasterBtnClicked.value = true;
  };

If there are only two buttons to handle, single boolean variable should work. Complexity would be less.

huangapple
  • 本文由 发表于 2023年6月12日 16:07:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定