【问题标题】:xeditable & select2 dropdown w/ajax source displaying empty after submittingxeditable & select2 下拉 w/ajax 源在提交后显示为空
【发布时间】:2017-10-12 19:43:57
【问题描述】:

我已经让xeditableselect2 使用api 作为源调用,除了以下之外,一切都很好。

提交select2下拉列表后,表格的值显示为EMPTY,需要刷新页面才能更新为正确的值。

有谁知道如何将值更新为选定的select2 下拉值?

我的html:

<td class="eo_role"><a href="#" data-pk={{r.pk}} data-type="select2" data-url="/api/entry/{{r.pk}}/"
data-name="eo_role" data-title="Enter EO_role">{{r.eo_role}}</a></td>

这是我的 JS:

$('#example .eo_role a').editable( {
    params: function(params) {  //params already contain `name`, `value` and `pk`
      var data = {};
      data[params.name] = params.value;
      return data;
    },
    source: 'http://localhost:8000/api/eo_role/select_two_data/',
    tpl: '<select></select>',
    ajaxOptions: {
        type: 'put'
        },
    select2: {
        cacheDatasource:true,
        width: '150px',
        id: function(pk) {
            return pk.id;
        },
        ajax: {
            url: 'http://localhost:8000/api/eo_role/select_two_data/',
            dataType: "json",
            type: 'GET',
            processResults: function(item) {return item;}    
        }
    },
    formatSelection: function (item) {
        return item.text;
    },
    formatResult: function (item) {
        return item.text;
    },
    templateResult: function (item) {
        return item.text;
    },
    templateSelection : function (item) {
        return item.text;
    }, 
});

再次 - 一切正常(数据库更新、下拉列表填充等),但 &lt;td&gt; 在提交下拉列表后更新为 "EMPTY" - 需要刷新页面以显示正确的值。

【问题讨论】:

标签: javascript jquery-select2 x-editable


【解决方案1】:

我想出了一个解决方法。我超级兴奋。

//outside of everything, EVERYTHING
//test object is a global holding object that is used to hold the selection dropdown lists
//in order to return the correct text.
var test = {};

$('#example .eo_role a').editable( {
    params: function(params) {  //params already contain `name`, `value` and `pk`
      var data = {};
      data[params.name] = params.value;
      return data;
    },

    //MUST be there - it won't work otherwise.
    tpl: '<select></select>',
    ajaxOptions: {
        type: 'put'
        },
    select2: {

        width: '150px',
        //tricking the code to think its in tags mode (it isn't)
        tags:true,
        //this is the actual function that triggers to send back the correct text.
        formatSelection: function (item) {
            //test is a global holding variable set during the ajax call of my results json.
            //the item passed here is the ID of selected item. However you have to minus one due zero index array.
            return test.results[parseInt(item)-1].text;
        },
        ajax: {
            url: 'http://localhost:8000/api/eo_role/select_two_data/',
            dataType: "json",
            type: 'GET',
            processResults: function(item) {
            //Test is a global holding variable for reference later when formatting the selection.
            //it gets modified everytime the dropdown is modified. aka super convenient.
            test = item;
            return item;}    
        }
    },  
});  

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。我是这样处理的:

    在 x-editable 源代码中查找:

       value2html: function(value, element) {
         var text = '', data,
         that = this;
         if(this.options.select2.tags) { //in tags mode just assign value
           data = value;
           //data = $.fn.editableutils.itemsByValue(value, this.options.select2.tags, this.idFunc);
           } else if(this.sourceData) {
              data = $.fn.editableutils.itemsByValue(value, this.sourceData, this.idFunc);
           } else {
              //can not get list of possible values
              //(e.g. autotext for select2 with ajax source)
           }
    

    如您所见,存在 else 语句,没有任何代码(只有 2 个 cmets),这就是我们遇到问题的情况。我的解决方案是添加缺少的代码:

    (...) else {
          //can not get list of possible values
          //(e.g. autotext for select2 with ajax source)
          data = value;
    }
    

    这是解决未启用标签模式的问题。到目前为止,我没有检测到任何不需要的行为。 示例代码:

    jQuery('[data-edit-client]').editable({
        type: 'select2',
        mode: 'inline',
        showbuttons: false,
        tpl: '<select></select>',
        ajaxOptions: {
            type: 'POST'
        },
        select2: {
            width: 200,
            multiple: false,
            placeholder: 'Wybierz klienta',
            allowClear: false,
            formatSelection: function (item) {
                //test is a global holding variable set during the ajax call of my results json.
                //the item passed here is the ID of selected item. However you have to minus one due zero index array.
                return window.cacheData[parseInt(item)].text;
            },
            ajax: {
                url: system.url + 'ajax/getProjectInfo/',
                dataType: 'json',
                delay: 250,
                cache: false,
                type: 'POST',
                data: {
                    projectID: system.project_id,
                    action: 'getProjectClients',
                    customer: parseInt(jQuery("[data-edit-client]").attr("data-selected-company-id"))
                },
                processResults: function (response) {
                    window.cacheData = response.data.clients;
                    return {
                        results: response.data.clients
                    };
                }
            }
        }
    });
    

    【讨论】:

    • 这对我不起作用 - 它正在将数据正确发布到服务器,但在选择后仍显示“空”字符串。
    • 嗯... xEditable 维护不善。我认为,最好的解决方案是直接使用select2
    • 我同意 - 除非我对 JavaScript 知之甚少,而且拥有内联可编辑功能非常有价值。如果只有另一种选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2018-06-25
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多