【问题标题】:How to organize this logic? (Create and update based on the radio button selection)如何组织这个逻辑? (根据单选按钮选择创建和更新)
【发布时间】:2018-02-22 14:30:34
【问题描述】:

我有一个包含一些单选按钮的表单,在单选按钮下方,有一些输入字段

一个单选按钮是静态的,“创建新的注册类型”,单击时表单字段应重置,以便用户可以在字段中插入新值,以便他可以为会议存储新的注册类型。

其他单选按钮是动态的,对应于数据库中特定会议的注册类型。当检查这些单选按钮中的每一个时,我想在表单字段中显示该特定检查的注册类型的信息(名称,容量等),以便可以更新每个注册类型。

怀疑

你知道如何组织这个逻辑吗?我在这方面没有成功。页面需要刷新吗?或者注册类型可以一次全部加载,然后使用 jquery 在每个单选按钮选择上显示每个注册类型?还是没有?

编辑注册表单:

<form method="post" class="clearfix">
      <div class="form-row">
        <div class="form-group col col-lg-6">
          <label for="registration_types">Registration Types</label>

          @foreach($registrationType as $rtype)
          <div class="form-check">
            <input class="form-check-input" type="radio" name="radiobutton" id="{{$rtype->id}}" value="option1">
            <label class="form-check-label" for="exampleRadios1">
              {{$rtype->name}}
            </label>
          </div>
          @endforeach

          <div class="form-check">
            <input class="form-check-input" type="radio" name="radiobutton" id="create_registration_type" checked value="option1">
            <label class="form-check-label" for="exampleRadios1">
              Create new registration type
            </label>
          </div>
        </div>
      </div>

      <div class="form-group">
        <label for="registration_type_name">Registration Type Name</label>
        <input type="text" required class="form-control" value="{{ $rtype->nome }}" name="registration_type_name" id="registration_type_name">
      </div>
      <div class="form-group">
        <label for="registration_type_capacity">Capacity</label>
        <input type="number" min="1"
               required class="form-control" value="{{ $rtype->capacity }}"
             name="registration_type_capacity" id="registration_type_capacity">
      </div>
      <!-- more registration type info fields -->
      </div>
    <input type="submit" class="btn btn-primary" value="Store registration type"/>
    <input type="submit" class="btn btn-primary" value="Update registration type"/>
    </form>

RegistrationType 控制器:

    class RegistrationTypeController extends Controller
    {
        public function edit($id)
        {
            $conference = Conference::find($id);
            $registrationType = RegistrationType::where('conference_id', $id)->get();


            return view('rtypes.edit')->with('conference', $conference)->with('registrationType', $registrationType);
        }
        public function update(Request $request, $id){
            $this->validate($request, [
            'registration_type_name' => 'required|string',
            'registration_type_description' => '',
        ]);

        $conference = Conference::find($id);

        $regType = RegistrationType::find($conference->id);
        $regType->name = $request->registration_type_name;

        $regType->description = $request->registration_type_description;
        ...

        $regType->save();

        Session::flash('success','Registration type updated with success');

        return redirect()->back();
       }

    }

注册类型模型

class RegistrationType extends Model
{
    protected $fillable = [
        'name', 'description', 'capacity', 'conference_id'
    ];

    public function confenrence(){
        return $this->belongsTo('App\Conference');
    }
}

JS:

 <script>
        $('#create_registration_type').click(function(){
            $('input[type="text"]').val('');
            $('input[type="number"]').val('');
            $('input[type="submit"]').val('Store');
        });
    </script>

【问题讨论】:

  • 您是如何尝试问题的第二部分的?你有相关的代码吗?
  • 在问题描述中添加控制器代码

标签: php jquery laravel


【解决方案1】:

如果我正确理解了你的问题,那么这个疑问与 Laravel 或后端无关,它只是一个前端实现。

如果您想根据所选单选更改文本输入中的默认值,我可以考虑 3 个不同的选项:

1 - 您可以刷新页面。我认为需要更少的 JS,但不是最好的解决方案。

2 - 您可以为每个收音机加载所有不同的默认值并将它们保存在 JS 数组中,为每个收音机提供相应的数组索引作为数据值(即 data-index="1"),然后您可以选择单选按钮时,使用 jQuery 加载输入框中的值。

3 - 每次选择电台时使用 AJAX 加载默认值。这可以通过创建您已经加载的值的缓存来改进,这样您就不会重复 AJAX 调用,以防用户选择无线电 A,然后选择无线电 B,然后再选择无线电 A。

就个人而言,我认为第二种选择是最好的。除非您有大量单选按钮并且一次加载所有值会成为问题。

【讨论】:

  • 感谢您的回答。不同的注册类型已经在 foreach "@foreach($registrationType as $rtype)...@endforeach" 中显示。你能举一个如何存储在 js 数组中的例子吗?因为我没有找到如何与 laravel 和 js 进行交互。是否需要创建一些控制器或使用 RegistrationType 控制器来做到这一点?
  • 要将 PHP 数组转换为 JS,您只需将其编码为 JSON。您可以使用 PHP 的函数 json_encode() 在您的控制器或刀片文件中执行此操作(如果您在控制器中执行此操作会更好)。因此,在您的控制器中,您可以在 return view('rtypes.edit') 行中添加 -&gt;with('registrationTypeJson', json_encode($registrationType)),然后在刀片文件的 JS 代码中添加 var registrationTypes = {{ $registrationTypeJson }},因为它已经编码。
  • 谢谢,但我得到了一个错误:未定义的变量:registrationTypeJson(查看:/Users/johnw/projects/proj/resources/views/index.blade.php)。你知道为什么吗?我只是尝试显示注册类型,例如: var registrationTypes = {{ $registrationTypeJson }} alert(registrationTypes);.
  • 您是否添加了-&gt;with('registrationTypeJson', json_encode($registrationType))?如果 Blade 找不到那个变量,你可能错过了。
  • 是的,编辑方法有那部分: public function edit($id) { $conference = Conference::find($id); $registrationType = RegistrationType::where('conference_id', $id)->get(); return view('rtypes.edit') ->with('conference', $conference) ->with('registrationTypeJson', json_encode($registrationType)); }
猜你喜欢
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
  • 2020-10-29
  • 1970-01-01
  • 2014-10-27
  • 2012-07-08
  • 1970-01-01
相关资源
最近更新 更多