简体   繁体   English

Bootstrap x-editable。以编程方式更改数据类型(删除select2数据类型)

[英]Bootstrap x-editable. Change data type programmatically (Remove select2 data-type)

I'm, using bootstrap x-editable https://vitalets.github.io/x-editable/index.html 我是,使用bootstrap x-editable https://vitalets.github.io/x-editable/index.html

This is my html code: 这是我的HTML代码:

<a href="#" data-name="category" class="addon" data-value="{{$cat->id}}"
   data-pk="{{$cat->id}}" data-type="select2" data-title="Favor llenar los campos"></a>

javascript code: javascript代码:

$('.addon').editable({
    placement: 'bottom',
    mode: 'inline',
    showbuttons: false,
    source: categories,
    select2: {
        width: 200
    },
    display: function (value) {
        var elem = $.grep(categories, function (o) {
            return o.id == value;
        });

        $(this).attr('data-value', value);
        $(this).parent().parent().find('.btnEdit').attr('href', '/wizard_product/' + product_id + '/category/' + value + '/edit');

        return $(this).text(elem[0].text);
    }
});

But. 但。 I want to change programmatically to normal x-editable element without the select2 options. 我想在没有select2选项的情况下以编程方式更改为普通的x-editable元素。

I have tried with jquery to change the data-type attribute of the a element to text, but it does not work. 我已尝试使用jquery将a元素的data-type属性更改为text,但它不起作用。

$('a.addon').attr('data-type', 'text');

Also tried: 还尝试过:

$('a.addon').select2('destroy');

Also tried: 还尝试过:

$('a.addon').editable({type: 'text'});

But neither options work. 但两种选择都不奏效。 select2 is still active. select2仍处于活动状态。 How can I remove select2 options of x-editable? 如何删除x-editable的select2选项? Can you help me please 你能帮我吗

You will have to combine the things that you've tried - destroy the default X-editable instance, change the data-type value, and reinstate X-editable on that element. 您必须组合您尝试过的东西 - 销毁默认的X可编辑实例,更改data-type值,并在该元素上恢复X-editable。

The most simple implementation/example would be: 最简单的实现/示例是:

$('.addon').editable('destroy').data('type', 'text').editable({type: 'text'});

In practice, you'll have to put your other settings back when you re-apply X-editable as text: 实际上,当您重新应用X-editable作为文本时,您将不得不重新设置其他设置:

$('.addon').editable('destroy');
$('.addon').data('type', 'text');
$('.addon').editable({
                placement: 'bottom',
                mode: 'inline',
                showbuttons: false,
                source: categories,
                type: 'text',
                display: function (value) {
                    var elem = $.grep(categories, function (o) {
                        return o.id == value;
                    });
                    $(this).attr('data-value', value);

                    $(this).parent().parent().find('.btnEdit').attr('href', '/wizard_product/' + product_id + '/category/' + value + '/edit');
                    return $(this).text(elem[0].text);
                }
            });

Edit: 编辑:

I've put together a demo that mirrors your setup as best I could tell and it seems to work: 我已经整理了一个演示,它可以反映您的设置,我可以告诉它,它似乎工作:

 var categories = [{ id: 'html', value: 'html', text: 'html' }, { id: 'javascript', value: 'javascript', text: 'javascript' }]; setupSelect2(); $('#useSelect2').click(function() { $('.addon') .data('type', 'select2') .editable('destroy'); setupSelect2(); }); $('#useText').click(function() { $('.addon') .data('type', 'text') .editable('destroy'); setupText(); }); function setupSelect2() { $('.addon').editable({ mode: 'inline', placement: 'bottom', showbuttons: false, source: categories, select2: { width: 200 }, display: function(value) { var elem = $.grep(categories, function(o) { return o.id == value; }); $(this).attr('data-value', value); if (elem[0]) return $(this).text(elem[0].text); } }); } function setupText() { $('.addon').editable({ mode: 'inline', placement: 'bottom', type: 'text', showbuttons: false, source: categories, display: function(value) { var elem = $.grep(categories, function(o) { return o.id == value; }); $(this).attr('data-value', value); if (elem[0]) return $(this).text(elem[0].text); } }); } 
 <script src="https://vitalets.github.io/x-editable/assets/jquery/jquery-1.9.1.min.js"></script> <link href="https://vitalets.github.io/x-editable/assets/select2/select2.css" rel="stylesheet" /> <script src="https://vitalets.github.io/x-editable/assets/select2/select2.js"></script> <link href="https://vitalets.github.io/x-editable/assets/bootstrap300/css/bootstrap.css" rel="stylesheet" /> <script src="https://vitalets.github.io/x-editable/assets/bootstrap300/js/bootstrap.js"></script> <link href="https://vitalets.github.io/x-editable/assets/x-editable/bootstrap3-editable/css/bootstrap-editable.css" rel="stylesheet" /> <script src="https://vitalets.github.io/x-editable/assets/x-editable/bootstrap3-editable/js/bootstrap-editable.js"></script> <link href="https://vitalets.github.io/x-editable/assets/select2/select2-bootstrap.css" rel="stylesheet" /> <h3>Select 2</h3> <a href="#" class="addon" data-type="select2" data-pk="1" data-title="Enter tags"></a> <br> <br> <button id="useSelect2" class="btn btn-default">Use Select2</button> <button id="useText" class="btn btn-default">Use Text</button> 

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

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