We help IT Professionals succeed at work.
Get Started

jQuery jqGrid cannot get custom formatter radio buttons value

1,628 Views
Last Modified: 2016-01-23
I've created a custom formatter to render a pair of radio buttons for a bool field in jqGrid using the radioButtonFormatter function below.

Also have radio button markup in the myelem function below which is the editoptions:custom_element call to retain the formatting when user clicks in the cell (but not directly on a radio button).

This much works, but if user then clicks out of the cell, the myvalue function which is wired to editoptions:custom_value does not return the value, in fact the 'value' parameter being passed in with 'elem' and 'operation' is empty if I alert it out... So we end up having values reset to 0,no when they should'nt be.

Cannot figure out why the value is not available in the myvalue function.


	
								
case 6: //Two Options

elType = "custom";
format = radioButtonFormatter;
objColumn = { Type: rData.new_Type.Value, name: rData.new_FieldName, width: rData.new_Width, align: align, editable: editable, bulkeditable: rData.new_bulkeditable, edittype: elType,editoptions:{custom_element: myelem, custom_value: myvalue}, formatter: format };

Open in new window


	function myelem (value, options) {
		  
		  var elemStr = '';
		  
		  var _position = value.indexOf('checked=""') + 18;  
		  var _value = value.substr(_position, 1);
		  		  
		  if (_value == '0'){

				elemStr = '<div class="radioButtons">' + 
								'<input type="radio" name="' + options.id + '" value="0" checked />Inelg ' + 
								'<input type="radio" name="' + options.id + '" value="1" />Elg' + 
							'</div>';		  
		  }
		  else if (_value == '1'){
				elemStr = '<div class="radioButtons">' + 
								'<input type="radio" name="' + options.id + '" value="0" />Inelg ' + 
								'<input type="radio" name="' + options.id + '" value="1" checked />Elg' + 
							'</div>';			  
		  }
		  		  
          // return DOM element from jQuery object
          return $(elemStr)[0];

	}
		
	function myvalue(elem, operation, value) {						
		if(operation === 'get') {
		   alert(value);
		   return $(elem).val();
		} else if(operation === 'set') {
		   $('input',elem).val(value);
		}
	}	

Open in new window


    function radioButtonFormatter(cellvalue, options, rowObject) {

        var radioMarkup = "";
        var dynamicName = options.colModel.name + '_' + options.rowId;
        var disabled = "";

        if (options.colModel.editable == false) {
            disabled = 'disabled';
        }

            if (cellvalue == true) {
                radioMarkup = '<div class="radioButtons"><input type="radio" name="' + dynamicName + '" value="0" onclick="cancelEdit(\'' + options.rowId + '\'); onCellExit(' + options.rowId + ', \'' + options.colModel.name + '\',0 , ' + options.rowId + ', ' + options.pos + ' )" ' + disabled + '>Inelg&nbsp;&nbsp;<input type="radio" name="' + dynamicName + '" value="1" onclick="cancelEdit(\'' + options.rowId + '\'); onCellExit(' + options.rowId + ', \'' + options.colModel.name + '\',1 , ' + options.rowId + ', ' + options.pos + ' )" checked ' + disabled + '>Elg</div>';
            }
            else {
                radioMarkup = '<div class="radioButtons"><input type="radio" name="' + dynamicName + '" value="0" onclick="cancelEdit(\'' + options.rowIdd + '\'); onCellExit(' + options.rowId + ', \'' + options.colModel.name + '\',0 , ' + options.rowId + ', ' + options.pos + ' )" checked ' + disabled + '>Inelg&nbsp;&nbsp;<input type="radio" name="' + dynamicName + '" onclick="cancelEdit(\'' + options.rowId + '\'); onCellExit(' + options.rowId + ', \'' + options.colModel.name + '\',1 , ' + options.rowId + ', ' + options.pos + ' )" value="1" ' + disabled + '>Elg</div>';
            }

        return radioMarkup

    }

Open in new window

Comment
Watch Question
Pirate
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE