简体   繁体   English

使用x-editable切换select2

[英]Toggle select2 using x-editable

I toggle editable by clicking on a different (a pencil icon) DOM object. 我通过单击另一个(铅笔图标)DOM对象来切换可编辑状态。 This works fine for simple text values, now I need an select box and preferbly the select2 dropdown. 这对于简单的text值很好用,现在我需要一个select框,最好是select2下拉列表。

On all other fields and the regular select dropdown I use the following: 在所有其他字段和常规选择下拉菜单中,我使用以下命令:

$('.edit-last_name').click(function(e){    
  e.stopPropagation();
  $('#last_name').editable('toggle');
});

For the select 2 dropdown I need to pass aditional params to editable, but I have no idea how to do it, the code below does not work. 对于select 2下拉菜单,我需要将附加参数传递给edit,但是我不知道该怎么做,下面的代码不起作用。 (the element is not triggerd) (该元素未触发)

$('.edit-country').click(function(e){    
    e.stopPropagation();
    $('#country').editable({
      toggle: 'show', 
      select2: {
        width: 200,
        placeholder: 'Select country',
        allowClear: true
      }
    })
});

Here is the html: 这是html:

<div class="row edit-line">
    <div class="col-md-3 col-xs-4">
        <strong>@lang('user.country')</strong>
    </div>
    <div class="col-md-6 col-xs-8 text-right">
        <span id="country" class="edit-field" data-type="select" data-source="{{ route('countries') }}" data-value="{{ $user->country }}" data-pk="{{ $user->id }}" data-url="{{ action('UsersController@update') }}" data-title="@lang('user.country')">{{ Countries::getOne($user->country, App::getLocale(), 'cldr'); }}</span>
    </div>
    <div class="col-md-3 text-muted hidden-xs">
        <a href="#" class="edit-country text-muted text-no-underline small"><i class="fa fa-pencil"></i> @lang('general.edit')</a>
    </div>
</div>

In fact it has notting to do specifically with select2, I just don't know how to pass params to editable. 实际上,它并没有专门针对select2,我只是不知道如何将params传递给editable。

You should bind editable to your input outside the click event. 您应该将可编辑内容绑定到click事件之外的输入。 When the user clicks you can then toggle the editable. 用户单击时,便可以切换可编辑内容。

Based on this question: X - editable input editable on click on other element , your javascript would be: 基于以下问题: X-可编辑输入,单击其他元素时可编辑 ,您的JavaScript将是:

$(document).ready(function() {
    // in case you don't want the modal
    $.fn.editable.defaults.mode = 'inline';

    // bind x-editable on DOM ready
    $('#country').editable({
        source: [
            {id: 'gb', text: 'Great Britain'},
            {id: 'us', text: 'United States'},
            {id: 'ru', text: 'Russia'}
        ],
        // change this from 'show' to 'manual'
        toggle: 'manual',
        select2: {
            width: 200,
            placeholder: 'Select country',
            allowClear: true
        }
    });

    // on element click
    $('.edit-country').click(function(e){
        e.stopPropagation();
        // toggle x-editable
        $('#country').editable('toggle');
    });
});

Here is a working jsfiddle . 这是一个工作的jsfiddle

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM