javascript - How to make an id unique on page with JSF composite components? - Stack Overflow

I am making a ponent for the Javascript charting library called flot.<cc:interface><c

I am making a ponent for the Javascript charting library called flot.

<cc:interface>        
    <cc:attribute name="data" required="true" /> 
</cc:interface>

<cc:implementation>
    <div id="placeholder" style="width:600px;height:300px;"></div>
    <script type="text/javascript"> 
        //<![CDATA[
    $(function () {       
        
      var d1 = [#{cc.attrs.data}];     

        $.plot($("#placeholder"), [ d1 ]);

    });
    //]]>
    </script>
</cc:implementation>

This is the small amount of code I have so far. The problem I have is how would I make that div tag randomly generate on a page so that I can output multiple charts. Obviously it won't do that in the current state. I would need to pass the value into the javascript function to.

I know I can just create another attribute with id required and the user would have to specify the id, but I've noticed on a lot of ponents that the id is not required. It seems in heavy ajax/javascript libraries like primefaces and icefaces that the ids are random some how.

I am making a ponent for the Javascript charting library called flot.

<cc:interface>        
    <cc:attribute name="data" required="true" /> 
</cc:interface>

<cc:implementation>
    <div id="placeholder" style="width:600px;height:300px;"></div>
    <script type="text/javascript"> 
        //<![CDATA[
    $(function () {       
        
      var d1 = [#{cc.attrs.data}];     

        $.plot($("#placeholder"), [ d1 ]);

    });
    //]]>
    </script>
</cc:implementation>

This is the small amount of code I have so far. The problem I have is how would I make that div tag randomly generate on a page so that I can output multiple charts. Obviously it won't do that in the current state. I would need to pass the value into the javascript function to.

I know I can just create another attribute with id required and the user would have to specify the id, but I've noticed on a lot of ponents that the id is not required. It seems in heavy ajax/javascript libraries like primefaces and icefaces that the ids are random some how.

Share edited Nov 4, 2020 at 17:42 peterh 1 asked Mar 27, 2011 at 2:36 Drew HDrew H 4,7399 gold badges59 silver badges92 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can get the posite ponent's own ID by #{cc.id}. So to ensure uniqueness, just do:

<div id="#{cc.id}_placeholder" style="width:600px;height:300px;"></div>

and

$.plot($("##{cc.id}_placeholder"), [ d1 ]);

JSF will autogenerate one if you don't specify any id attribute on the ponent. E.g.

<my:plot id="foo">

Here foo will be used as #{cc.id} in the posite ponent implementation.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742403071a4437299.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信