-1

This is being used for product comparison feature. A product has attributes like product code, name, image-URL, reviews, price etc. My product compare panel will have only a few features for each product which I am planning to store in session. Can this be done using JQuery? What should be the strategy?


Update:

Component Controller

@Override
    protected void fillModel(final HttpServletRequest request, final Model model,
            final ProductCompareComponentModel component)

final Cookie[] cookies = request.getCookies();
        String productList = null;
        boolean cookieFound = false;

        logger.info("Displaying session value for product compare" + sessionService.getAttribute("sessionProductCode"));
        if (cookies != null)
        {
            logger.info("Cookies not null");
            for (final Cookie cookie : cookies)
            {
                logger.info("Cookie name" + cookie.getName());
                if (cookie.getName().equals("productList") && cookie.getValue() != null)
                {
                    cookieFound = true;
                    productList = cookie.getValue().toString();
                    logger.info("Product List value" + cookie.getValue().toString());

                }
            }
        }

if (cookieFound == false)
        {
            if (sessionService.getAttribute("sessionProductCode") != null)
            {
                productList = sessionService.getAttribute("sessionProductCode");
            }
        }

        final ArrayList<ProductCompareDTO> prodCompareMapPanelView = populateCompareMap(productList);

        if (component.getDisplayType().toString() == CommonConstants.COMPARE_VIEW_PANEL)
        {
            logger.info("Displaying panel view ");
            model.addAttribute("prodCompareMapPanelView", prodCompareMapPanelView);
        }
        else

        if (component.getDisplayType().toString() == CommonConstants.COMPARE_VIEW_FULL)
        {
            logger.info("Displaying full view ");
        }

        model.addAttribute("displaytype", component.getDisplayType().toString());
        model.addAttribute("maxCount", component.getMaxProductCount());
    }

Component JSP

    <c:choose>
        <c:when test="${not empty maxCount && not empty displaytype}">


            <div id="compare-pane" class="row comparePanel1">
                <button type='button' onclick='doComparePanelClose()'>Close
                    Compare Panel</button>

                <c:if test="${displaytype == 'PANELVIEW'}">

                    <c:forEach items="${prodCompareMapPanelView}" var="mapEntry">
                        <div class="col-sm-4" id="innerdiv${mapEntry.code}">

                            <c:if test="${(not empty mapEntry.name)}">
                            ${mapEntry.name}
                            </c:if>
                            <c:if test="${not empty mapEntry.thumbnail }">
                                <img src="${mapEntry.thumbnail}" />
                                <button id="close${mapEntry.code}" class="compareButton"
                                    type="submit" data-product-id="${mapEntry.code}"
                                    onclick="closeCompareProductPanel('${mapEntry.code}');">Close
                                    X</button>
                            </c:if>
                        </div>

                    </c:forEach>

                </c:if>

                <div id="bottomInnerComparePanel1">*Compare upto ${maxCount }
                    products</div>
                <button id="comparesubmit" class="compareButton" type="submit"
                    onclick="compareSubmit();">See Comparison ></button>

            </div>
            <c:url value="/search/compare" var="compareUrl" />
            <form id="compareForm" method="get" action="${compareUrl}"
                target="_blank">

                <c:if test="${displaytype == 'FULLVIEW'}">
                </c:if>
            </form>
        </c:when>

    </c:choose>

SearchPageController

    @SuppressWarnings("boxing")
        @RequestMapping(value = "/compare", method = RequestMethod.GET)
        public String compareProducts(@RequestParam(value = "prevPage", defaultValue = StringUtils.EMPTY) final String prevPage,
                final Model model, final HttpSession session, final HttpServletRequest request)
                throws CMSItemNotFoundException, UnsupportedEncodingException
        {
        // fetching product list from sesssion and returning Product Data map

    final Map<String, List<ProductClassificationData>> productClassDataMap = new HashMap<String, List<ProductClassificationData>>();
            final Map<String, HashSet<String>> prodClassMap = new TreeMap<String, HashSet<String>>();
            final String lastCode = null;

            final Cookie[] cookies = request.getCookies();
            String productList = null;
            boolean cookieFound = false;

            if (cookies != null)
            {
                logger.info("Cookies not null");
                for (final Cookie cookie : cookies)
                {
                    logger.info("Cookie name" + cookie.getName());
                    if (cookie.getName().equals("productList") && cookie.getValue() != null)
                    {
                        cookieFound = true;
                        productList = cookie.getValue().toString();
                        logger.info("Product List value" + cookie.getValue().toString());

                    }
                }
            }

    if (StringUtils.isNotBlank(productList))
            {
                final String[] values = productList.split("\\|");
                final String PRODUCT_COMPARE_PAGE = "productComparePage";
                storeCmsPageInModel(model, getContentPageForLabelOrId(PRODUCT_COMPARE_PAGE));
                setUpMetaDataForContentPage(model, getContentPageForLabelOrId(PRODUCT_COMPARE_PAGE));
                final List<ProductData> productDatalList = new ArrayList<ProductData>();
                final List<String> productCodesList = new ArrayList<String>();
                for (final String productcode : values)
                {
                    ProductData productData = null;
                    final List<ProductOption> options = new ArrayList<>(
                            Arrays.asList(ProductOption.BASIC, ProductOption.CLASSIFICATION, ProductOption.PRICE, ProductOption.STOCK));
                    if (productcode != null && "".equalsIgnoreCase(productcode))
                    {
                        productData = productFacade.getProductForCodeAndOptions(productcode, options);
                        productDatalList.add(productData);
                        productCodesList.add(productcode);
                    }
                }
    // fetch feature data and add it to model

    }

final int mapSize = finalDataList.size();
            model.addAttribute("finalDataList", finalDataList);
            model.addAttribute("mapSize", mapSize);
            model.addAttribute("compareCodeList", productCodesList);
            model.addAttribute("compareCodeListSize", productCodesList.size());
            model.addAttribute("prodDataList", productDatalList);
        }

        return getViewForPage(model);
   }

