如何在此包“victorybiz/laravel-tel-input”的Livewire组件中填充数据?

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

How can populate data in livewire component of this package "victorybiz/laravel-tel-input"

问题

I am using this package to show phone input with tel, in my livewire php class I am setting data that I want to populate in the component this package provides

我正在使用这个包来展示电话输入,我在我的Livewire PHP类中设置了数据,我想在这个包提供的组件中填充这些数据。

public function mount()
{
    $this->data['country_code'] = str_replace("+", "", optional(user())->country_code);
    $this->data['phone_no'] = str_replace($this->data['country_code'], '', optional(user())->phone_no);

    $this->data['full_no'] = optional(user())->phone_no;
}

Here is my blade file and i have added a livewire component which they have suggested in the docs

这是我的Blade文件,我已经添加了一个Livewire组件,这是文档中建议的方式:

<x-tel-input
    wire:model="phone"
    id="phone"
    name="phone"
    class="form-input"
/>
<input wire:model="phone_country" type="hidden" id="phone_country" name="phone_country">

Here is the documentation link Laravel Telephone Input

这是文档链接 Laravel Telephone Input

英文:

I am using this package to show phone input with tel, in my livewire php class I am setting data that I want to populate in the component this package provides

public function mount()
{
    $this-&gt;data[&#39;country_code&#39;] = str_replace(&quot;+&quot;,&quot;&quot;,optional(user())-&gt;country_code);
    $this-&gt;data[&#39;phone_no&#39;] = str_replace($this-&gt;data[&#39;country_code&#39;],&#39;&#39;,optional(user())-&gt;phone_no);

    $this-&gt;data[&#39;full_no&#39;] = optional(user())-&gt;phone_no;
}

Here is my blade file and i have added a livewire component which they have suggested in the docs

&lt;x-tel-input
            wire:model=&quot;phone&quot;
            id=&quot;phone&quot;
            name=&quot;phone&quot;
            class=&quot;form-input&quot;
        /&gt;
        &lt;input wire:model=&quot;phone_country&quot; type=&quot;hidden&quot; id=&quot;phone_country&quot; name=&quot;phone_country&quot;&gt;

Here is the documentation link Laravel Telephone Input

答案1

得分: 0

以下是您要翻译的内容:

在 blade 文件中,通过以下方式我已经实现了我想要做的事情:

<x-tel-input
    wire:model="users.phone_no"
    id="phone_no"
    name="phone_no"
    class="form-input numeric form-control form-control-lg"
/>
<input wire:model="users.country_code" type="hidden" id="country_code" name="country_code">

在 PHP 类的 mount 函数中,可以像这样传递值:

public $users = [];
public function mount()
{
    $this->users['country_code'] = optional(user())->country_code;
    $this->users['phone_no'] = optional(user())->phone_no;
}

如果您想设置 Livewire 的公共属性,可以这样做:

@push('dashboard-js')
<script>
    phone_no.addEventListener('telchange', function(e) {
        @this.set("users.phone_no", e.detail.validNumber);
        @this.set("users.country_code", e.detail.dialCode);
    });
</script>
@endpush
英文:

By doing following I have achieved what I want to do
in a blade file

&lt;x-tel-input
            wire:model=&quot;users.phone_no&quot;
            id=&quot;phone_no&quot;
            name=&quot;phone_no&quot;
            class=&quot;form-input numeric form-control form-control-lg&quot;
        /&gt;
        &lt;input wire:model=&quot;users.country_code&quot; type=&quot;hidden&quot; id=&quot;country_code&quot; name=&quot;country_code&quot;&gt;

and in a PHP class mount function pass the values like this

public $users = [];
public function mount()
{
    $this-&gt;users[&#39;country_code&#39;] = optional(user())-&gt;country_code;
    $this-&gt;users[&#39;phone_no&#39;]     = optional(user())-&gt;phone_no;
}

and also if you want to set livewire public property you can do this

@push(&#39;dashboard-js&#39;)
&lt;script&gt;
    phone_no.addEventListener(&#39;telchange&#39;, function(e) {
        @this.set(&quot;users.phone_no&quot;, e.detail.validNumber);
        @this.set(&quot;users.country_code&quot;, e.detail.dialCode);
    });
&lt;/script&gt;
@endpush

huangapple
  • 本文由 发表于 2023年5月22日 14:08:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303414.html
匿名

发表评论

匿名网友

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

确定