【问题标题】:How to properly show in the frontend the checkboxes checked or not based on what is stored in DB?如何根据数据库中存储的内容在前端正确显示选中或未选中的复选框?
【发布时间】:2018-09-01 14:23:14
【问题描述】:

我有一个包含一些问题/字段(电子邮件、电话号码等)的表单,然后用户可以选择是否应为会议存在的注册类型包含该字段,以及该字段是否应通过复选框对每个注册类型强制或不强制。

根据选中的复选框,将信息插入到具有以下结构的数据透视表“registration_type_questions”中:registration_type_id、question_id、必需。

数据库暂时是这样的:

registration_type_id  question_id  required
    2                   1          1    (include quetion 1 in rt02 and is mandatory)
    1                   2          1    (include quetion 2 in rt01 and is mandatory)
    1                   1          1    (include quetion 1 in rt01 and is mandatory)    

怀疑

我想根据数据库中的内容在前端显示选中或未选中的复选框。你知道如何根据这个 db 值检查前端的复选框吗?

使用该数据库信息,前端应如下所示:

表格:

<table class="table table-striped table-responsive-sm">
    <thead>
    <tr>
        <th scope="col">Questions</th>
        <th scope="col">Include for registration type</th>
        <th scope="col">Mandatory</th>
    </tr>
    </thead>
    <tbody>
    @foreach($question as $q)
        <tr>
            <td>{{$q->question}}</td>
            <td>
                @foreach($registration_types as $rtype)
                    <div class="form-check">
                        <input autocomplete="off" name="include[{{ $q->id }}][{{ $rtype->id }}]" class="form-check-input {{$rtype->name}}" type="checkbox" value="1" id="{{$rtype->id}}">
                        <label class="form-check-label" for="exampleRadios1">
                            {{$rtype->name}}
                        </label>
                    </div>

                @endforeach
            </td>
            <td>
                @foreach($registration_types as $rtype)
                    <div class="form-check">
                        <input autocomplete="off"  name="mandatory[{{ $q->id }}][{{ $rtype->id }}]"
                               class="form-check-input mandatorycheckbox" type="checkbox" value="1" id="{{$rtype->id}}">
                        <label class="form-check-label" for="exampleRadios1">
                            for the registration type "{{$rtype->name}}"
                        </label>
                    </div>
                @endforeach
            </td>
        </tr>
    @endforeach

    </tbody>
</table>

问题模型

class Question extends Model
{

    public function registration_type(){
        return $this->belongsToMany('App\RegistrationType', 'registration_type_questions');
    }
}

注册类型模型

class RegistrationType extends Model
{
    public function conference(){
        return $this->belongsTo('App\Conference');
    }

    public function questions(){
        return $this->belongsToMany('App\Question', 'registration_type_questions');
    }
}

会议模型

class Conference extends Model
{
   // A conference has many registration types
    public function registrationTypes(){
        return $this->hasMany('App\RegistrationType', 'conference_id');
    }
}

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    根据 DB 值添加或删除 checked 属性。

    @foreach($registration_types as $rtype)
      <div class="form-check">
          <input autocomplete="off"
                 name="include[{{ $q->id }}][{{ $rtype->id }}]" 
                 class="form-check-input {{$rtype->name}}" 
                 type="checkbox" 
                 id="{{$rtype->id}}"
                 {{ array_search($rtype->id, $q->registration_type->pluck('id')->all()) !== FALSE
                    ? 'checked' : '' }}>
          <label class="form-check-label" for="exampleRadios1">
            {{$rtype->name}}
          </label>
      </div>
    @endforeach
    

    出于性能考虑,您可能已经从控制器准备了$q-&gt;registration_type-&gt;pluck('id')-&gt;toArray(),因此您不必为每种注册类型运行这些函数。

    【讨论】:

    • 感谢它对注册类型复选框起作用,您还知道如何为强制性复选框做些什么吗?
    【解决方案2】:

    您需要在表单中添加:

    <input autocomplete="off"  
           name="mandatory[{{ $q->id }}][{{ $rtype->id }}]"
           class="form-check-input mandatorycheckbox" 
           type="checkbox" 
           value="1" 
           id="{{$rtype->id}}"
           {{$rtype->required==1?" checked":""}}>
    

    您意识到未勾选的单选框/复选框不会随表单一起提交。这意味着您需要先检查它们是否存在 (isset)。

    我注意到的另一个问题。在您给出的数据示例中,当您更新 registration_type_id of 1 时,它将更新哪个记录(此代码可能在其他地方未提供)。

    【讨论】:

    • 谢谢,对不起,但我不正确。相对于第一点,用户可以看到哪些复选框被选中并根据他的需要进行更改并提交表单。另一点,我不明白是什么问题。你能解释清楚吗?
    【解决方案3】:

    在您的控制器中将关系值存储到变量并将其传递给视图。

    foreach($question as $ques) {
        foreach($ques->registration_type as $regType) {
            $optional[$ques->id][$regType->id] = 1;
    
            if($regType->pivot->required == 1) {
                $required[$ques->id][$regType->id] = 1;
            }
        }
    }
    

    $optionalrequired 传递给视图。与$question

    在您的视图中检查当前复选框是否在变量中定义了关系,然后将复选框属性设置为“checked”。

    @foreach($question as $q)
        <tr>
            <td>{{$q->question}}</td>
            <td>
                @foreach($registration_types as $rtype)
                    <div class="form-check">
                        <input autocomplete="off" name="include[{{ $q->id }}][{{ $rtype->id }}]" 
                            class="form-check-input {{$rtype->name}}" type="checkbox" value="1" id="{{$rtype->id}}" {{ !empty($optional[$q->id][$rtype->id]) ? "checked" : "" }}>
                        <label class="form-check-label" for="exampleRadios1">
                            {{$rtype->name}}
                        </label>
                    </div>
                @endforeach
            </td>
            <td>
                @foreach($registration_types as $rtype)
                    <div class="form-check">
                        <input autocomplete="off"  name="mandatory[{{ $q->id }}][{{ $rtype->id }}]"
                           class="form-check-input mandatorycheckbox" type="checkbox" value="1" id="{{$rtype->id}}" {{ !empty($required[$q->id][$rtype->id]) ? "checked" : "" }}>
                        <label class="form-check-label" for="exampleRadios1">for the registration type "{{$rtype->name}}"</label>
                    </div>
                @endforeach
            </td>
        </tr>
    @endforeach
    

    【讨论】:

    • 谢谢,但它不能正常工作。在数据库中,目前存在的两个问题(电子邮件和电话)和目前存在的两种注册类型(一般和加号)都需要。但在前端,只有注册类型“加号”复选框被选中。
    • 我在 QuestionController 中有返回视图的编辑方法,我传递给视图,如:return view('questions.edit') ->with('conference', $conference) -> with('registration_types', $registrationTypes) ->with('registration_types_names', $registrationTypeName) ->with('question', $question) ->with('optional', $optional) ->with('required', $必需);
    • 这个:数组:2 [▼ 1 => 数组:1 [▼ 2 => 1] 2 => 数组:1 [▼ 2 => 1]]
    • 再次感谢,它的工作!你能解释一下这个foreach吗?我没有正确理解它是如何工作的。 " foreach($ques->registration_type as $regType) { $optional[$ques->id][$regType->id] = 1; if($regType->pivot->required == 1) { $required[ $ques->id][$regType->id] = 1; } }".
    • 使用 foreach 循环,您的数据库的精确副本被复制到二维数组 optionalrequired。数组$optional[question_id][registration_type_id]是以这种格式创建的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2019-11-10
    • 2021-08-23
    相关资源
    最近更新 更多