只有当 JSON 值为真时显示。

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

Only show when JSON value is true

问题

如何只显示JSON数组中"checked"值设置为true的th/tr,使用v-if语句?
如果checked === true,我想要显示它。如果checked === false,我想要隐藏它。

App.vue:

  <Overview
    :activeColumns="items"
  ></Overview>

<script lang="ts" setup>
const items = [
  { id: 0, text: 'date', checked: true },
  { id: 1, text: 'name', checked: false },
  { id: 2, text: 'day', checked: false },
  { id: 3, text: 'time', checked: false },
  { id: 4, text: 'created', checked: false },
  { id: 5, text: 'reason', checked: true },
  { id: 6, text: 'comment', checked: true },
];

</script>

Overview.vue:

<script lang="ts" setup>
 const props = defineProps<{
  activeColumns: {};
}>();

const reactiveProps = reactive({
  activeColumns: props.activeColumns,
});

<table>
  <thead>
    <tr>
      <th class="date"><!-- 仅在 (text === 'date' && checked === true) 时显示 -->
        <CustomComponent /> <!-- 不使用JSONArray数据中的任何内容 -->
      </th>
      <th class="name"> <!-- 仅在 (text === 'name' && checked === true) 时显示 -->
        <OtherCustomComponent /> <!-- 不使用JSONArray数据中的任何内容 -->
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="date"><!-- 不使用JSONArray数据中的任何内容 --></td><!-- 仅在 (text === 'date' && checked === true) 时显示 -->
      <td class="name"><!-- 不使用JSONArray数据中的任何内容 --></td><!-- 仅在 (text === 'name' && checked === true) 时显示 -->
    </tr>
  </tbody>
</table>
</script>
英文:

How can I only show the th/tr where the value of "checked" in the JSON Array is set to true using a v-if statement?
If checked === true, I want to display it. If checked === false, I want to hide it.

App.vue:

  <Overview
    :activeColumns="items"
  ></Overview>

<script lang="ts" setup>
const items = [
  { id: 0, text: 'date', checked: true },
  { id: 1, text: 'name', checked: false },
  { id: 2, text: 'day', checked: false },
  { id: 3, text: 'time', checked: false },
  { id: 4, text: 'created', checked: false },
  { id: 5, text: 'reason', checked: true },
  { id: 6, text: 'comment', checked: true },
];

</script>

Overview.vue:

<script lang="ts" setup>
 const props = defineProps<{
  activeColumns: {};
}>();

const reactiveProps = reactive({
  activeColumns: props.activeColumns,
});

<table>
  <thead>
    <tr>
      <th class="date"><!-- Only show if (text === 'date' && checked === true) -->
        <CustomComponent /> <!-- Does not use any of the JSONArray data -->
      </th>
      <th class="name"> <!-- Only show if (text === 'name' && checked === true) -->
        <OtherCustomComponent /> <!-- Does not use any of the JSONArray data -->
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="date"><!-- Does not use any of the JSONArray data --></td><!-- Only show if (text === 'date' && checked === true) -->
      <td class="name"><!-- Does not use any of the JSONArray data --></td><!-- Only show if (text === 'name' && checked === true) -->
    </tr>
  </tbody>
</table>
</script>

答案1

得分: 1

以下是您要翻译的内容:

"It appears that your goal is to display table columns depending on the checked status of a data property that represents the column. If so, then again, I'd make all data reactive, and then I'd use Vue's computed properties to help us decide what to show.

So, assuming that this represents a simplification of the columns data:

const items = ref([
    { id: 0, text: 'date', checked: true },
    { id: 1, text: 'name', checked: false },
    { id: 2, text: 'day', checked: false },
    { id: 3, text: 'reason', checked: true },
    { id: 4, text: 'comment', checked: true },
]);

Then we can give the second component, Overview.vue, a props called "columns":

const props = defineProps(['columns']);

And then a computed property called activeColumns that holds only the "checked" column objects:

const activeColumns = computed(() => {
  return props.columns.filter((c) => c.checked);
});

Then let's assume that the data to display in the table looks something like this (again, a simplification):

