2013-01-18 68 views
1

當編輯列爲edittype: selectmultiple: true時,jqGrid將數據作爲逗號分隔值發送到服務器。有可能綁定一些事件到這個特定的列,這將返回一個數組而不是逗號分隔的值?jqGrid,編輯模式,序列化多選

我想避免使用serializeEditData事件,因爲

  1. 這一個是電網專用,不列具體這將是我的一個麻煩 - 我試圖分開列邏輯從網格邏輯
  2. 它聽起來不是一個好主意,將數組轉換爲字符串,並再次將字符串轉換爲相同的數組。

回答

2

我認爲,你將不得不在的情況下使用edittype: 'custom'和提供的editoptionscustom_elementcustom_value方法您的自定義實現。如果自定義輸入元素的值將需要讀取,則jqGrid將使用參數'get'參數調用custom_value方法。你的實現custom_value方法可能會返回大小寫的項目。

重要的是要明白,一般來說,您將需要實現自定義格式化程序,該程序允許顯示多列表輸入的數據數組。幸運的formatter: "select"the line

cellval = cellval + ""; 

其將數組逗號分隔的項目。由於該行數組將通過使用.join(",")轉換爲字符串,並且formatter: "select"成功接受數組作爲輸入。

The demo

enter image description here

演示上面的方法所描述的。它使用下面的代碼:

{ 
    name: "Subcategory", 
    width: 250, 
    formatter: "select", 
    edittype: "custom", 
    editoptions: { 
     value: {"1": "football", "2": "formula 1", "3": "physics", "4": "mathematics"}, 
     custom_element: function (value, options) { 
      return $.jgrid.createEl.call(this, "select", 
       $.extend(true, {}, options, {custom_element: null, custom_value: null}), 
       value); 
     }, 
     custom_value: function ($elem, operation, value) { 
      if (operation === "get") { 
       return $elem.val(); 
      } 
     }, 
     multiple: true 
    } 
} 

在上面的代碼中,我使用$.jgrid.createEl創建多值由現有的jqGrid代碼的選擇。唯一需要做的就是從選項中刪除custom_elementcustom_value,以防止setAttributes中不需要的方法調用。如果將修改setAttributesthe line代碼並且在排除的屬性的列表中包括"custom_value""custom_element",則可以將表達$.extend(true, {}, options, {custom_element: null, custom_value: null})減少爲options

以這種方式,我們得到幾乎相同的<select>與使用edittype: "select"。只有兩點不同:內部

  • <select>元素的edittype: "custom"內部創建將有額外的屬性class="customelement"
  • edittype: "custom"中創建的<select>元素將被包裝(會直接子)<span class="editable">...</span>

這種差異在我們的案例中似乎並不重要。

修訂setAttributes方法的代碼現在固定的jqGrid對gtihub主代碼(參見the suggestionthe commit)。因此,在jqGrid的(如較高4.4.1)的下一個版本一個可能的custom_element代碼減少

custom_element: function (value, options) { 
    return $.jgrid.createEl.call(this, "select", options, value); 
} 

the corresponding demo

+0

我期待你的答案在這裏奧列格!謝謝! – migajek

+0

@migajek:不客氣!在遠程保存數據的情況下,我不測試m建議,但我認爲它的工作方式完全相同。 – Oleg

+0

@ migajek:我寫了小小的「** UPDATED **」部分給我的回答。 – Oleg