将v-model设置为数组中的键(v-model.key =" someArray")

时间:2017-07-28 09:31:54

标签: html json laravel vue.js vuejs2

我正在做一个过滤页面,我需要存储过滤器名称和过滤器值, 为此,我有一个这样的结构:

data: { attributes: { checkboxes: [], strings: { keys: [], values: [] }, ...

并且HTML看起来像那样(顺便说一下我也使用Laravel Blade,但这里没有相关性):

...
@if ($atribut->type == 'checkbox')
   <label class="btn btn-default" for="{{ $atribut->tag }}"> {{ $nom }} <input v-model="attributes.checkboxes" class="badgebox" id="{{ $atribut->tag }}" type="checkbox" value="{{ $atribut->id }}"><span class="badge">&check;</span></label>
   <br>
   <br>
@endif
@if ($atribut->type == 'string')
  @php $nom = obj_array_find(DB::table('literals')->get(), $atribut->nom, 'id')->cat @endphp
  <input type="hidden" v-model="attributes.strings.keys" value="{{ $atribut->id }}">
  {!! Form::text($atribut->tag, null, ['class' => 'form-control', 'placeholder' => $nom, 'v-model' => 'attributes.strings.values']) !!}
  <br>
@endif
...

问题是,是否有可能有类似的东西:

<input type="hidden" v-model.key="attributes.strings" value="{{ $atribut->id }}">
{!! Form::text($atribut->tag, null, ['class' => 'form-control', 'placeholder' => $nom, 'v-model.value' => 'attributes.strings]) !!}

2 个答案:

答案 0 :(得分:0)

要回答您的具体问题,v-model将绑定到一个键值对,您不需要分配键。

构造输入绑定的方式输入框将对象绑定到您的值字段,您将在输入框中看到[对象对象]。

我创建了这个jsfiddle,它向您展示了如何绑定到模型。我假设您的数据以下面的列表格式建模。

attributes: [
    {checkboxes: [1], strings: { keys: [1], values: ['tomato'] }},
    {checkboxes: [2], strings: { keys: [2], values: ['potato'] }}
]

我还为您提供了一个json对象来监视这两个键中每个键的数据更新。这将帮助您调试实际尝试的位置。

JSFiddle没有办法渲染laravel刀片,所以这里是刀片版本:

<input v-for="attribute in attributes" type="hidden" v-model="attribute.strings.values" value="1">
{!! Form::text('bam', null, ['v-for' => 'attribute in attributes', 'class' => 'form-control', 'placeholder' => 'boom', 
        'v-model' => 'attribute.strings.values']) !!}

如果你在问题的开头提到过,你想要创建一个过滤器(我假设一个自动完成类型对象?),那么这就不可能了。

希望这有帮助。

答案 1 :(得分:0)

对不起我的坏解释,但我终于找到了解决方案,

我的意思是我需要知道一个大的输入列表,哪个值与每个输入相关联,最好的方法就是这个:

data: { 
  filtres: {
    transaccions: {}
  }
}

(如果您使用刀片渲染)

this.filtres.transaccions.someInputKey: SomeInputValue

因为这样你得到了这个:

$atribut->tag

其中密钥由using (response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) { throw new ApplicationException("error code " + response.StatusCode.ToString()); } } //process the response stream ..(json , html , etc.. ) Encoding enc = System.Text.Encoding.GetEncoding(1252); StreamReader loResponseStream = new StreamReader(response.GetResponseStream(), enc); 给出,当然值将由输入本身给出