From: Sven Schöling Date: Wed, 10 Jul 2013 14:45:21 +0000 (+0200) Subject: ajax_autocomplete umgestellt auf get_models und inline JSON (20% schneller als template) X-Git-Tag: release-3.1.0beta1~182 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=7a6466811399cba50ab3a55f18665b5663f6d3a5;p=kivitendo-erp.git ajax_autocomplete umgestellt auf get_models und inline JSON (20% schneller als template) --- diff --git a/SL/Controller/Part.pm b/SL/Controller/Part.pm index f0ad1ca9a..4d8da25f8 100644 --- a/SL/Controller/Part.pm +++ b/SL/Controller/Part.pm @@ -11,6 +11,7 @@ use SL::Controller::Helper::Sorted; use SL::Controller::Helper::Paginated; use SL::Controller::Helper::Filtered; use SL::Locale::String qw(t8); +use SL::JSON; use Rose::Object::MakeMethods::Generic ( 'scalar --get_set_init' => [ qw(parts) ], @@ -20,15 +21,15 @@ use Rose::Object::MakeMethods::Generic ( __PACKAGE__->run_before(sub { $::auth->assert('part_service_assembly_edit') }); __PACKAGE__->make_filtered( - ONLY => [ qw(part_picker_search part_picker_result) ], + ONLY => [ qw(part_picker_search part_picker_result ajax_autocomplete) ], LAUNDER_TO => 'filter', ); __PACKAGE__->make_paginated( - ONLY => [ qw(part_picker_search part_picker_result) ], + ONLY => [ qw(part_picker_search part_picker_result ajax_autocomplete) ], ); __PACKAGE__->make_sorted( - ONLY => [ qw(part_picker_search part_picker_result) ], + ONLY => [ qw(part_picker_search part_picker_result ajax_autocomplete) ], DEFAULT_BY => 'partnumber', DEFAULT_DIR => 1, @@ -39,31 +40,26 @@ __PACKAGE__->make_sorted( sub action_ajax_autocomplete { my ($self, %params) = @_; - my $limit = $::form->{limit} || 20; - my $type = $::form->{type} || {}; - my $query = { ilike => "%$::form->{term}%" }; - my @filter; - push @filter, SL::DB::Manager::Part->type_filter($type); - push @filter, ($::form->{column}) - ? ($::form->{column} => $query) - : (or => [ partnumber => $query, description => $query ]); - - $self->{parts} = SL::DB::Manager::Part->get_all(query => [ @filter ], limit => $limit); - $self->{value} = $::form->{column} || 'description'; + my $value = $::form->{column} || 'description'; # if someone types something, and hits enter, assume he entered the full name. # if something matches, treat that as sole match if ($::form->{prefer_exact}) { - for my $part (@{ $self->{parts} }) { - if ( lc $part->description eq lc $::form->{term} - || lc $part->partnumber eq lc $::form->{term}) { - $self->{parts} = [ $part ]; - last; - } - } + # TODO! } - $self->render('part/ajax_autocomplete', { layout => 0, type => 'json' }); + my @hashes = map { + +{ + value => $_->$value, + label => $_->long_description, + id => $_->id, + partnumber => $_->partnumber, + description => $_->description, + type => $_->type, + } + } @{ $self->parts }; + + $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 }); } sub action_test_page { diff --git a/SL/DB/Part.pm b/SL/DB/Part.pm index 3fb34bad2..94ce4d207 100644 --- a/SL/DB/Part.pm +++ b/SL/DB/Part.pm @@ -179,6 +179,10 @@ sub get_chart { return $charts->{$taxzone}->{$type}; } +sub long_description { + join ' ', grep $_, map $_[0]->$_, qw(partnumber description); +} + 1; __END__ diff --git a/SL/Presenter/Part.pm b/SL/Presenter/Part.pm index 25d682037..bef55a35f 100644 --- a/SL/Presenter/Part.pm +++ b/SL/Presenter/Part.pm @@ -51,6 +51,8 @@ C can be an id or C instance. If C contains C only parts of this type will be used for autocompletion. +Obsolete parts will by default not displayed for selection. However they are +accepted as default values and can persist during updates. =back diff --git a/js/autocomplete_part.js b/js/autocomplete_part.js index 90e4312f0..00bdc5dd6 100644 --- a/js/autocomplete_part.js +++ b/js/autocomplete_part.js @@ -18,14 +18,10 @@ namespace('kivi', function(k){ var last_dummy = $dummy.val(); var open_dialog = function(){ open_jqm_window({ - url: 'controller.pl', - data: { - action: 'Part/part_picker_search', + url: 'controller.pl?action=Part/part_picker_search', + data: $.extend({ real_id: real_id, - 'filter.all:substr::ilike': function(){ return $dummy.val() }, - 'filter.type': function(){ return $type.val() }, - 'column': function(){ return $column.val() }, - }, + }, ajax_data($dummy.val())), id: 'part_selection', }); return true; @@ -33,11 +29,11 @@ namespace('kivi', function(k){ function ajax_data(term) { return { - term: term, - type: function() { return $type.val() }, + 'filter.all:substr::ilike': term, + 'filter.type': function() { return $type.val() }, + 'filter.obsolete': 0, column: function() { return $column.val()===undefined ? '' : $column.val() }, current: function() { return $real.val() }, - obsolete: 0, } } @@ -68,12 +64,9 @@ namespace('kivi', function(k){ function update_results () { $.ajax({ url: 'controller.pl?action=Part/part_picker_result', - data: { - 'filter.all:substr::ilike': function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val }, - 'filter.type': $type.val(), - 'column': $column.val(), - 'real_id': $real.val, - }, + data: $.extend({ + 'real_id': $real.val(), + }, ajax_data(function(){ var val = $('#part_picker_filter').val(); return val === undefined ? '' : val })), success: function(data){ $('#part_picker_result').html(data) } }); }; diff --git a/templates/webpages/part/ajax_autocomplete.json b/templates/webpages/part/ajax_autocomplete.json deleted file mode 100644 index 53fa6f043..000000000 --- a/templates/webpages/part/ajax_autocomplete.json +++ /dev/null @@ -1,13 +0,0 @@ -[%- USE HTML %][% USE JSON %][ -[%- FOREACH part = SELF.parts %] -[%- ajax_autocomplete__label = part.partnumber _ " " _ part.description %] - { - "value": [% part.${SELF.value}.json %], - "label": [% ajax_autocomplete__label.json %], - "id": [% part.id.json %], - "partnumber": [% part.partnumber.json %], - "description": [% part.description.json %], - "type": [% part.type.json %] - }[% ',' UNLESS loop.last %] -[%- END %] -]