Class: DrgcmsFormFields::TextAutocomplete

Inherits:
DrgcmsField
  • Object
show all
Defined in:
app/models/drgcms_form_fields.rb

Overview

Implementation of text_autocomplete DRG CMS form field.

Form options:

  • name: field name (required)

  • type: text_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:

10:
  name: user_id
  type: text_autocomplete
  search: dc_user.name
  html:
    size: 30

Instance Attribute Summary

Attributes inherited from DrgcmsField

#html, #js

Instance Method Summary collapse

Methods inherited from DrgcmsField

get_data, #hash_to_options, #initialize, #record_text_for, #ro_standard, #set_initial_value, #t

Constructor Details

This class inherits a constructor from DrgcmsFormFields::DrgcmsField

Instance Method Details

#renderObject

Render text_autocomplete field html code



1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
# File 'app/models/drgcms_form_fields.rb', line 1062

def render
# Return descriptive text and put it into input field
# search field name
  if @yaml['search'].class == Hash
    table    = @yaml['search']['table']
    ret_name = @yaml['search']['field']
    method   = @yaml['search']['method']
  elsif @yaml['search'].match(/\./)
    table, ret_name, method = @yaml['search'].split('.') 
  else
    ret_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
  return 'Table or field keyword not defined!' unless (table and ret_name)
# TODO check if table exists 
  t = table.classify.constantize
# find record and return value of field
  value_send_as = 'p_' + @yaml['name']
  value = if @parent.params[value_send_as]
    @parent.params[value_send_as]
  elsif @record and @record[@yaml['name']]
    @record[@yaml['name']]
  end
# Found value to be written in field  
  if value
    record = t.find(value)
    value_displayed = record[ret_name] if record      
  end
# return if readonly
  return ro_standard(value_displayed) if @readonly
# Add method back, so autocomplete will know that it must search for method inside class
  ret_name = "#{ret_name}.#{method}" if method
  @yaml['html'] ||= {}
  @yaml['html']['value'] = value_displayed
#    
  _name = '_' + @yaml['name']
  record = record_text_for(@yaml['name'])  
  @html << @parent.text_field(record, _name, @yaml['html'])
  if @yaml['with_new']
    @html << @parent.image_tag('drg_cms/add.png', class: 'in-edit-add', title: t('drgcms.new'), 
             style: "vertical-align: top;", 'data-table' => @yaml['with_new'] )    
  end
  @html << @parent.hidden_field(record, @yaml['name'], value: value)        # actual value will be in hidden field
# JS stuff    
  @js << "$(document).ready(function() {\n  $(\"#\#{record}_\#{_name}\").autocomplete( {\n    source: function(request, response) {\n      $.ajax({\n        url: '/dc_common/autocomplete',\n        type: \"POST\",\n        dataType: \"json\",\n        data: { input: request.term, table: \"\#{table}\", search: \"\#{ret_name}\" \#{(',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      $(\"#\#{record}_\#{@yaml['name']}\").val(ui.item.id);\n    },\n    minLength: 2\n  });\n});\n"
    
  self 
end