Select items from Datagrid header and display in rows [Javascript] [Chatakondu Gallery]

Discussion in 'Javascript' started by Suresh, Apr 16, 2004.

  1. Suresh

    Suresh Guest

    formName: name of the form
    ctrlCheckBox: left side check boxes
    ctrlSrcField: source fields where the value is coming from
    ctrlHidSrcField: hidden source field. in case if you need to pass
    hidden values from source field.
    ctrlDestField: destination field where the value will be displayed.
    ctrlHidDestField: hidden destination field. in case if you need to
    pass hidden value back.
    ErrMessageText: error text message if you need to display when error
    occurred.

    [Here is the example how this can be used.
    1) Create a data grid.
    2) First column in header is check box and items also checkboxes
    {ctrlCheckBox - field}.
    3-a) Second column in header is Drop-down box {ctrlSrcField} . And
    you have drop-down boxes {ctrlDestField} in items also. And you have
    'GO' button also in header.
    (or)
    3-b)Second column in header is Text Box {ctrlSrcField,
    ctrlHidSrcField}. And you have text-boxes {ctrlDestField,
    ctrlHidDestField} in items also. And you have 'GO' button also in
    header.

    4) Select a value from header control and select number of check
    boxes from items and click 'GO' button. Selected value will be
    displayed in destination controls.

    5) This is very useful when you want update multiple rows data from
    header.
    ]


    function fnFillItem(formName, ctrlCheckBox, ctrlSrcField,
    ctrlHidSrcField, ctrlDestField, ctrlHidDestField, ErrMessageText)
    {
    if (formName == '')
    var formName = document.forms(0).name
    obj = eval("document." + formName)

    var objctrlSrcField, objctrlHidSrcField;

    for (var i=0;i<obj.elements.length;i++)
    {
    var e = obj.elements;
    var elementName = e.name.toString()
    if (elementName.indexOf(ctrlSrcField) >= 0 )
    {
    objctrlSrcField = e;
    }
    if (ctrlHidSrcField != '')
    if (elementName.indexOf(ctrlHidSrcField) >= 0 )
    {
    objctrlHidSrcField = e;
    }
    }
    //check at least one check box is checked
    var AtLeastOneChecked = false;
    for (var i=0;i<obj.elements.length;i++)
    {
    var e = obj.elements;
    var elementName = e.name.toString()
    if (elementName.indexOf(ctrlCheckBox) >= 0 )
    if (e.checked )
    {
    AtLeastOneChecked = true;
    break;
    }
    }
    if (!AtLeastOneChecked)
    {
    alert("Please check at least one checkbox");
    return;
    }
    // Assign values to dest fields
    for (var i=0;i<obj.elements.length;i++)
    {
    var e = obj.elements;
    var elementName = e.name.toString()

    if (elementName.indexOf(ctrlCheckBox) >= 0 )
    if (e.checked)
    {
    var objctrlDestField = eval("document.forms[" + "\"" + formName +
    "\"" + "].item(" + "\"" + elementName.substring(0,
    elementName.indexOf(ctrlCheckBox)) + ctrlDestField + "\")")

    if (objctrlSrcField.type == 'select-one') //if the source
    control is drop-down or list box
    {
    // If it is drop-down and not selected pop-up error message
    //if (objctrlSrcField.value == '')
    //{
    // alert(ErrMessageText)
    // objctrlSrcField.focus();
    // return;
    //}

    // if the destination control is drop-down or list box
    if (objctrlDestField.type == 'select-one' ||
    objctrlDestField.type == 'select-multiple' )
    {
    var ItemExistInDestField = false;

    // check if item is already in the list box
    for (var j = 0; j < objctrlDestField.options.length; j++)
    if (objctrlSrcField.value == objctrlDestField.options[j].value)
    {
    ItemExistInDestField = true;
    break;
    }

    if (!ItemExistInDestField && objctrlSrcField.value != '')
    {
    var NewOption = document.createElement("OPTION");
    NewOption.value = objctrlSrcField.value;
    NewOption.text =
    objctrlSrcField.options[objctrlSrcField.selectedIndex].text;
    objctrlDestField.add(NewOption);
    objctrlDestField.options(objctrlDestField.length-1).selected =
    true;
    }
    }
    else
    {
    //if the destination control is other than drop-down or list box
    if (objctrlSrcField.value == '')
    objctrlDestField.value = ""
    else
    objctrlDestField.value =
    objctrlSrcField.options[objctrlSrcField.selectedIndex].text
    }
    } //if (objctrlSrcField.type == 'select-one')

    else if (objctrlSrcField.type == 'text') //if the source
    control is text box
    {
    // if the destination control is drop-down or list box
    if (objctrlDestField.type == 'select-one')
    {
    var ItemExistInDestField = false;

    // check if item is already in the list box
    for (var k = 0; k < objctrlDestField.options.length; k++)
    if (objctrlHidSrcField.value ==
    objctrlDestField.options[k].value)
    {
    ItemExistInDestField = true;
    break;
    }

    if (!ItemExistInDestField && objctrlSrcField.value != '')
    {
    var NewOption = document.createElement("OPTION");
    NewOption.value = objctrlHidSrcField.value;
    NewOption.text = objctrlSrcField.value;
    objctrlDestField.add(NewOption);
    objctrlDestField.options(objctrlDestField.length-1).selected =
    true;

    }
    }
    else //if the destination control is other than
    drop-down or list box
    objctrlDestField.value = objctrlSrcField.value
    }

    else if (objctrlSrcField.type == 'checkbox') //if the source
    control is check box
    objctrlDestField.checked = objctrlSrcField.checked

    //if (ctrlHidDestField != '')
    //{
    // var objctrlHidDestField = eval("document.forms[" + "\"" +
    formName + "\"" + "].item(" + "\"" + elementName.substring(0,
    elementName.indexOf(ctrlCheckBox)) + ctrlHidDestField + "\")")
    // objctrlHidDestField.value = objctrlSrcField.value
    //}
    }
    }
    }
     
    Suresh, Apr 16, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VijayRama
    Replies:
    2
    Views:
    2,335
    Gregory A. Beamer
    Oct 12, 2009
  2. Eric
    Replies:
    5
    Views:
    200
  3. Henrik
    Replies:
    1
    Views:
    433
    Ken Cox [Microsoft MVP]
    Jul 6, 2006
  4. Suresh
    Replies:
    1
    Views:
    138
    Richard Cornford
    Apr 16, 2004
  5. Suresh
    Replies:
    2
    Views:
    402
    Suresh
    Apr 22, 2004
Loading...

Share This Page