views:

610

answers:

3

For those of you trying to understand jqGrid custom edit types ...

I created a multi-checkbox form element, and thought I'd share. This was built using version 3.6.4. If anyone has a more efficient solution, please pass it on.

Within the colModel, the appropriate edit fields look like this:

edittype:'custom'
editoptions:{ custom_element:MultiCheckElem, custom_value:MultiCheckVal, list:'Check1,Check2,Check3,Check4' }

Here are the javascript functions (BTW, It also works – with some modifications – when the list of checkboxes is in a DIV block):

//————————————————————
// Description:
//   MultiCheckElem is the "custom_element" function that builds the custom multiple check box input
//   element. From what I have gathered, jqGrid calls this the first time the form is launched. After
//   that, only the "custom_value" function is called.
//
//   The full list of checkboxes is in the jqGrid "editoptions" section "list" tag (in the options
//   parameter).
//————————————————————
function MultiCheckElem( value, options )
{
   //———-
   // for each checkbox in the list
   //   build the input element
   //   set the initial "checked" status
   // endfor
   //———-
   var ctl = '';
   var ckboxAry = options.list.split(',');

   for ( var i in ckboxAry )
   {
      var item = ckboxAry[i];
      ctl += '<input type="checkbox" ';

      if ( value.indexOf(item + '|') != -1 )
         ctl += 'checked="checked" ';
      ctl += 'value="' + item + '"> ' + item + '</input><br />&nbsp;';
   }

   ctl = ctl.replace( /<br />&nbsp;$/, '' );
   return ctl;
}



//————————————————————
// Description:
//   MultiCheckVal is the "custom_value" function for the custom multiple check box input element. It
//   appears that jqGrid invokes this function the first time the form is submitted and, the rest of
//   the time, when the form is launched (action = set) and when it is submitted (action = 'get').
//————————————————————
function MultiCheckVal(elem, action, val)
{
   var items = '';
   if (action == 'get') // the form has been submitted
   {
      //———-
      // for each input element
      //   if it's checked, add it to the list of items
      // endfor
      //———-
      for (var i in elem)
      {
         if (elem[i].tagName == 'INPUT' && elem[i].checked )
            items += elem[i].value + ',';
      }

      // items contains a comma delimited list that is returned as the result of the element
      items = items.replace(/,$/, '');
   }
   else // the form is launched
   {
      //———-
      // for each input element
      //   based on the input value, set the checked status
      // endfor
      //———-
      for (var i in elem)
      {
         if (elem[i].tagName == 'INPUT')
         {
            if (val.indexOf(elem[i].value + '|') == -1)
               elem[i].checked = false;
            else
               elem[i].checked = true;
         }
      } // endfor
   }

   return items;
}
A: 

Excellent, thanks a lot for sharing.

A: 

Just look for it. Many thanks.

A: 

Thanks for the post. It helped me get through most of the process of creating a multiple checkbox edit type. The first case I'd like to use it on has 46 different options so that gets to be a fairly long column of checkboxes. I'd like to break that up into multiple columns so it doesn't take up as much vertical space on the screen. The first thing I tried was to wrap it all up into a table with 6 columns. It appeared fine on the screen, the appropriate boxes were checked depending on the record being displayed, and they were all arranged neatly in 6 columns. But when I tried editing the data, it would come back as not detecting any change in the data. It turns out, instead of applying the field name to each of the checkboxes, it was applying the name to the table.

I then saw the OP's comment that this could work with the checkboxes in a DIV so I tried that instead of a table. I put them all in a single column wrapped in a DIV, but it had the same problem where the name was being applied to the DIV and not the checkboxes.

Anyone know how to make this work within a table or div so I can lay out these checkboxes in several columns? I'm doing the table or div coding in the MultiCheckElem function, but maybe I need to do that somewhere else? I've been able to work out quite a bit with the jqGrid documentation or this forum, but this one has me stumped. I appreciate any thoughts.