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

时间:2013-04-28 20:38:00

标签: jquery ajax jquery-plugins twitter-bootstrap

我正在使用j -ery的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数据。我怎么能这样做呢?

2 个答案:

答案 0 :(得分:2)

我有点挣扎。基本上,最终工作的是

  1. 通过在editables success函数中触发编辑上游可编辑,成功调整下游可编辑的更新,
  2. 将旧的下游可编辑替换为自身的克隆,以删除附加的表单(我还没想出如何直接更新),并且
  3. 在替换上调用editables函数。
  4. 请在下方查看。

        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
    

答案 1 :(得分:0)

我以前没有使用过这个插件,但是看一下这些文档似乎可行:

 $('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();
});