Ignore:
Timestamp:
04/27/12 11:34:20 (12 years ago)
Author:
nanardon
Message:
  • use js to add values on multi-valuable attribute
File:
1 edited

Legend:

Unmodified
Added
Removed
  • LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Model/AttrForms.pm

    r949 r951  
    249249sub attr_field_name { 
    250250    my ($self, $attr) = @_; 
    251     return ($self->{object} 
    252             ? $self->{object}->id . '_' 
    253             : '' 
    254         ) . $attr 
     251    my $id = $self->{object} 
     252        ? $self->{object}->id . '_' 
     253        : ''; 
     254    $id =~ s/\./_/g; 
     255    $id . $attr; 
    255256} 
    256257 
     
    288289        my $html_id = $htmlname . 
    289290            (scalar(@html_fields) ? scalar(@html_fields) : ''); 
     291        $html_id .= 'NB' if ($attribute->multiple); 
    290292 
    291293        my $html_field = ''; 
     294        my $html_ref = ''; 
    292295        for ($attribute->form_type) { 
    293296            /^textarea$/i and do { 
     
    403406                } 
    404407                if ($attribute->uniq) { 
    405                     $html_field .= qq|<span class="inputvalidate" id="${html_id}_observer_uniq"></span>|; 
    406                     $html_field .= "\n"; 
    407                     $html_field .= $self->{c}->prototype->observe_field( 
     408                    $html_ref .= qq|<span class="inputvalidate" id="${html_id}_observer_uniq"></span>|; 
     409                    $html_ref .= "\n"; 
     410                    $html_ref .= $self->{c}->prototype->observe_field( 
    408411                        $html_id, 
    409412                        { 
     
    418421                        } 
    419422                    ); 
    420                     $html_field .= qq|<span style="display:none" id="${html_id}_stat">Searching...</span>|; 
     423                    $html_ref .= qq|<span style="display:none" id="${html_id}_stat">Searching...</span>|; 
    421424                } 
    422425                last; 
     
    439442                } 
    440443                 
    441                 $html_field .= $self->{c}->prototype->observe_field( $html_id, { 
     444                $html_ref .= $self->{c}->prototype->observe_field( $html_id, { 
    442445                        update => "${html_id}_span", 
    443446                        url => $self->{c}->uri_for('/ajax', 'rawattr', $ref), 
     
    453456                ) 
    454457            } 
    455             $html_field .= sprintf(qq{<span id="%s" style="margin-left: 1em">}, 
     458            $html_ref .= sprintf(qq{<span id="%s" style="margin-left: 1em">}, 
    456459                "${html_id}_span"); 
    457460 
    458461            if (defined($text)) { 
    459                 $html_field .= $uri_part 
     462                $html_ref .= $uri_part 
    460463                    ? sprintf('<a href="%s">%s</a>', 
    461464                        $self->{c}->uri_for("/$uri_part", $attribute->display($attr_raw_value)), 
     
    464467            } 
    465468 
    466             $html_field .= "</span>\n"; 
     469            $html_ref .= "</span>\n"; 
    467470        } 
    468         push(@html_fields, $html_field); 
     471        push(@html_fields, $html_field . $html_ref); 
     472        if ($attribute->{multiple} && (!$attr_raw_value)) { 
     473            my $esc_html_field = $html_field; 
     474            my $esc_html_ref = $html_ref; 
     475            $esc_html_ref =~ s/'/\\'/g; 
     476            #$esc_html_field =~ s/"/\\"/g; 
     477            my $addempty = sprintf( 
     478                '<div id="%s"></div>', 
     479                "${html_id}_mul_emp0", 
     480            ); 
     481            $addempty .= sprintf(qq# 
     482            <script type="text/javascript"> 
     483                var cn_${html_id} = 0; 
     484                function add_${html_id} () { 
     485                    cn_${html_id}++; 
     486                    var cn = cn_${html_id}; 
     487                    var htmlid = '${html_id}'.replace(/NB/g, cn); 
     488                    document.getElementById('${html_id}_mul_emp' + (cn - 1)).innerHTML += 
     489                        '%s<span class="inputvalidate" id="${html_id}_observer_uniq"></span><br>'.replace(/NB/g, cn); 
     490                    document.getElementById('${html_id}_mul_emp' + (cn - 1)).innerHTML +=  
     491                        '<div id="${html_id}_mul_emp' + cn + '"></div>'; 
     492 
     493                    new Form.Element.Observer( '${html_id}'.replace('NB', cn), 
     494                           2, 
     495                           function( element, value ) { 
     496                                new Ajax.Updater( '${html_id}_observer_uniq'.replace(/NB/g, cn),  
     497                                    'http://localhost:8080/ajax/objattrexist/nethost/cname', 
     498                                    { parameters: 'val='+document.getElementById(htmlid).value+'&exclude=$attr_raw_value',asynchronous: 1 } )  
     499                            } ); 
     500                } 
     501            </script> 
     502            <button type="button" onclick="add_${html_id}()">+</button> 
     503            #, 
     504            $esc_html_field 
     505            ); 
     506            push(@html_fields, $addempty); 
     507        } 
    469508    } 
    470509    return join("<br>\n", @html_fields); 
     
    495534    my ($self, $attrs) = @_; 
    496535    $self->{c}->req->param($self->label) || $attrs or return; 
    497     my $prefix = $self->{object}->id . '_'; 
     536    my $prefix = $self->attr_field_name(''); 
    498537    my %fields; 
    499538    foreach ($attrs ? @{ $attrs } : $self->write_attributes) { 
Note: See TracChangeset for help on using the changeset viewer.