开发者

How to clone select and set selected option and pass it back as string. (doing as part of custom formatter of jqgrid)

开发者 https://www.devze.com 2023-02-19 01:31 出处:网络
I am using a custom formatter for a column in jqgrid. Code : function getCellContentForFormula(cellValue, opts, rowObject) {

I am using a custom formatter for a column in jqgrid.

Code :

function getCellContentForFormula(cellValue, opts, rowObject) { 
var开发者_C百科 splitResult = "FHLMC 30 YR";
var selectId = opts.colModel.name + ':' + rowObject.coupon+ ':' + 'substituteSelect';
    //      var $selectBnchmark = $('#productSelect').clone().attr("id",selectId).val(splitResult);
var $selectBnchmark = $('#productSelect').clone().attr("id", selectId);
$('#'+selectId+ ' option[value='+splitResult+']').attr('selected', 'selected');
var bnchMarkSelect = $('<span>').append($selectBnchmark).remove().html();
console.log(bnchMarkSelect);
return '<br/>'+ bnchMarkSelect;
    }

The above code is correctly creating the drop down with the options but is not showing the selected value.

Console.log output:

 <select id="valueOfCMPlusTwo:5:substituteSelect" class="prodSelect"> <option  value="">-Select-</option><option value="FHLMC 15 YR">FHLMC 15 YR</option><option value="FHLMC 30 YR">FHLMC 30 YR</option></select>

I am having trouble combining lines 3 and 4, I need the output like :

<select id="valueOfCMPlusTwo:5:substituteSelect" class="prodSelect"> <option value="">-Select-</option><option value="FHLMC 15 YR">FHLMC 15 YR</option><option value="FHLMC 30 YR" selected="selected">FHLMC 30 YR</option></select>

Please help.


It you work with the custom formatter you should just work with strings and not use and jQuery operation. For example if you have HTML fragment represented <select> having "blabla" as the "id" and you want to set new id you can just use string replace method. In the same way you can rewrite your custom formatter.

You current code is much much slowly. You should understand, that every $("#myid") operation is the slower the more elements with id you have on you page. Moreover you use jQuery.clone which has not fixed bugs. So I strictly recommend you simplify the code of your custom formatter and use only string or RegExp operations inside.

0

精彩评论

暂无评论...
验证码 换一张
取 消