【问题标题】:Spree or Solidus - Filter Product Taxons - taxons.jsSpree 或 Solidus - 过滤产品分类 - taxons.js
【发布时间】:2019-02-07 18:34:44
【问题描述】:

假设我有 3 个分类单元(Builders、States、Services)。有没有办法过滤“编辑产品分类”以在单击时仅列出下拉列表中的“服务”分类?狂欢视图是edit_product.html.erb

谢谢

taxons.js
//= require solidus_admin/Sortable
Spree.ready(function() {
  var productTemplate = HandlebarsTemplates['products/sortable'];
  var productListTemplate = function(products) {
    return _.map(products, productTemplate).join('') || "<h4>" + Spree.translations.no_results + "</h4>";
  };

  var saveSort = function(e) {
    var item = e.item;
    Spree.ajax({
      url: Spree.routes.classifications_api,
      method: 'PUT',
      data: {
        product_id: item.getAttribute('data-product-id'),
        taxon_id: $('#taxon_id').val(),
        position: e.newIndex
      }
    });
  };

  var formatTaxon = function(taxon) {
    return Select2.util.escapeMarkup(taxon.pretty_name);
  };

  $('#taxon_id').select2({
    dropdownCssClass: "taxon_select_box",
    placeholder: Spree.translations.find_a_taxon,
    ajax: {
      url: Spree.routes.taxons_search,
      params: {
        "headers": {
          "X-Spree-Token": Spree.api_key
        }
      },
      data: function(term, page) {
        return {
          per_page: 50,
          page: page,
          q: {
            name_cont: term
          }
        };
      },
      results: function(data) {
        return {
          results: data['taxons'],
          more: data.current_page < data.pages
        };
      }
    },
    formatResult: formatTaxon,
    formatSelection: formatTaxon
  });

  $('#taxon_id').on("change", function(e) {
    Spree.ajax({
      url: Spree.routes.taxon_products_api,
      data: {
        id: e.val,
        simple: 1
      },
      success: function(data) {
        $('#taxon_products').html(productListTemplate(data.products));

        var el = document.querySelector('#taxon_products')

        new Sortable(el, {
          draggable: ".sort_item",
          onEnd: saveSort
        });
      }
    });
  });
});

以上JS适用于Spree或Solidus电商平台。

希望能够过滤分类单元以包含名称“服务”。

澄清: 假设我有“建设者、国家和服务”分类法。在 Admin Edit Product 中,构建器、状态和服务的所有分类都列在下拉列表中。

我希望能够过滤列出哪些分类法。在这种情况下,我希望下拉列表框仅列出“服务”分类。

**Would like to see this:**
Services -> Senior Care
Services -> Nurse

**Do not want this:**
Builders -> Wall
Builders-> Mason
Services -> Senior Care
Services -> Nurse
States -> Utah
States -> Texas

【问题讨论】:

    标签: javascript ruby-on-rails spree solidus


    【解决方案1】:

    通过修改 taxon_autocomplete.js 来完成这项工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多