Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b7383969

Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt

  • ID b73839693bf70d20b8cf21a49cad3ded744c182f
  • Vorgänger 647c292c
  • Nachfolger fa515ecf

Helfer-Layout-Funktion zum Sortieren von Listen mittels JavaScript + AJAX

Unterschiede anzeigen:

SL/Template/Plugin/L.pm
396 396
  return $code;
397 397
}
398 398

  
399
sub sortable_element {
400
  my ($self, $selector, @slurp) = @_;
401
  my %params                    = _hashify(@slurp);
402

  
403
  my %attributes = ( helper => <<JAVASCRIPT );
404
    function(event, ui) {
405
      ui.children().each(function() {
406
        \$(this).width(\$(this).width());
407
      });
408
      return ui;
409
    }
410
JAVASCRIPT
411

  
412
  if ($params{url} && $params{with}) {
413
    my $as      = $params{as} || $params{with};
414
    my $filter  = ".filter(function(idx) { return this.substr(0, " . length($params{with}) . ") == '$params{with}'; })";
415
    $filter    .= ".map(function(idx, str) { return str.replace('$params{with}_', ''); })";
416

  
417
    $attributes{stop} = <<JAVASCRIPT;
418
      function(event, ui) {
419
        \$.post('$params{url}', { '${as}[]': \$(\$('${selector}').sortable('toArray'))${filter}.toArray() });
420
        return ui;
421
      }
422
JAVASCRIPT
423
  }
424

  
425
  my $attr_str = join(', ', map { "${_}: $attributes{$_}" } keys %attributes);
426

  
427
  my $code = <<JAVASCRIPT;
428
<script type="text/javascript">
429
  \$(function() {
430
    \$( "${selector}" ).sortable({ ${attr_str} }).disableSelection();
431
  });
432
</script>
433
JAVASCRIPT
434

  
435
  return $code;
436
}
437

  
399 438
sub online_help_tag {
400 439
  my ($self, $tag, @slurp) = @_;
401 440
  my %params               = _hashify(@slurp);
......
603 642

  
604 643
=back
605 644

  
645
=item C<sortable_element $selector, %params>
646

  
647
Makes the children of the DOM element C<$selector> (a jQuery selector)
648
sortable with the I<jQuery UI Selectable> library. The children can be
649
dragged & dropped around. After dropping an element an URL can be
650
postet to with the element IDs of the sorted children.
651

  
652
C<%params> can contain the following entries:
653

  
654
=over 2
655

  
656
=item C<url>
657

  
658
The URL to POST an AJAX request to after a dragged element has been
659
dropped. The AJAX request's return value is ignored. If given then
660
C<$params{with}> must be given as well.
661

  
662
=item C<with>
663

  
664
A string that is interpreted as the prefix of the children's ID. Upon
665
POSTing the result each child whose ID starts with C<$params{with}> is
666
considered. The prefix and the following "_" is removed from the
667
ID. The remaining parts of the IDs of those children are posted as a
668
single array parameter. The array parameter's name is either
669
C<$params{as}> or, missing that, C<$params{with}>.
670

  
671
=item C<as>
672

  
673
Sets the POST parameter name for AJAX request after dropping an
674
element (see C<$params{with}>).
675

  
676
=back
677

  
678
Example:
679

  
680
  <table id="thing_list">
681
    <thead>
682
      <tr><td>This</td><td>That</td></tr>
683
    </thead>
684
    <tbody>
685
      <tr id="thingy_2"><td>stuff</td><td>more stuff</td></tr>
686
      <tr id="thingy_15"><td>stuff</td><td>more stuff</td></tr>
687
      <tr id="thingy_6"><td>stuff</td><td>more stuff</td></tr>
688
    </tbody>
689
  <table>
690

  
691
  [% L.sortable_element('#thing_list tbody',
692
                        'url'  => 'controller.pl?action=SystemThings/reorder',
693
                        'with' => 'thingy',
694
                        'as'   => 'thing_ids') %]
695

  
696
After dropping e.g. the third element at the top of the list a POST
697
request would be made to the C<reorder> action of the C<SystemThings>
698
controller with a single parameter called C<thing_ids> -- an array
699
containing the values C<[ 6, 2, 15 ]>.
700

  
606 701
=item C<dump REF>
607 702

  
608 703
Dumps the Argument using L<Data::Dumper> into a E<lt>preE<gt> block.

Auch abrufbar als: Unified diff