Database driven checkboxes

1. Download and install the new rodakino version

You find the new version here: rodakino_0_2_2.tgz

Also look here for  rodakino RAD download and installation . Be sure to install rodakino_0_2_2.tgz, not an older one !!!

 

Goal:

We need a checkbox that is automatically synchronized with the corresponding database field.

Problem: The value of a checkbox is defined thru its "checked" Attribute only. We need to map this "checked" to a database field.

 

To accomplish this, we first have to define the representation of the database field:

It must be a char(1), the allowed values are 'T' or 'F' !

Second, the rodakino framework has to identify and handle not only "text" input fields, but also "checkbox" input fields.

Third we have to extend the val() function on the client side, so a checkbox returns 'T' and 'F' instead dealing with the "checked" attibute.

 

 

2. We need a new database table

$> psql -U node node

-- test input alternatives like checkbox
create table INPUT_TESTS (
    TEST_PK numeric(8),
    TEST_TEXT varchar(10),
    TEST_CBX char(1),   -- T...True, F...False
    constraint PK_INPUT_TESTS primary key (TEST_PK)
);
grant select,insert,update,delete on INPUT_TESTS to test;

 

-- insert menu item for rdbw_input_tests
insert into RODA_MENUS( MENUID,MENU_PROMPT,MENU_CLASS,OBJECT2CALL,PARENT_MENUID,SORT_ORDER,DELETED)
    values('input_tests','Input Tests','W','rw_input_tests','mymenu',1010,'N');

insert into MENU_PROFILE_ITEMS(MENU_PROFILE,MENUID,READONLY)
  values('TEST','input_tests','N');

 

3. The rodakino window rw_input_tests.hbs

This window exists and is already working and can be called from the rodakino menu.

You find it in the usual place in folder

.../rodakino/app/roda_windows/rw_input_tests

 

roda checkbox

Everything is already done in version 0_2_2, just lean back and enjoy!

 

Everywhere in the rodakino framework, we change the "input" selector to ":input". So all types of inputs a found, not just plain text inputs.

 

Then we create a new jQuery function in .../rodakino/layout_js.hbs and replace every call to val(...) by a call to rodaVal(...).

 

  // extended val() function, so it works for other input types too
  $.fn.rodaVal = function(value) {
    var myType = $(this).attr("type");
    // print the main attributes
    console.log("rodaVal.1 input name, type: "+
          $(this).attr("name") + " , "+ myType);

    // checkbox: only 'T' or 'F' allowed in rodakino in db, datatype is char(1)
    if (myType == 'checkbox') {
      // no value given - return value
      if (!value) {
        // checked -> return T, else F
        console.log("rodaVal.2 cbx checked:"+JSON.stringify($(this).prop('checked')));
        return $(this).prop('checked') ? 'T' : 'F';
      } else {
        // value 'T' given -> set to checked, not 'T' -> uncheck
        if (value == 'T') {
          $(this).prop('checked',true);
        } else {
          $(this).prop('checked',false);
        }
        console.log("rodaVal.3 cbx value: "+value);
        return $(this);
      }
    } else {
      // I assume, that input type = "text" -> call original val()
      console.log("rodaVal.4 text value: "+value);
      return value ? $(this).val(value) : $(this).val();
    }
  };

 

For text input fields, rodaVal() behaves identically to val().

But for checkbox fields, rodaVal('T') or rodaVal('F') sets or resets the "checked" attribute, and if called without a parameter, it returns 'T' if "checked", else 'F'.

So, from now on, a checkbox behaves exactly like a text input and can be used in the same way throughout rodakino.