jQuery X-Editable:根据其他选择字段的值更新选择字段

Posted

技术标签:

【中文标题】jQuery X-Editable:根据其他选择字段的值更新选择字段【英文标题】:jQuery X-Editable: Update select field based on value of other select field 【发布时间】:2013-04-22 11:09:49 【问题描述】:

我正在为 jquery 使用 X-Editable 插件。我有两个通过 ajax 动态提供数据的选择字段。这是我的代码:

字段:

<td class="center">
<a href="#" data-name="'.$res['mid'].'" class="zone">'.$zonename.'</a>
</td>
<td class="center">
<a href="#" data-name="'.$res['mid'].'" class="area" data-zona="'.$zoneuid.'">'.$areaname.'</a>
</td>

还有 jQuery:

$('a.zone').editable(
            type: 'select',
            url: '../admin/callbacks/quickEdit.php?t=zone',
            pk: 1,
            showbuttons: true,
            source: function() 
                var result;
                $.ajax(
                    url: '../admin/callbacks/jsonDataList.php',
                    data: t: 'zone',
                    type: 'GET',
                    global: false,
                    async: false,
                    dataType: 'json',
                    success: function(data) 
                        result = data;
                    
                );
                return result;
            ,
            success: function(response, newValue) 
                $(this).parent().siblings('td').children('a.area').data('zona', newValue);
                console.log(response, newValue);
            
        );
        $('a.area').editable(
            type: 'select',
            pk: 1,
            url: '../admin/callbacks/quickEdit.php?t=area',
            showbuttons: true,
            source: function() 
                var result;
                var zona = $(this).data('zona');
                $.ajax(
                    url: '../admin/callbacks/jsonDataList.php',
                    data: t: 'area', zone: zona,
                    type: 'GET',
                    global: false,
                    async: false,
                    dataType: 'json',
                    success: function(data) 
                        result = data;
                    
                );
                return result;
            ,
            success: function(response, newValue) 
                console.log(response);
            
        );

我想要做的是:当他们更改 $('a.zone') 的值时,我希望 $('a.area') 重新加载 ajax 数据。我该怎么做?

【问题讨论】:

【参考方案1】:

我为此挣扎了一会儿。基本上,最终起作用的是

    通过在可编辑项成功函数中触发上游可编辑项的编辑成功来调整我的下游可编辑项的更新, 用自己的克隆替换旧的下游可编辑以摆脱附加的表单(我还没有弄清楚如何直接更新),并且 在替换上调用 editables 函数。

在下面查看。

    var editable_triggered_updates = function (changed_element, newValue)  

        var update_second_editable = function (el_id, newUpstreamValue) 
            var data =  
                id_upstream_editable: "oldUpstreamValue" 
            ;
            if (data[el_id]===undefined) 
                return;
            

            // IE cache workaround
            var n = new Date().getTime();

            $.getJSON(my_lookup_url, t:n, my_get_parameter: newUpstreamValue, function(return_object)

                // Step 2: get rid of old editable form by replacing editable with clone
                $('#id_downstream_editable').replaceWith($('#id_downstream_editable').clone()); 
                $('#id_downstream_editable').attr('data-source', return_object['data-source']);

                // Step 3: call editable on new objects
                $('#id_downstream_editable').editable( 
                    mode: 'inline',
                    ajaxOptions: 
                        dataType: 'json',
                        sourceCache: 'false'
                    
                );
            );
        ;

        update_second_editable(changed_element.id, newValue);
    ;

    !function (triggered_updates)  // editables setup
        $(".editable").editable(
            mode: 'inline', 
            ajaxOptions: 
                dataType: 'json',
                sourceCache: 'false'
            
            success: function(response, newValue) 
                triggered_updates(this, newValue); // Step 1
            ,
        );
    (editable_triggered_updates || console.log); // Step 1 also

【讨论】:

【参考方案2】:

我以前没有使用过该插件,但看了看文档似乎可以:

 $('a.zone').editable(
        type: 'select',
        url: '../admin/callbacks/quickEdit.php?t=zone',
        pk: 1,
        showbuttons: true,
        source: function() 
            var result;
            $.ajax(
                url: '../admin/callbacks/jsonDataList.php',
                data: t: 'zone',
                type: 'GET',
                global: false,
                async: false,
                dataType: 'json',
                success: function(data) 
                    result = data;
                
            );
            return result;
        ,
        success: function(response, newValue) 
            $(this).parent().siblings('td').children('a.area').data('zona', newValue);
            console.log(response, newValue);
        
    );

function loadArea()
    $('a.area').editable(
        type: 'select',
        pk: 1,
        url: '../admin/callbacks/quickEdit.php?t=area',
        showbuttons: true,
        source: function() 
            var result;
            var zona = $(this).data('zona');
            $.ajax(
                url: '../admin/callbacks/jsonDataList.php',
                data: t: 'area', zone: zona,
                type: 'GET',
                global: false,
                async: false,
                dataType: 'json',
                success: function(data) 
                    result = data;
                
            );
            return result;
        ,
        success: function(response, newValue) 
            console.log(response);
        
    );



loadArea();

$('a.zone').on('save', function(e, params) 
    loadArea();
);

【讨论】:

以上是关于jQuery X-Editable:根据其他选择字段的值更新选择字段的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 x-editable 和 jquery 验证插件

使用x-editable更新行中的值后jquery数据表未排序

php 使用jquery的x-editable插件进行Laravel 5 inplace编辑。内联和批量编辑示例。

x-editable插件怎么用

X-editable Datepicker + Bootstrap 3? [关闭]

editable组件_表格行内编辑事件