Class: DrgcmsFormFields::MultitextAutocomplete
- Inherits:
-
DrgcmsField
- Object
- DrgcmsField
- DrgcmsFormFields::MultitextAutocomplete
- Defined in:
- app/models/drgcms_form_fields.rb
Overview
Implementation of multitext_autocomplete DRG CMS form field.
multitext_autocomplete field is complex data entry field which uses autocomplete function when selecting multiple values for MongoDB Array field. Array typically holds id’s of selected documents and control typically displays value of the field name defined by search options.
Form options:
-
name:
field name (required) -
type:
multitext_autocomplete (required) -
table
Collection (table) name. When defined search must contain field name -
search:
Search may consist of three parameters from which are separated either by dot (.) or comma(,)-
search_field_name; when table option is defined search must define field name which will be used for search query
-
collection_name.search_field_name; Same as above except that table options must be ommited.
-
collection_name.search_field_name.method_name; When searching is more complex custom search
method may be defined in CollectionName model which will provide result set for search.
-
Form example:
90:
name: kats
type: multitext_autocomplete
search: dc_category.name
html:
size: 30
Instance Attribute Summary
Attributes inherited from DrgcmsField
Class Method Summary collapse
-
.get_data(params, name) ⇒ Object
Class method for retrieving data from multitext_autocomplete form field.
Instance Method Summary collapse
-
#render ⇒ Object
Render multitext_autocomplete field html code.
-
#ro_standard(table, search) ⇒ Object
Returns value for readonly field.
Methods inherited from DrgcmsField
#hash_to_options, #initialize, #record_text_for, #set_initial_value, #t
Constructor Details
This class inherits a constructor from DrgcmsFormFields::DrgcmsField
Class Method Details
.get_data(params, name) ⇒ Object
Class method for retrieving data from multitext_autocomplete form field.
519 520 521 522 523 524 525 526 527 |
# File 'app/models/drgcms_form_fields.rb', line 519 def self.get_data(params, name) r = [] params['record'].each do |k,v| # if it starts with - then it was removed r << BSON::ObjectId.from_string(v) if k.match("#{name}_") and v[0,1] != '-' end r.uniq! r end |
Instance Method Details
#render ⇒ Object
Render multitext_autocomplete field html code
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'app/models/drgcms_form_fields.rb', line 413 def render # search field name if @yaml['search'].to_s.match(/\./) table, field_name, method = @yaml['search'].split(/\.|\,/) search = method.nil? ? field_name : "#{field_name}.#{method}" else # search and table name are separated search = field_name = @yaml['search'] end # determine table name if @yaml['table'] table = if @yaml['table'].class == String @yaml['table'] # eval(how_to_get_my_table_name) elsif @yaml['table']['eval'] eval @yaml['table']['eval'] else p "Field #{@yaml['name']}: Invalid table parameter!" nil end end unless (table and search) @html << 'Table or search field not defined!' return self end # return ro_standard(table, search) if @readonly # TODO check if table exists collection = table.classify.constantize unless @record.respond_to?(@yaml['name']) @html << "Invalid field name: #{@yaml['name']}" return self end # put field to enter search data on form @yaml['html'] ||= {} @yaml['html']['value'] = '' # must be. Otherwise it will look into record and return error _name = '_' + @yaml['name'] @html << '<table class="ui-autocomplete-table"><td>' @html << @parent.link_to(@parent.fa_icon('plus-square lg', class: 'dc-animate dc-green'), '#',onclick: 'return false;') # dummy add. But it is usefull. record = record_text_for(@yaml['name']) @html << ' ' << @parent.text_field(record, _name, @yaml['html']) # text field for autocomplete @html << "<div id =\"#{record}#{@yaml['name']}\">" # div to list active records # find value for each field inside categories unless @record[@yaml['name']].nil? @record[@yaml['name']].each do |element| # this is quick and dirty trick. We have model dc_big_table which can be used for retrive # more complicated options # TODO retrieve choices from big_table rec = if table == 'dc_big_table' collection.find(@yaml['name'], @parent.session) else collection.find(element) end # Related data is missing. It happends. @html << if rec link = @parent.link_to(@parent.fa_icon('remove lg', class: 'dc-animate dc-red'), '#', onclick: "$('##{rec.id}').hide(); var v = $('##{record}_#{@yaml['name']}_#{rec.id}'); v.val(\"-\" + v.val());return false;") field = @parent.hidden_field(record, "#{@yaml['name']}_#{rec.id}", value: element) "<div id=\"#{rec.id}\" style=\"padding:2px;\">#{link} #{rec[field_name]}<br>#{field}</div>" else '** error **' end end end @html << "</div></td></table>" # Create text for div to be added when new category is selected link = @parent.link_to(@parent.fa_icon('remove lg', class: 'dc-animate dc-red'), '#', onclick: "$('#rec_id').hide(); var v = $('##{record}_#{@yaml['name']}_rec_id'); v.val(\"-\" + v.val());return false;") field = @parent.hidden_field(record, "#{@yaml['name']}_rec_id", value: 'rec_id') one_div = "<div id=\"rec_id\" style=\"padding:2px;\">#{link} rec_search<br>#{field}</div>" # JS stuff @js << <<EOJS $(document).ready(function() { $("##{record}_#{_name}").autocomplete( { source: function(request, response) { $.ajax({ url: "#{ @parent.url_for( controller: 'dc_common', action: 'autocomplete' )}", type: "POST", dataType: "json", data: { input: request.term, table: "#{table}", search: "#{search}" #{(',id: "'+@yaml['id'] + '"') if @yaml['id']} }, success: function(data) { response( $.map( data, function(key) { return key; })); } }); }, change: function (event, ui) { var div = '#{one_div}'; div = div.replace(/rec_id/g, ui.item.id) div = div.replace('rec_search', ui.item.value) $("##{record}#{@yaml['name']}").append(div); $("##{record}_#{_name}").val(''); }, minLength: 2 }); }); EOJS self end |
#ro_standard(table, search) ⇒ Object
Returns value for readonly field
398 399 400 401 402 403 404 405 406 407 408 |
# File 'app/models/drgcms_form_fields.rb', line 398 def ro_standard(table, search) result = '' table = table.classify.constantize return self if @record[@yaml['name']].nil? # when field name and method are defined together search = search.split('.').first if search.match('.') @record[@yaml['name']].each do |element| result << table.find(element)[search] + '<br>' end super(result) end |