Thursday, 5 October 2023

On Change get sub category, based on category in admin side

 1. app/assets/javascripts/active_admin.js

$(document).on('change', '#bx_block_latest_news_latest_new_category_id', function () {

  var categoryId = $(this).val();

  $.ajax({

    url: '/accounts/sub_categories.json',

    method: 'GET',

    data: { category_id: categoryId },

    dataType: 'json',

    success: function (data) {

      var subcategorySelect = $('#product_subcategory_id');

      subcategorySelect.empty();

      $.each(data, function (key, value) {

        subcategorySelect.append($('<option>').text(value.name).attr('value', value.id));

      });

    }

  });

});


-------------------------------------------------------------


2. Routes.rb

resources :accounts, :only => [:create, :destroy] do

      get 'sub_categories', on: :collection

    end

---------------------------------------------------------------

3. admin/users.rb


form do |f|

    f.inputs do

    f.input :category_id, as: :select, collection: BxBlockCategories::Category.all.map{|e| ["#{e.name}", e.id]}, :input_html => {}, include_blank: false

      f.input :title, as: :select, collection: [], input_html: { id: 'product_subcategory_id' }

      # f.input :title

      f.input :description, :as => :ckeditor

      f.input :image, label: 'Add image', as: :file, :input_html => {}

      span do

        image_tag(object.image, height: '80', width: '100') rescue nil

      end

    end

    f.actions do

      if resource.persisted?

        f.action :submit, as: :button, label: 'Update Latest news'

      else

        f.action :submit, as: :button, label: 'Create Latest News'

      end

      f.action :cancel, as: :link, label: 'Cancel'

    end

  end


------------------------------------------------------------------------------

4. accounts_controller.rb


def sub_categories

      all_df = BxBlockCategories::SubCategory.all

      render json: all_df

    end


--------------------------------------------------------


yadi update karte hai to, selected data ana chahiye

admin/users.rb

controller do

    helper_method :options_for_sub_categories

    def options_for_sub_categories

      categories_and_subcategories = []

      BxBlockCategories::Category.all.each do |category|

        categories_and_subcategories << ["Category: #{category.name}", category.id, { disabled: true }]

        category.sub_categories.each do |sub_category|

          categories_and_subcategories << ["#{sub_category.name}", sub_category.id]

        end

      end

      return categories_and_subcategories

    end

  end


---------------------------------------------------------------------

admin/users.rb

form do |f|

    f.inputs "Catalogue Details" do

      # f.input :category_id, as: :searchable_select, collection: BxBlockCategories::Category.all.collect {|var| [var.name, var.id] }, include_blank: false

      # f.input :sub_category_id, as: :searchable_select, collection: BxBlockCategories::SubCategory.all.collect {|var| [var.name, var.id] }, include_blank: false

      # f.input :sub_category, as: :select, collection: options_for_sub_categories(f.object.category_id), include_blank: 'Select a Sub-Category'



















































No comments:

Post a Comment