Class: DrgcmsFormFields::MultitextAutocomplete
- Inherits:
-
DrgcmsField
- Object
- DrgcmsField
- DrgcmsFormFields::MultitextAutocomplete
- Defined in:
- app/models/drgcms_form_fields/multitext_autocomplete.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) -
tableCollection (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
#css_code, #hash_to_options, #html, #initialize, #record_text_for, #set_css_code, #set_initial_value, #set_style, #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.
187 188 189 190 191 192 193 194 195 |
# File 'app/models/drgcms_form_fields/multitext_autocomplete.rb', line 187 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'app/models/drgcms_form_fields/multitext_autocomplete.rb', line 71 def render # search field name if @yaml['search'].class == Hash table = @yaml['search']['table'] field_name = @yaml['search']['field'] method = @yaml['search']['method'] search = method.nil? ? field_name : "#{field_name}.#{method}" elsif @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 @yaml['html']['placeholder'] = t('drgcms.search_placeholder') _name = '_' + @yaml['name'] @html << '<div class="ui-autocomplete-border">' @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']) # text field for autocomplete @html << ' ' << @parent.text_field(record, _name, @yaml['html']) # div to list active selections @html << "<div id =\"#{record}#{@yaml['name']}\">" # 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;") link = @parent.fa_icon('check lg', class: 'dc-green') if @readonly field = @parent.hidden_field(record, "#{@yaml['name']}_#{rec.id}", value: element) "<div id=\"#{rec.id}\" style=\"padding:4px;\">#{link} #{rec.send(field_name)}<br>#{field}</div>" else '** error **' end end end @html << "</div></div>" # 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:4px;\">#{link} rec_search<br>#{field}</div>" # JS stuff @js << "$(document).ready(function() {\n $(\"#\#{record}_\#{_name}\").autocomplete( {\n source: function(request, response) {\n $.ajax({\n url: \"\#{ @parent.url_for( controller: 'dc_common', action: 'autocomplete' )}\",\n type: \"POST\",\n dataType: \"json\",\n data: { input: request.term, table: \"\#{table}\", search: \"\#{search}\" \#{(',id: \"'+@yaml['id'] + '\"') if @yaml['id']} },\n success: function(data) {\n response( $.map( data, function(key) {\n return key;\n }));\n }\n });\n },\n change: function (event, ui) { \n var div = '\#{one_div}';\n div = div.replace(/rec_id/g, ui.item.id)\n div = div.replace('rec_search', ui.item.value)\n $(\"#\#{record}\#{@yaml['name']}\").append(div);\n $(\"#\#{record}_\#{_name}\").val('');\n $(\"#\#{record}_\#{_name}\").focus();\n },\n minLength: 2\n });\n});\n" self end |
#ro_standard(table, search) ⇒ Object
Returns value for readonly field
56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/drgcms_form_fields/multitext_autocomplete.rb', line 56 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 |