0

I am displaying data as per product specification or as per it's configuration.

Configuration display left side of page. When user select configuration the related data should be filtered. I have made category of attribute like Screen Size, RAM, HDD, Color, Size, Processor, optical drive etc. And under it I am displaying data like 2GB, 4GB, 2.2GHz, Blue, 320GB etc. enter image description here

But My problem is All Category display, if it's sub data exist or not. I don't want to display category if sub data not exist like Screen Size is blank then it should not be display.

My JSP Code :

<%
            List<String> attribNameList = (List<String>)request.getAttribute("attribName");
            List<String> attribValueList = (List<String>)request.getAttribute("attribValue");
            List<String> attribDataList = (List<String>)request.getAttribute("attribData");
            List<Integer> acfIDList = (List<Integer>)request.getAttribute("attribacID");

            List<Integer> acIDList = (List<Integer>)request.getAttribute("acID");
            List<String> acNAMEList = (List<String>)request.getAttribute("acNAME");


            String aname,aval,adata,acname;
            Integer acfid,acid;

            for( int i=0;i<acIDList.size();i++)
            {
                acid=acIDList.get(i);
                acname=acNAMEList.get(i);
                //Print Category
                %>
                    <a style="color: black;"><%= acname %></a><br>
                <%

                for(int i1=0;i1<attribNameList.size();i1++)
                {
                    aname = attribNameList.get(i1);
                    aval = attribValueList.get(i1);
                    adata = attribDataList.get(i1);
                    acfid = acfIDList.get(i1);                          

                    if(acid == acfid)
                    {
                        //Print Attribute
                        %><br>
                            <%-- <a><%= aname %></a> &nbsp; --%>
                            <a><%= aval %></a> &nbsp;
                            <%
                                if(adata == null)
                                {

                                }
                                else
                                {
                                    %>
                                        <a><%= adata %></a>
                                    <%
                                }

                    }
                }
                %>
                <br>
                <% 
            }

        %> 

Here `acIDList` is a attribute category id and `acnameList` is category name list and `acfIDList` is a `foreign key` in attribute_master table so I have put a if condition that when `acID` and `acfID` matches it creates List and under it sub data display.

and aval is attribute value that is 2,4,2.2, etc. and adata is for GB, GHz, MP, etc.

So I want to remove acName that is blank.

I dont want to display category name if data not exist so shall I have to put some condition on

<a style="color: black;"><%= acname %></a><br>

line. Any suggestion Please..

Java Curious ღ
  • 3,622
  • 8
  • 39
  • 63
  • change the if condition if(acid == acfid) to if(acid == acfid && !aval.equals("")) – jsjunkie Dec 10 '13 at 06:22
  • 3
    Just a side remark, in order to get a maintainable application consider to separate the view (i.e. jsp) from the application logic. – Henry Dec 10 '13 at 06:23
  • I think check the if(acid-- acfid &&!aval.isEmpty() && !aname.isEmpty()) – Neha Dec 10 '13 at 06:24
  • @Neha - I don't want to display main category Name i.e. acName if sub data not exist so i don't think that your suggestion is correct, my update of question see at last in question. – Java Curious ღ Dec 10 '13 at 06:31

3 Answers3

1

Change your If condition as below

if(adata != null)                                
{
    %>
    <%-- <a><%= aname %></a> &nbsp; --%>
    <a><%= aval %></a> &nbsp;
        <a><%= adata %></a>
    <%
}

Put your aname printing inside your if clause.

Also to remove acname printing add if condition as below.

//Print Category
if (attribNameList.size() !=  0
    %>
        <a style="color: black;"><%= acname %></a><br>
    <%
}

So your final code should look like something.

String aname,aval,adata,acname;
Integer acfid,acid;

