How to display avatar icon with dynamically created name initials

前端 未结 4 445
眼角桃花
眼角桃花 2021-01-14 12:34

I have a requirement that, by passing a name, it should return an avatar icon which contains the first letters of the words contained in that name. For instance: if I pass

4条回答
  •  一向
    一向 (楼主)
    2021-01-14 13:27

    You could create a custom UI5-control for that. It supports databinding too :)

    Example on JSBin:

    var NameIcon = sap.ui.core.Control.extend("NameIcon", { // call the new Control type "NameIcon" and let it inherit
                                         // from sap.ui.core.Control
    
      // the Control API:
      metadata : {
          properties : {           // setter and getter are created behind the scenes, 
                                   // incl. data binding and type validation
              "name" : "string",   // in simple cases, just define the type
              "size" : {type: "sap.ui.core.CSSSize", defaultValue: "40px"} 
                                   // you can also give a default value and more
          }
      },
    
    
      // the part creating the HTML:
      renderer : function(oRm, oControl) { // static function, so use the given "oControl" instance 
                                           // instead of "this" in the renderer function
    
          oRm.write("");
          oRm.writeEscaped(oControl.getInitials()); // write another Control property, with protection 
                                                // against cross-site-scripting
          oRm.write("
    "); }, getInitials:function(){ var name = this.getName(); if (!name) return ""; var parts = name.split(" "); var result = parts.map(function(p){return p.charAt(0).toLocaleUpperCase();}).join(""); return result; }, // an event handler: onclick : function(evt) { // is called when the Control's area is clicked - no event registration required alert("Control clicked! Text of the Control is:\n" + this.getText()); } });

提交回复
热议问题