2013-03-28 79 views
2

我有一個jqGrid,它需要在編輯時在一行的列中有單選按鈕。 以下是我的代碼:jqGrid自定義編輯類型(單選按鈕列)自定義元素不發射設置事件編輯

function BindPreclosingDocs(response) { 
    var previouslyselectedRow; 
    var preclosingtable = $('#preclosing'); 
    preclosingtable.jqGrid({ 
     datatype: 'jsonstring', 
     datastr: JSON.stringify(response.ServiceModel), 
     colNames: ['', 'Documents Received', 'Comments', 'SubDocument', 'NA'], 
     colModel: [ 
     { name: 'Documents', index: 'Documents', align: 'left', sortable: false, editable: false, width: 240 }, 
     { name: 'DocsReceived', index: 'DocsReceived', align: 'center', sortable: false, editable: true, edittype: 'checkbox', editoptions: { value: "True:False" }, formatter: "checkbox", width: 140 }, 
     { name: 'Comments', index: 'Comments', align: 'center', sortable: false, editable: true, edittype: "textarea", editoptions: { rows: "3", cols: "16" }, width: 180 }, 
     { name: 'SubDocument', index: 'SubDocument', editable: false, width: 1 }, 
     { name: 'NA', index: 'NA', editable: true, formatter: 'dynamicText', width: 150, edittype: 'custom', editoptions: { custom_element: radioelem, custom_value: radiovalue} } 
     ], 
     rowNum: response.ServiceModel.PreClosing.length, 
     pager: '#preclosingpagerdiv', 
     viewrecords: true, 
     sortorder: "asc", 
     sortname: 'Documents', 
     jsonReader: { 
      root: "PreClosing", 
      repeatitems: false, 
      id: 'ConfigId' 
     }, 
     shrinkToFit: false, 
     height: 'auto', 
     grouping: true, 
     groupingView: { 
      groupField: ['SubDocument'], 
      groupColumnShow: [false], 
      plusicon: 'ui-icon-circle-triangle-s', 
      minusicon: 'ui-icon-circle-triangle-n' 
     }, 
     loadComplete: function() { 
      HideGroupHeaders(this);     
     }, 
     onSelectRow: function (id) { 
      preclosingtable.jqGrid('saveRow', previouslyselectedRow, false, 'clientArray'); 
      previouslyselectedRow = SetJQGridRowEdit(id, previouslyselectedRow, preclosingtable); 
     } 
    }); 
    preclosingtable.setGridWidth('710'); 
}; 


//RowSelect 
function SetJQGridRowEdit(id, previousid, grid) { 
    if (id && id !== previousid) { 
     grid.jqGrid('restoreRow', previousid); 
     grid.jqGrid('editRow', id, true); 
     previousid = id; 
     return previousid; 
    } 
}; 

//Build radio button 
function radioelem(value, options) { 
    var receivedradio = '<input type="radio" name="receivednaradio" value="R"'; 
    var breakline = '/>Received<br>'; 
    var naradio = '<input type="radio" name="receivednaradio" value="N"'; 
    var endnaradio = '/>NA<br>'; 
    if (value == 'Received') { 
     var radiohtml = receivedradio + ' checked="checked"' + breakline + naradio + endnaradio; 
     return radiohtml; 
    } 
    else if (value == 'NA') { 
     var radiohtml = receivedradio + breakline + naradio + ' checked="checked"' + endnaradio; 
     return radiohtml; 
    } 
    else { 
     return receivedradio + breakline + naradio + endnaradio; 
    } 
}; 

function radiovalue(elem, operation, value) { 
    if (operation === 'get') { 
     return $(elem).val(); 
    } else if (operation === 'set') { 
     if ($(elem).is(':checked') === false) { 
      $(elem).filter('[value=' + value + ']').attr('checked', true); 
     } 
    } 
}; 

我格式化和unformatter代碼如下我有

dynamicText: function (cellvalue, options, rowObject) { 
     if (cellvalue == 'R') { 
      return 'Received'; 
     } 
     else if (cellvalue == 'N') { 
      return 'NA'; 
     } 
     else { 
      return ''; 
     } 
    } 

$.extend($.fn.fmatter.dynamicText, { 
    unformat: function (cellValue, options, elem) { 
     debugger; 
     var text = $(elem).text(); 
     return text === '&nbsp;' ? '' : text; 
    } 
}); 

的問題是,當我選擇一個行和檢查的編輯按鈕,它不會放火在無線電價值功能。當選中一行時,它會創建單選按鈕,並觸發radiovalue函數。任何幫助,以便我可以設置一個值單選按鈕?!

謝謝

回答

3

我認爲你是對的。在不同的編輯模式下,custom_value回調的使用有所不同。

如果表單編輯使用一些可編輯的列有edittype: 'custom'然後第一custom_element功能(在你的情況下,它radioelem功能)的$.jgrid.createEl內部調用。那麼custom_value將在rowid !== "_empty"(不適用於添加表單)的情況下另行調用。詳情請參閱the lines of code

問題是custom_elementvalue參數。因此,它可以設置自定義控制中的值並呼叫custom_element,並且custom_value的另一個呼叫與"set"不是必需的。

另一種編輯模式(內嵌編輯和單元編輯)只是創建自定義控件。將不會使用"set"參數調用回調custom_value

我確認the documentation關於自定義控件太短。我想你可以刪除部分radiovalue的部分'set'。我認爲下面的代碼將良好的工作太(即使在表單編輯的情況下):

function radiovalue(elem, operation, value) { 
    if (operation === 'get') { 
     return $(elem).val(); 
    } 
} 

一個備註:如果你會嘗試的自定義控件的用途,應該不要忘記使用recreateForm: true選項。作爲在線編輯,表單編輯和搜索中使用自定義控件的示例,您會發現here。您可以在the answer中找到該演示的參考資料。

+0

感謝Oleg的回覆。我沒有在這裏使用窗體edititng。我正在編輯選定行上的內嵌行。我遇到的問題是,一旦我選擇了一行,我就會得到單選按鈕組。現在我選擇值爲'N'的第二個單選按鈕。當選擇其他行時,我保存到clientArray,我期望以前編輯過的行列將NA列值更新爲NA。但它將其更新爲「收到」。即使我沒有從單選按鈕中選擇任何選項,它也會將其更新爲「已收到」。 – fcmaine

+0

@SrinivasPotluri:不客氣!你的問題是「爲什麼自定義元素不能在編輯時觸發設置事件」。我回答你,它只是在表單編輯中被解僱。你在上次評論中提出的問題是*新問題*。我想你有一些問題,因爲你調用'restoreRow'而不是'saveRow',或者只是因爲你在編輯模式中使用單選按鈕而不是使用自定義格式化程序。要理解這個問題,您應該發佈帶​​有測試數據**的新問題**,以及您在何處描述詳細的測試用例,預期結果和演示結果。 – Oleg

+0

我也在jsfiddle上添加了代碼示例[link](http:// jsfiddle。net/srinivaspotluri/xx7Jg/3 /) – fcmaine