简体   繁体   English

如何在 laravel livewire 中应用 select2,在多个刀片文件中有 1 个组件

[英]How to apply select2 in laravel livewire with 1 component in multiple blade file

**Component File**
class PurchaseRequests extends Component
}

 public $show = 'index';

 public function create()
 {
    $this->reset();
    $this->method = 'store';
    $this->show = 'form';
 }

 public function render()
 {
   return view('livewire.purchase_requests.'.$this->show);
 }
}

Blade File index.blade.php form.blade.php刀片文件索引.blade.php form.blade.php

I tried to apply the select2 in index.blade.php and its working but when i try it to form.blade.php its not working.我尝试在 index.blade.php 中应用 select2 及其工作,但是当我尝试将其应用于 form.blade.php 时它不工作。 i dont know whats the problem.我不知道是什么问题。

I don't know what could be causing the issue once I can't see any code.一旦我看不到任何代码,我不知道是什么导致了这个问题。 But for my experience with select2 and Livewire I'm going to share with you my codding.但是对于我在 select2 和 Livewire 方面的经验,我将与您分享我的编码。 This is a simple example for one component and the blade related.这是一个组件和刀片相关的简单示例。 component零件

public $models;
public $selected_item;
protected $listeners = ['selected_select2_item'];  // this listen for the select2 emitted event 

public function hydrate()  // hydrate the select2 element on every re-render
{
   $this->emit('select2');
}

public function mount()
{
  $this->models = Model::all();
}

public function render()
{
   //......
}

public function selected_select2_item($value)  // function load from listener
{
   dd($value); 
}

and in the blade component在刀片组件中

@section('content')
//......

<select id="select2_element" wire:change="$emit('selected_select2_item',$event.target.value)">
  @foreach($models as $model)  
    <option value="{{$model->id}}">{{$model->name}}</option>
  @endforeach
</select>

//......
@endsection

@section('custom_scripts')
  <script>
    $(document).ready(function() {

            window.initSelectDrop=()=>{
                $('#select2_element').select2({
                    placeholder: '-- Select model--',
                    allowClear: true});
            }

            initSelectDrop();

            $('#select2_element').on('change', function (e) {
                livewire.emit('selected_select2_item', e.target.value)
            });

            window.livewire.on('select2',()=>{
                initSelectDrop();
            });

        }); 
  </script>
@endsection

Hope this code can help you like it did it before with me;希望这段代码可以帮助你喜欢它以前和我一起做的事情; ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel select2 在刀片文件上设置选定选项 - Laravel select2 set selected option on blade file 无法显示刀片文件中的分页链接 - Laravel Livewire - Can't display the pagination links in blade file - Laravel Livewire Livewire/Blade 组件中的渲染变量 - Render variable in Livewire/Blade component 如何使用select2和laravelcollective填充视图laravel刀片中的其他字段 - How to fill other field in view laravel blade using select2 and laravelcollective 默认情况下如何使用select2和Laravel选择一些多个选项 - How to select some multiple options by default using select2 and Laravel Laravel 刀片多选。 如何选择数据透视表中的记录? - Laravel blade multiple select. How to select records in the pivot table? 如何使用 Laravel 7 中的 select2 multiple 将多个值插入 DB - How to insert multiple values to DB using select2 multiple in Laravel 7 Laravel livewire - 如何将用户的地理坐标传递给 livewire 组件 - Laravel livewire - how to pass geo coordinates of user to livewire component 如何在刀片模板laravel中使用单个文件vue组件? - How to use a single file vue component in blade template laravel? Laravel Livewire - 如何强制刷新父组件? - Laravel Livewire - How to force parent component refresh?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM