2017-10-12 167 views
0

我得到xeditableselect2api調用作爲來源,一切都很好,除了以下內容。xeditable&select2下拉w/ajax源提交後顯示爲空

提交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; 
    }, 
}); 

再次 - 一切正常(數據庫更新,下拉列表填充等),但是<td>獲取與更新"EMPTY"提交下拉後 - 要求刷新頁面以顯示正確的值。

+0

我試過類似的問題/解決方案的鏈接,但沒有工作。 https://stackoverflow.com/questions/28190106/x-editable-putting-empty-after-successful-update – TangoAlee

回答

0

我想出了一個解決方法。我超級泵。

//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;}  
     } 
    }, 
}); 
1

我面臨同樣的問題。我處理這樣的說法:

在X編輯源代碼查找:

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) 
     } 

正如你所看到的,有別的statment,無需任何代碼(只有2條評論)就是這種情況,與我們有一個問題。我的解決辦法是添加缺少的代碼:啓用無標籤模式

(...) 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 
       }; 
      } 
     } 
    } 
}); 
+0

這不適合我 - 它正確地發佈數據到服務器,只是仍然顯示「空」字符串選擇後。 – TangoAlee

+0

那麼... xEditable維護得不好。我認爲,最好的解決方案是直接使用[select2](https://select2.org/) – Yurciu

+0

我同意 - 除了我通常對javascript一無所知,並且具有內聯可編輯功能非常有價值。如果只有另一種選擇。 – TangoAlee