const tableData = ref([
    {
        "id": 1,
        "date": "2/1/2023",
        "name": "SNAFU",
        "day": "Wednesday",
        "reason": "Whatever 1",
        "comment": "Comment 1"
    },
    {
        "id": 2,
        "date": "2/2/2023",
        "name": "FUBAR",
        "day": "Thurday",
        "reason": "Whatever 2",
        "comment": "Comment 2"
    },
    //.... more data here

Then we can use the computed property, activeColumns in a v-for to decide which data to show:

<tbody>
	<tr v-for="row in tableData" :key="row.id">
		<td v-for="col in activeColumns" :key="col.id">{{ row[col.text] }}</td>
	</tr>
</tbody>

You also mention,

In each div I have different components, so I can not loop over them with a v-for.

But this isn't necessarily true since you can put your components into an array or an object and use dynamic components to allow you to loop over them:

// components that show the related table headers
import Date from './Date.vue';
import Name from './Name.vue';
import Day from './Day.vue';
import Reason from './Reason.vue';
import Comment from './Comment.vue';

const props = defineProps(['columns']);

const activeColumns = computed(() => {
  return props.columns.filter((c) => c.checked);
});

// an object that holds all the table headers  
const columnHeaders = ref({
  'date': Date,
  'name': Name,
  'day': Day,
  'reason': Reason,
  'comment': Comment
});  

Displayed as dynamic components:

<thead>
    <tr>
        <th v-for="col in activeColumns" :key="col.id">
           <component :is="columnHeaders[col.text]"></component>
        </th>
    </tr>
</thead>

For example:

只有当 JSON 值为真时显示。

App.vue

<template>
    <h2>Columns To Display</h2>
    <div v-for="(item, index) in items">
        <label for="item.id">
            <input type="checkbox" v-model="item.checked" :value="item.id" :name="item.id" />
            {{ item.text }}
        </label>
    </div>
    <hr>
    <Overview :columns="items"></Overview>
</template>
    
<script lang="ts" setup>
import { ref } from 'vue';
import Overview from './Overview.vue';

const items = ref([
    { id: 0, text: 'date', checked: true },
    { id: 1, text: 'name', checked: false },
    { id: 2, text: 'day', checked: false },
    { id: 3, text: 'reason', checked: true },
    { id: 4, text: 'comment', checked: true },
]);
</script>

and Overview.vue

<template>
    <h2>
        Overview
    </h2>
    <table>
        <thead>
            <tr>
                <th v-for="col in activeColumns" :key="col.id">
                   <component :is="columnHeaders[col.text]"></component>
                </th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="row in tableData" :key="row.id">
                <td v-for="col in activeColumns" :key="col.id">{{ row[col.text] }}</td>
            </tr>
        </tbody>
</table>
</template>
  
<script lang="ts" setup>
import { ref, computed, defineProps } from 'vue';
import Date from './Date.vue';
import Name from './Name.vue';
import Day from './Day.vue';
import Reason from './Reason.vue';
import Comment from './Comment.vue';

const props = defineProps(['columns']);

const activeColumns = computed(() => {
  return props.columns.filter((c) => c.checked);
});
  
const columnHeaders = ref({
  'date': Date,
  'name': Name,
  'day': Day,
  'reason': Reason,
  'comment': Comment
});  

const tableData = ref([
    {
        "id": 1,
        "date": "2/1/2023",
        "name": "SNAFU",
        "day": "Wednesday",
        "reason": "Lorem ipsum",
        "comment": "dolor sit amet, consectetur adipiscing elit"
    },
    {
        "id": 2,
        "date": "2/2/2023",
        "name": "FUBAR",
        "day": "Thurday",
        "reason": "Ut enim",
        "comment": "ad minim veniam, quis nostrud exercitation"
    },
    {
        "id": 3,
        "date": "2/3/2023",
        "name": "BUZBAF",
        "day": "Friday",
        "reason": "Duis aute irure",
        "comment": "dolor in reprehenderit in voluptate velit esse cillum dolore"
    },
    {
        "id": 4,
        "date": "2/4/2023",
        "name": "FOO",
        "day": "Saturday",
        "reason": "Excepteur sint occaecat",
        "comment": "cupidatat non proident, sunt in culpa qui officia"
    },
    {
        "id": 5,
        "date": "2/5/2023",
        "name": "BAR",
        "day": "Sunday",
        "reason": "Sed ut perspiciatis",
        "comment":

<details>
<summary>英文:</summary>

It appears that your goal is to display table columns depending on the checked status of a data property that represents the column. If so, then again, I&#39;d make all data reactive, and then I&#39;d use Vue&#39;s computed properties to help us decide what to show.

So, assuming that this represents a simplification of the columns data:

```lang-js
const items = ref([
    { id: 0, text: &#39;date&#39;, checked: true },
    { id: 1, text: &#39;name&#39;, checked: false },
    { id: 2, text: &#39;day&#39;, checked: false },
    { id: 3, text: &#39;reason&#39;, checked: true },
    { id: 4, text: &#39;comment&#39;, checked: true },
]);

Then we can give the second component, Overview.vue, a props called "columns":

const props = defineProps([&#39;columns&#39;]);

And then a computed property called activeColumns that holds only the "checked" column objects:

const activeColumns = computed(() =&gt; {
  return props.columns.filter((c) =&gt; c.checked);
});

Then let's assume that the data to display in the table looks something like this (again, a simplification):

const tableData = ref([
    {
        &quot;id&quot;: 1,
        &quot;date&quot;: &quot;2/1/2023&quot;,
        &quot;name&quot;: &quot;SNAFU&quot;,
        &quot;day&quot;: &quot;Wednesday&quot;,
        &quot;reason&quot;: &quot;Whatever 1&quot;,
        &quot;comment&quot;: &quot;Comment 1&quot;
    },
    {
        &quot;id&quot;: 2,
        &quot;date&quot;: &quot;2/2/2023&quot;,
        &quot;name&quot;: &quot;FUBAR&quot;,
        &quot;day&quot;: &quot;Thurday&quot;,
        &quot;reason&quot;: &quot;Whatever 2&quot;,
        &quot;comment&quot;: &quot;Comment 2&quot;
    },
	//.... more data here

Then we can use the computed property, activeColumns in a v-for to decide which data to show:

&lt;tbody&gt;
	&lt;tr v-for=&quot;row in tableData&quot; :key=&quot;row.id&quot;&gt;
		&lt;td v-for=&quot;col in activeColumns&quot; :key=&quot;col.id&quot;&gt;{{ row[col.text] }}&lt;/td&gt;
	&lt;/tr&gt;
&lt;/tbody&gt;

You also mention,
> In each div I have different components, so I can not loop over them with a v-for.

But this isn't necessarily true since you can put your components into an array or an object and use dynamic components to allow you to loop over them:

// components that show the related table headers
import Date from &#39;./Date.vue&#39;;
import Name from &#39;./Name.vue&#39;;
import Day from &#39;./Day.vue&#39;;
import Reason from &#39;./Reason.vue&#39;;
import Comment from &#39;./Comment.vue&#39;;

const props = defineProps([&#39;columns&#39;]);

const activeColumns = computed(() =&gt; {
  return props.columns.filter((c) =&gt; c.checked);
});

// an object that holds all the table headers  
const columnHeaders = ref({
  &#39;date&#39;: Date,
  &#39;name&#39;: Name,
  &#39;day&#39;: Day,
  &#39;reason&#39;: Reason,
  &#39;comment&#39;: Comment
});  

Displayed as dynamic components:

&lt;thead&gt;
    &lt;tr&gt;
        &lt;th v-for=&quot;col in activeColumns&quot; :key=&quot;col.id&quot;&gt;
           &lt;component :is=&quot;columnHeaders[col.text]&quot;&gt;&lt;/component&gt;
        &lt;/th&gt;
    &lt;/tr&gt;
&lt;/thead&gt;

For example:

只有当 JSON 值为真时显示。

App.vue

&lt;template&gt;
    &lt;h2&gt;Columns To Display&lt;/h2&gt;
    &lt;div v-for=&quot;(item, index) in items&quot;&gt;
        &lt;label for=&quot;item.id&quot;&gt;
            &lt;input type=&quot;checkbox&quot; v-model=&quot;item.checked&quot; :value=&quot;item.id&quot; :name=&quot;item.id&quot; /&gt;
            {{ item.text }}
        &lt;/label&gt;

    &lt;/div&gt;

    &lt;hr&gt;
&lt;Overview :columns=&quot;items&quot;&gt;&lt;/Overview&gt;
&lt;/template&gt;
    
&lt;script lang=&quot;ts&quot; setup&gt;

import { ref } from &#39;vue&#39;;
import Overview from &#39;./Overview.vue&#39;;

const items = ref([
    { id: 0, text: &#39;date&#39;, checked: true },
    { id: 1, text: &#39;name&#39;, checked: false },
    { id: 2, text: &#39;day&#39;, checked: false },
    { id: 3, text: &#39;reason&#39;, checked: true },
    { id: 4, text: &#39;comment&#39;, checked: true },
]);
&lt;/script&gt;

and Overview.vue

&lt;template&gt;
    &lt;h2&gt;
        Overview
    &lt;/h2&gt;
    &lt;table&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th v-for=&quot;col in activeColumns&quot; :key=&quot;col.id&quot;&gt;
                   &lt;component :is=&quot;columnHeaders[col.text]&quot;&gt;&lt;/component&gt;
                &lt;/th&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
            &lt;tr v-for=&quot;row in tableData&quot; :key=&quot;row.id&quot;&gt;
                &lt;td v-for=&quot;col in activeColumns&quot; :key=&quot;col.id&quot;&gt;{{ row[col.text] }}&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
&lt;/table&gt;
&lt;/template&gt;
  
&lt;script lang=&quot;ts&quot; setup&gt;

import { ref, computed, defineProps } from &#39;vue&#39;;
import Date from &#39;./Date.vue&#39;;
import Name from &#39;./Name.vue&#39;;
import Day from &#39;./Day.vue&#39;;
import Reason from &#39;./Reason.vue&#39;;
import Comment from &#39;./Comment.vue&#39;;

const props = defineProps([&#39;columns&#39;]);

const activeColumns = computed(() =&gt; {
  return props.columns.filter((c) =&gt; c.checked);
});
  
const columnHeaders = ref({
  &#39;date&#39;: Date,
  &#39;name&#39;: Name,
  &#39;day&#39;: Day,
  &#39;reason&#39;: Reason,
  &#39;comment&#39;: Comment
});  

const tableData = ref([
    {
        &quot;id&quot;: 1,
        &quot;date&quot;: &quot;2/1/2023&quot;,
        &quot;name&quot;: &quot;SNAFU&quot;,
        &quot;day&quot;: &quot;Wednesday&quot;,
        &quot;reason&quot;: &quot;Lorem ipsum&quot;,
        &quot;comment&quot;: &quot;dolor sit amet, consectetur adipiscing elit&quot;
    },
    {
        &quot;id&quot;: 2,
        &quot;date&quot;: &quot;2/2/2023&quot;,
        &quot;name&quot;: &quot;FUBAR&quot;,
        &quot;day&quot;: &quot;Thurday&quot;,
        &quot;reason&quot;: &quot;Ut enim&quot;,
        &quot;comment&quot;: &quot;ad minim veniam, quis nostrud exercitation&quot;
    },
    {
        &quot;id&quot;: 3,
        &quot;date&quot;: &quot;2/3/2023&quot;,
        &quot;name&quot;: &quot;BUZBAF&quot;,
        &quot;day&quot;: &quot;Friday&quot;,
        &quot;reason&quot;: &quot;Duis aute irure&quot;,
        &quot;comment&quot;: &quot;dolor in reprehenderit in voluptate velit esse cillum dolore&quot;
    },
    {
        &quot;id&quot;: 4,
        &quot;date&quot;: &quot;2/4/2023&quot;,
        &quot;name&quot;: &quot;FOO&quot;,
        &quot;day&quot;: &quot;Saturday&quot;,
        &quot;reason&quot;: &quot;Excepteur sint occaecat&quot;,
        &quot;comment&quot;: &quot;cupidatat non proident, sunt in culpa qui officia&quot;
    },
    {
        &quot;id&quot;: 5,
        &quot;date&quot;: &quot;2/5/2023&quot;,
        &quot;name&quot;: &quot;BAR&quot;,
        &quot;day&quot;: &quot;Sunday&quot;,
        &quot;reason&quot;: &quot;Sed ut perspiciatis&quot;,
        &quot;comment&quot;: &quot;unde omnis iste natus error sit voluptatem accusantium doloremque laudantium&quot;
    }
]);
&lt;/script&gt;
  
&lt;style scoped&gt;
table {
    border-collapse: collapse;
    border: 1px solid #FF0000;
}

table td,
th {
    border: 1px solid #FF0000;
    padding: 2px 10px;
}
&lt;/style&gt;

For the array of components used as table headers:

Date.vue

&lt;template&gt;
  Date
&lt;/template&gt;

Name.vue

&lt;template&gt;
  Name
&lt;/template&gt;

Day.vue

&lt;template&gt;
  Day
&lt;/template&gt;

Reason.vue

&lt;template&gt;
  Reason
&lt;/template&gt;

Comment.vue

&lt;template&gt;
  Comment
&lt;/template&gt;

答案2

得分: -2

您应该更改JSON对象。

const activeDivs = {
  date: { id: 0, text: "日期", checked: true },
  name: { id: 1, text: "姓名", checked: false },
  day: { id: 2, text: "日期", checked: false },
  time: { id: 3, text: "时间", checked: false },
  created: { id: 4, text: "创建时间", checked: false },
  reason: { id: 5, text: "原因", checked: true },
  comment: { id: 6, text: "评论", checked: true },
};

如果您不能更改JSON结构则可以使用find方法来访问单个对象

```javascript
found = Object.values(activeDivs).find((element) => element.text === '日期');

或者您可以使用计算属性来将数组转换为所需的对象。

英文:

You should change the JSON object.

const activeDivs = {
      date: { id: 0, text: &quot;date&quot;, checked: true },
      name: { id: 1, text: &quot;name&quot;, checked: false },
      day: { id: 2, text: &quot;day&quot;, checked: false },
      time: { id: 3, text: &quot;time&quot;, checked: false },
      created: { id: 4, text: &quot;created&quot;, checked: false },
      reason: { id: 5, text: &quot;reason&quot;, checked: true },
      comment: { id: 6, text: &quot;comment&quot;, checked: true },
    };

if you can't change the JSON structure then use find method to access single object.

found = activeDivs.find((element) =&gt; element.text == &#39;date&#39;);

or you can use computed to turn your array to desired object.

huangapple
  • 本文由 发表于 2023年3月3日 22:32:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628367.html
匿名

发表评论

匿名网友

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

确定