Class: DrgcmsFormFields::TreeSelect
- Inherits:
-
Select
- Object
- DrgcmsField
- Select
- DrgcmsFormFields::TreeSelect
- Defined in:
- app/models/drgcms_form_fields.rb
Overview
Implementation of tree_select DRG CMS form field. Field will provides multiple select functionality displayed as a tree. Might be used for selecting multiple categories in a parent-child tree view.#
Form options:
-
name:field name (required) -
type:tree_select (required) -
choices:Values for choices separated by comma. Values can also be specified like description:value.
In this case description will be shown to user, but value will be saved to document.
choices: 'OK:0,Ready:1,Error:2'
choices: Ruby,Pyton,PHP
-
eval:Choices will be provided by evaluating expression eval: ModelName.choices4_field; Model class should define method which will provide data for field. Data returned must be of type Array and have 3 elements. 1 - description text 2 - id value 3 - parent id -
html:html options which apply to select and text_field fields (optional)
Form example:
10:
name: categories
type: tree_select
eval: 'Categories.all_categories'
html:
size: 50x10
Instance Attribute Summary
Attributes inherited from DrgcmsField
Class Method Summary collapse
-
.get_data(params, name) ⇒ Object
Return value.
Instance Method Summary collapse
-
#_make_tree(parent) ⇒ Object
Prepare choices for tree data rendering.
-
#make_tree(parent) ⇒ Object
Prepare choices for tree data rendering.
-
#render ⇒ Object
Render tree_select field html code.
Methods inherited from Select
#do_eval, #get_choices, #ro_standard
Methods inherited from DrgcmsField
#hash_to_options, #initialize, #record_text_for, #ro_standard, #set_initial_value, #set_style, #t
Constructor Details
This class inherits a constructor from DrgcmsFormFields::DrgcmsField
Class Method Details
.get_data(params, name) ⇒ Object
Return value. Return nil if input field is empty
1504 1505 1506 |
# File 'app/models/drgcms_form_fields.rb', line 1504 def self.get_data(params, name) params['record'][name].blank? ? nil : params['record'][name].split(',') end |
Instance Method Details
#_make_tree(parent) ⇒ Object
Prepare choices for tree data rendering.
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 |
# File 'app/models/drgcms_form_fields.rb', line 1434 def _make_tree(parent) @html << '<ul>' choices = @choices[parent.to_s].sort_alphabetical_by(&:first) # use UTF-8 sort choices.each do |choice| jstree = %Q[{"selected" : #{choice[4] ? 'true' : 'false'} }] # data-jstree must be singe quoted @html << %Q[<li data-id="#{choice[1]}" data-jstree='#{jstree}'>#{choice.first}\n] # call recursively for children make_tree(choice[1]) if @choices[ choice[1].to_s ] @html << "</li>" end @html << '</ul>' end |
#make_tree(parent) ⇒ Object
Prepare choices for tree data rendering.
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 |
# File 'app/models/drgcms_form_fields.rb', line 1411 def make_tree(parent) return '' unless @choices[parent.to_s] @html << '<ul>' choices = if @choices[parent.to_s].first.last != 0 @choices[parent.to_s].sort_by {|e| e[3] } # sort by order if first is not 0 # @choices[parent.to_s].sort_alphabetical_by(&:first) # use UTF-8 sort else @choices[parent.to_s].sort_alphabetical_by(&:first) # use UTF-8 sort end choices.each do |choice| jstree = %Q[{"selected" : #{choice[4] ? 'true' : 'false'} }] # data-jstree must be singe quoted @html << %Q[<li data-id="#{choice[1]}" data-jstree='#{jstree}'>#{choice.first}\n] # call recursively for children make_tree(choice[1]) if @choices[ choice[1].to_s ] @html << "</li>" end @html << '</ul>' end |
#render ⇒ Object
Render tree_select field html code
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 |
# File 'app/models/drgcms_form_fields.rb', line 1451 def render return ro_standard if @readonly set_initial_value('html','value') require 'sort_alphabetical' record = record_text_for(@yaml['name']) @html << "<div id=\"#{@yaml['name']}\" class=\"tree-select\" #{set_style()} >" # Fill @choices hash. The key is parent object id @choices = {} do_eval(@yaml['eval']).each {|data| @choices[ data[2].to_s ] ||= []; @choices[ data[2].to_s ] << (data << false)} # put current values hash with. To speed up selection when there is a lot of categories current_values = {} current = @record[@yaml['name']] || [] current = [current] unless current.class == Array # non array fields current.each {|e| current_values[e.to_s] = true} # set third element of @choices when selected @choices.keys.each do |key| 0.upto( @choices[key].size - 1 ) do |i| choice = @choices[key][i] choice[4] = true if current_values[ choice[1].to_s ] end end make_tree(nil) @html << '</ul></div>' # add hidden communication field @html << @parent.hidden_field(record, @yaml['name'], value: current.join(',')) # javascript to update hidden record field when tree looses focus @js ="$(function(){\n $(\"#\#{@yaml['name']}\").jstree( {\n \"checkbox\" : {\"three_state\" : false}, \n \"core\" : { \"themes\" : { \"icons\": false } },\n \"plugins\" : [\"checkbox\"]\n });\n});\n \n$(document).ready(function() {\n $('#\#{@yaml['name']}').on('focusout', function(e) {\n var checked_ids = [];\n var checked = $('#\#{@yaml['name']}').jstree(\"get_checked\", true);\n $.each(checked, function() {\n checked_ids.push( this.data.id );\n });\n $('#record_\#{@yaml['name']}').val( checked_ids.join(\",\") );\n });\n});\n" self end |