When the user clicks on "See comparison button" I am redirecting to a new page productComparePage.jsp where I want to display a full comparison of products with all its attributes. But right now I am getting redirected from Search Page controller to Component controller again. I get the Panel view again on this new page.

HybrisHelp
  • 5,518
  • 2
  • 27
  • 65
ADIT
  • 119
  • 1
  • 1
  • 9
  • 1
    I'm sure it can but there simply isn't enough detail provided for what you intend to compare to or from where? Please take a few minutes to help us help you by reading [ask] and [mcve] – charlietfl Jun 02 '18 at 15:05
  • I am getting a list of javascript objects -'Products' in my jsp file. I have to compare them as per user's selection. This is an ecommerce product compare feature on any B2C site – ADIT Jun 02 '18 at 15:07
  • 1
    That is still far too vague. I can't imagine you read those 2 links thoroughly in the last 2 minutes – charlietfl Jun 02 '18 at 15:09
  • I do not have a code snippet presently. But I can give an outline My JSP file - ProductList.jsp I can access all products here using {product},{product.productCode},{product.name} Now I have to write a jquery to pass every product detail for product comparison . So I define a checkbox for every product on productList Page which passes the product object to my jquery Next step is to add this detail to session . Suppose user selects Product 001 . I want the product Code, Name, Price to go inside user session so that I can add it to product Compare div. I hope its clear now? – ADIT Jun 02 '18 at 15:27
  • Please [edit the question](https://stackoverflow.com/posts/50658178/edit) with particulars not do a major update in comments. Learn how to use ajax to send data to server. – charlietfl Jun 02 '18 at 15:30

1 Answers1

1

You can use the cookie to store the object. You can find many examples on the internet. Like this, this. You should not store each product data in the cookie.

What I would suggest

  • Create product compare panel/component, which should always be there on the screen.
  • Whenever use selects any product, it should add to your compare panel and store that products code(SKU), name, image src in the cookie. Make sure that product compare panel is always display using cookies data, so even with the page refresh, it will work. Like the way, you have to handle remove event.

  • When a user clicks on compare button, you can make ajax call with the list of selected product code(from cookies) and get the product data object to render your compare page or simply submit the data to your controller and display the compare page.

However, plenty of open source plugin available which also can help you to get your job done.

HybrisHelp
  • 5,518
  • 2
  • 27
  • 65
  • Hello , I was able to create a component . It has two attributes 1. no. of products to be displayed 2. type of view -panel view/full view On click of 'Compare button I am redirecting to another page located at /search/compare which is supposed to have fullcomparison of products . This is another jsp page. However I am still getting inside componentcontroller and the panel view itself displays. My logic for fullcomparison is inside SearchPageController . Is it a right way to do ? – ADIT Jun 12 '18 at 14:49
  • Can't say anything until you provide all detail with the code snippet. – HybrisHelp Jun 13 '18 at 04:22
  • Hello , I have updated the question with code snippet. – ADIT Jun 13 '18 at 07:31
  • Don't mix your component with two view type. Let it be for just panel. Now when you click on compare button, user data should be submitted/fetch to your controller and render that data to the new page(cmsPage). If you don't want to redirect to the new page then you can use OOTB colorbox js to render the data as the popup. – HybrisHelp Jun 14 '18 at 05:02
  • Hello I am doing the same thing. However it gets redirected to componentcontroller and again the panel view renders // In my JSP
    ${displaytype}****************
    – ADIT Jun 15 '18 at 11:42
  • Contact me over the email, I will help you out. – HybrisHelp Jun 15 '18 at 13:31
  • Hi, I was able to redirect to the full view jsp page. – ADIT Jun 18 '18 at 14:11
  • Hi , Can you please tell why aren't we using the same controller for both the views (panel/fullview) ? – ADIT Jun 19 '18 at 14:52
  • The component controller is being invoked internally. Ideally, it only responsible to feed the data to its view. – HybrisHelp Jun 20 '18 at 05:01