rails select级联

来源:互联网 发布:今天美国钻井平台数据 编辑:程序博客网 时间:2024/05/18 19:41

首先自定义input表单:

class TaxonSelectInput < SimpleForm::Inputs::CollectionSelectInput  include ActionView::Helpers::FormTagHelper  def input(wrapper_options = nil)    data = options[:data] || {}    class_name = self.class.name.underscore.dasherize    template.content_tag(:div, class: "taxon-picker #{class_name}", data: data) do      template.concat(        select_tag(:"#{attribute_name}_taxonomies", nil, class: 'taxonomies')      )      template.concat(        select_tag(:"#{attribute_name}_taxons", nil, class: 'taxons')      )      template.concat super(wrapper_options)    end  endend

因为数据库设计问题,我这里需要2个不同的下拉


js开发

const Cache = {}const root = ''export default class TaxonPicker {  static defaults = {    taxonomiesUrl: '/api/xxx',    taxonsUrl: '/api/xxx',    taxonChildrenUrl: '/api/xxx'  }  /**   * Creates a new instance of a china district picker.   * @class   * @param {jQuery} element - jQuery object to make into a picker.   * @param {Object} options - Overrides to the default plugin settings.   */  constructor(element, options) {    this.$element = element    this.options = $.extend({}, TaxonPicker.defaults, this.$element.data(), options)    this.taxon_id = `${this.options.taxon_id}`    this.selectedData = {      selectedTaxonomy: null,      selectedTaxon: null,      selectedTaxonChildren: null,    }    this.$taxonomies = this.$element.find('.taxonomies')    this.$taxons = this.$element.find('.taxons')    this.$taxonChildren = this.$element.find('.taxon_children')  }  init() {    this.loadTaxonomies()    this.attachEvents()    this.$taxonChildren.addClass('hide')    // this.test()  }  attachEvents() {    this.$taxonomies.on('change', () => {      this.selectedData.selectedTaxonomy = this.$taxonomies.val()      this.selectedData.selectedTaxon = null      this.selectedData.selectedTaxonChildren = null      this.$taxons.empty()      this.$taxonChildren.empty()      this.loadTaxons(this.selectedData.selectedTaxonomy)        .then(() => {          this.$taxonChildren.empty()        })    })    this.$taxons.on('change', () => {      this.selectedData.selectedTaxon = this.$taxons.val()      this.selectedData.selectedTaxonChildren = null      this.$taxonChildren.empty()      this.loadTaxonChildren(this.$taxons.val())    })    this.$taxonChildren.on('change', () => {      this.selectedData.selectedTaxonChildren = this.$taxonChildren.val()    })  }  loadTaxonomies = () => {    let ret    if (Cache[root]) {      ret = Promise.resolve(Cache[root])    } else {      ret = $.get(this.options.taxonomiesUrl).then((divisions) => {        Cache[root] = divisions        return divisions      })    }    return ret.then((taxonomies) => {      this.resetOptions(        this.$taxonomies,        taxonomies,        this.selectedData.selectedTaxonomy      )    })  }  loadTaxons = (taxonomyId) => {    let ret    if (Cache[taxonomyId]) {      ret = Promise.resolve(Cache[taxonomyId])    } else {      ret = $.get(this.options.taxonsUrl, { id: taxonomyId }).then((divisions) => {        Cache[taxonomyId] = divisions        return divisions      })    }    return ret.then((taxons) => {      this.resetOptions(        this.$taxons,        taxons,        this.selectedData.selectedTaxon      )    })  }  loadTaxonChildren = (parentId) => {    let ret    if (Cache[parentId]) {      ret = Promise.resolve(Cache[parentId])    } else {      ret = $.get(this.options.taxonChildrenUrl, { id: parentId }).then((divisions) => {        Cache[parentId] = divisions        return divisions      })    }    return ret.then((taxonChildren) => {      this.resetOptions(        this.$taxonChildren,        taxonChildren,        this.selectedData.selectedTaxonChildren      )    })  }  resetOptions = ($selector, divisions, selectedId) => {    $selector.empty()    let collection = divisions    const isSubSelector = $selector    if (isSubSelector && divisions.length === 0) {      collection = Cache[root].filter((division) => division.id)      $selector.addClass('hide')    } else {      $selector.removeClass('hide')    }    collection.forEach((division) => {      const optionElement = $(document.createElement('option'))      const attributes = {        value: division.id,        selected: `${selectedId}` === division.id      }      optionElement.attr(attributes).text(division.name)      $selector.append(optionElement)    })  }}

使用:

<%= f.input :taxon_id,                    as: :taxon_select,                    input_html: { class: "taxon_children" },                    data: { 'taxon_id': f.object.taxon_id, 'level': 3 },                    label_html: { for: "taxons" },                    label: '产品分类' %>


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 游泳憋不住气怎么办 总感觉内衣不舒服怎么办 下面流黄水有腥臭味怎么办 怀孕腿腋磨的疼怎么办 大人夜咳嗽厉害怎么办 九天没有来月经怎么办 耳朵里有耳屎怎么办 衣服洗完有酸味怎么办 裤子总有异味怎么办 痔疮破了流水怎么办 网络电视换台太麻烦怎么办 想换卡怎么办!太麻烦了 结婚后离婚孩子怎么办 怀孕凉咳嗽了怎么办 妇科炎症拖久了怎么办 手指甲干燥起皮怎么办 指甲周围皮粗糙怎么办 怀孕初期尿不尽怎么办 孕晚期漏羊水怎么办 貂皮被压出皱褶怎么办 冰箱被水泡了怎么办 布料怎么熨不平怎么办 书封面有折痕怎么办 拿书盖泡面 皱了怎么办 双眼皮上有褶子怎么办 真皮包被水泡了怎么办 合同被水泡了怎么办 纸张湿水了怎么办 睡不好眼睛肿怎么办 眼部周围很干燥怎么办 毕业了的书怎么办 油弄到手起水泡怎么办 t恤印花沾上纸怎么办 纸箱字印错了怎么办 开空调喉咙干怎么办 书泡水了皱了怎么办 裤子料子容易皱怎么办 棉裙子皱了怎么办 裙子坐皱了怎么办 脚起泡烂了怎么办 宝宝吃了湿巾纸怎么办