for( int i=0;i<acIDList.size();i++) {

    ....
    ....

    //Print Category
    if (attribNameList.size() !=  0)
    { 
    %> 
      <a style="color: black;"><%= acname %></a><br>
    <%      
    }
    for(int i1=0;i1<attribNameList.size();i1++) {
      ....
      ....

      //Print Attribute
      if(adata != null)        
      {
      %>        
        <a><%= aval %></a> &nbsp;
        <a><%= adata %></a>             
      }
      %>
    <br>
<% 
}
%> 
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
1

I suggest that you try logic this way .. to solve your issue..

for( int i=0;i<acIDList.size();i++)
        {
           int acid=acIDList.get(i);
            acname=acNAMEList.get(i);
            //Print Category                
            int i = getIndex(acid);
       if(i != -1)              
       {        aname = attribNameList.get(i);
                aval = attribValueList.get(i);
                adata = attribDataList.get(i);
                //Print full data 
      if(!aval.equals("")) //add your cond to check empty ..
          {  %>
                <a style="color: black;"><%= acname %></a><br>
              <%
                    %><br>
                        <%-- <a><%= aname %></a> &nbsp; --%>
                        <a><%= aval %></a> &nbsp;
                        <%
                            if(adata == null)
                            {         /*add you msg or html in case of null*/                   }
                            else //here all html with content and design
                            {  %>   <a><%= adata %></a>
                                <%
                            }
                }  }
            %>   <br>   <% 
        }

This you can define above or any server util class ..

 function int getIndex(int acid) 
{
    for(int i1=0;i1<attribNameList.size();i1++)
            {
                if(acid == acfIDList.get(i1))
                { return il; }
            }
    return -1;      
}
Neha
  • 1,548
  • 2
  • 10
  • 13
  • What about the below snippet of code ? where I ll have to use it ? – Java Curious ღ Dec 10 '13 at 07:04
  • U need to write html inside the cond .. of if(!aval.qquals("")) {} ..and if you asking about getindex() you can define in jsp <% %> at start of jsp. refer - http://stackoverflow.com/questions/826932/declaring-functions-in-jsp – Neha Dec 10 '13 at 07:09
  • 1
    and use i already mention in code -- int i = getIndex(acid); this gives the index number of ur matched acid .. for this index u running loop .. take out this index separate than check any cond on the values of subctagory (index).. – Neha Dec 10 '13 at 07:12
  • How `getIndex` will work without any return type ? shall I have to give it a return type like `Integer`, etc. – Java Curious ღ Dec 10 '13 at 07:29
  • 1
    obviously and i modified it 17 mins ago with comment that i forget to write it..+ i edited my ans too.. – Neha Dec 10 '13 at 07:34
  • It gives me error under `int` in function declaration that `invalid assignment operator` and at end `getIndex(acid)` that `insert ; to complete statement.` – Java Curious ღ Dec 10 '13 at 07:37
  • Ok let me try it and inform you if i ll face any problem. I ll inform you soon. – Java Curious ღ Dec 10 '13 at 07:40
  • now more errors under `int` and `Integer` and after argument under Integer it shows misplaced constructs. – Java Curious ღ Dec 10 '13 at 08:40
  • 1
    its generic issues of programming .. its Integer class is not comparable to int .. so declare the Integer acid; -> int acid; .. and change the method signature to function getIndex(int acid).. – Neha Dec 10 '13 at 08:43
  • I try to give idea.. these general programming basic issues you should try to resolve urself.. :) – Neha Dec 10 '13 at 08:44
  • now it shows that ; expected on both side of argument means braces. it shows when i remove int as return type. – Java Curious ღ Dec 10 '13 at 08:46
  • where can I share a code ? on stackoverflow or on your mail account or anywhere else ? – Java Curious ღ Dec 10 '13 at 08:48
  • I have tried to put code inside <%!%> tag then error gone and 2 error remain that attribNameList can not resolved and remove int from declaration that is used as argument. – Java Curious ღ Dec 10 '13 at 08:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42842/discussion-between-java-curious--and-neha) – Java Curious ღ Dec 10 '13 at 08:53
  • Hello are you there ? I got a problem. – Java Curious ღ Dec 11 '13 at 05:17
  • I have added my problem to chat. – Java Curious ღ Dec 11 '13 at 05:33
  • Hello, will you see this once : http://stackoverflow.com/questions/20520014/how-to-give-different-id-to-textbox-run-time/20520151?noredirect=1 – Java Curious ღ Dec 12 '13 at 05:41
  • Hello, I have put a checkbox with each data attribute and want to filter data as per attribute I have almost done it but I a getting single problem that all acID are sent to servlet, I just want to send acID of checkbox which ae checked. Any suggestion please. – Java Curious ღ Dec 13 '13 at 06:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43096/discussion-between-java-curious--and-neha) – Java Curious ღ Dec 13 '13 at 06:26
0

not sure if I got this correct, but if you need to omit blank\null values, you can use the

<c:if test="${CONDITION}">
</c:if>

tag

secario
  • 456
  • 4
  • 14