Laravel 和 Inertia.js - 将复选框设置为选中

Laravel and Inertia.js - Setting checkbox to checked

我正在做一个项目,我在其中传递了关注列表和客户关注列表。我正在尝试检查在编辑视图中具有匹配的客户关注点的所有关注点。这里的想法是,我可以列出所有可能的问题,并预先检查客户的问题。但是我似乎无法让它工作。下面的代码是我正在使用的,但这只会创建 4 个关注列表的副本。如有任何帮助,我们将不胜感激。

ClientController.php

 /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Client  $client
     * @return \Illuminate\Http\Response
     */
    public function edit(Client $client)
    {
        //
        $concerns = Concern::all();
           
        return Inertia::render('Client/Edit', 
            [
                'client' => $client::with('concerns')->get()->first(),
                'concerns' => $concerns,
            ]
        );
    }

Client/Edit.Vue

 <ul id="concerns">
                                                <li v-for="(concern,id) in concerns" v-bind:key="id">
                                                    <span v-for="(cc, id) in client.concerns" v-bind:key="id">
                                                        <span v-if="concern.id === cc.id">
                                                            <input type="checkbox" :name="concern.concern_slug" :value="concern.id" v-model="form.concerns" checked /><label :for="concern.concern">{{ concern.concern }}</label>
                                                        </span>
                                                        <span v-if="concern.id !== cc.id">
                                                            <input type="checkbox" :name="concern.concern_slug" :value="concern.id" v-model="form.concerns" /><label :for="concern.concern">{{ concern.concern }}</label>
                                                        </span>
                                                    </span>


                                                  </li>
                                                </ul>

...
<script>
import BreezeAuthenticatedLayout from '@/Layouts/Authenticated.vue'
import BreezeCheckbox from '@/Components/Checkbox.vue'
import { Head } from '@inertiajs/inertia-vue3';
import VueGoogleAutocomplete from 'vue-google-autocomplete'

export default {
    props: {
        errors: Object,
        concerns: Object,
        client: Object,
        clientConcerns: Object,
    },

    data () {
        return {
            form: {
                first_name: this.client.first_name,
                last_name: this.client.last_name,
                email: this.client.email,
                address: this.client.address,
                city: this.client.city,
                postal_code: this.client.postal_code,
                province: this.client.province,
                preferred_day: this.client.preferred_day,
                preferred_time: this.client.preferred_time,
                preferred_frequency: this.client.preferred_frequency,
                goals: this.client.goals,
                concerns: [],
            },
        }
    },
...
</script>

您对两个 v-for 使用相同的 :key,这可能会导致渲染问题。您也不需要为了设置 checked 属性 而复制 input 元素。您可以为此使用 v-model

  <ul id="concerns">
    <li
      v-for="(concern,id) in concerns"
      v-bind:key="id"
    >
      <span
        v-for="(cc, ccId) in client.concerns"
        v-bind:key="ccId"
      >
        <input
          type="checkbox"
          :name="concern.concern_slug"
          :value="concern.id"
          v-model="concerns" <!-- Not sure if this can be an array of objects -->
        />
        <label :for="concern.concern">{{ concern.concern }}</label>
      </span>
    </li>
  </ul>

要解决此问题,我必须添加一组关注 ID 作为响应的一部分。我在 Edit.vue 中接受它作为道具并将其填写到 concerns.Please 的表单字段中,请参阅下面的更新代码。

ClientController.php

/**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Client  $client
     * @return \Illuminate\Http\Response
     */
    public function edit(Client $client)
    {
        //
        $concerns = Concern::all();

        $client_with_concerns = $client::with('concerns')->get()->first();

        $clientConcerns = collect();

        foreach($client_with_concerns->concerns as $concern) {
            $clientConcerns->push($concern['id']);
        }
        
        return Inertia::render('Client/Edit', 
            [
                'client' => $client::with('concerns')->get()->first(),
                'concerns' => $concerns,
                'clientConcerns' => $clientConcerns,
            ]
        );
    }

Client/Edit.vue

...
    data () {
        return {
            form: {
                first_name: this.client.first_name,
                last_name: this.client.last_name,
                email: this.client.email,
                address: this.client.address,
                city: this.client.city,
                postal_code: this.client.postal_code,
                province: this.client.province,
                preferred_day: this.client.preferred_day,
                preferred_time: this.client.preferred_time,
                preferred_frequency: this.client.preferred_frequency,
                goals: this.client.goals,
                concerns: this.clientConcerns,
            },
        }
    },
...

请注意,我已将 $clientConcerns 添加为响应的一部分,然后将其用作表单

concerns 的值