2017-04-19 16 views
1

現在、sap.m.Inputフィールドを拡張して、ラベルの配置をスタイルして拡張できるようにしようとしています。 レンダリングは正常に動作しますが、何とかデータバインディングがプロセスで失われてしまい、なぜそれがわかりませんか。これは私のコントロールです:カスタムコントロール - データバインディングが機能しない

sap.ui.define([ 
    'sap/m/Input', 
], function(Input) { 
    'use strict'; 

    return Input.extend('one.sj.control.BhTextInput', { 
    metadata: { 
     properties: { 
     label: { 
      type: 'string', 
     }, 
     }, 
     aggregations: { 
      icon: { 
      type: 'sap.ui.core.Icon', 
      multiple: false, 
      visibility: 'public', 
      }, 
     }, 
    }, 

    renderer: function(oRM, oControl) { 
     oRM.write('<div class="formControl">'); 

     oRM.write('<input placeholder="'+oControl.getPlaceholder()+'"'); 
     oRM.write('type="'+oControl.getType()+'"'); 
     oRM.write('value="'+oControl.getValue()+'"'); 
     oRM.writeClasses(); 
     oRM.writeControlData(oControl); 
     oRM.write('/>'); 
     oRM.write('<label class="inputLabel" for="'+oControl.getId()+'"'); 
     oRM.write('>'); 
     oRM.renderControl(oControl.getIcon()); 
     oRM.write('<span class="inputLabelContent">'); 
     oRM.write(oControl.getLabel()); 
     oRM.write('</span>'); 
     oRM.write('</label>'); 

     oRM.write('</div>'); 
    }, 
    }); 
}); 

ご覧のとおり、非常に簡単です。 これは、私はそれを使用する方法:

<sj:BhTextInput 
    id="username" class="input textInput" 
    placeholder="{i18n>HINT_USERNAME}" value="{creds>/username}" 
    type="Text"> 
    <sj:icon> 
    <core:Icon src="sap-icon://email" class="inputIcon" /> 
    </sj:icon> 
</sj:BhTextInput> 

私は、私は上記のrenderer方法で手動<input/>建設を交換するとき、それは正常に動作しているが、私のモデルの問題ではありません確認して:

sap.m.InputRenderer.render(oRM, oControl); 

何か間違った点がありますか?ありがとう!

EDIT:「データバインディングが失われる」ということを少し明確にするために、私はこのような私のコントローラ内の入力フィールドにバインドされた値にアクセスするときに空の文字列を取得しています:getModel('creds').getProperty('/username');。これは、上で書かれた手動構築を置き換えるときに機能します。

答えて

0

これがあなたの問題の原因であるかどうかはわかりませんが、oRM.writeはレンダリングされたHTMLにスペースを追加しないと思います。属性を書き込むためには、oRM.writeAttributeを使用する方がよいでしょう。また、oRM.addClassを使用してクラスを追加する必要があります。

+0

ありがとうございました。私はwriteAttributeについて知らなかった。残念ながら、これは何も変わらない。私の現在の解決策は、InputRendererを使用することです。私は、Inputが実際にレンダリングされていないので、InputのonAfterRenderingも呼び出されていないと考えられます。 – puelo

+0

生成されたHTMLタグを共有できますか? – amiramw

0

それを動作させるために必要な変更がいくつかあります。

注1:InputBase API(sap.m.Inputの親)は、<input>タグの値を正しく取得するために"inner"を含むIDを持つ必要があります。これはINputBaseのAPIからです:

/** * Returns the DOM value respect to maxLength * When parameter is set chops the given parameter * * TODO: write two different functions for two different behaviour */

InputBase.prototype._getInputValue = function(sValue) { 
     sValue = (sValue === undefined) ? this.$("inner").val() || "" : sValue.toString(); 

     if (this.getMaxLength && this.getMaxLength() > 0) { 
      sValue = sValue.substring(0, this.getMaxLength()); 
     } 

     return sValue; 
    }; 

だから、すべての変更には、DOMの値を読み取り、コントロールのメタデータを更新します。

/** * Handles the change event. * * @protected * @param {object} oEvent * @returns {true|undefined} true when change event is fired */

InputBase.prototype.onChange = function(oEvent) { 

    // check the control is editable or not 
    if (!this.getEditable() || !this.getEnabled()) { 
     return; 
    } 

    // get the dom value respect to max length 
    var sValue = this._getInputValue(); 

    // compare with the old known value 
    if (sValue !== this._lastValue) { 

     // save the value on change 
     this.setValue(sValue); 

     if (oEvent) { 
     //IE10+ fires Input event when Non-ASCII characters are used. As this is a real change 
     // event shouldn't be ignored. 
      this._bIgnoreNextInputEventNonASCII = false; 
     } 


     // get the value back maybe formatted 
     sValue = this.getValue(); 

     // remember the last value on change 
     this._lastValue = sValue; 

     // fire change event 
     this.fireChangeEvent(sValue); 

     // inform change detection 
     return true; 
    } else { 
     // same value as before --> ignore Dom update 
     this._bCheckDomValue = false; 
    } 
}; 

だから、私はこのような制御のあなたのレンダラ方法を変更:

renderer: function(oRM, oControl) { 
     oRM.write('<div class=formControl'); 
     oRM.writeClasses(); 
     oRM.writeControlData(oControl); // let div handle control metadata such as id. 
     oRM.write(">") 
     oRM.write('<input placeholder="'+oControl.getPlaceholder()+'"'); 
     oRM.write('id="'+oControl.getId()+'-inner"'); // set id with 'inner' 
//  oRM.write('type="'+oControl.getType()+'"'); dont know why type is throwing error s=, so had to comment it. 
     oRM.write('value="'+oControl.getMyValue()+'"'); 
//  oRM.writeClasses(); 
//  oRM.writeControlData(oControl); 
     oRM.write('/>'); 
     oRM.write('<label class="inputLabel" for="'+oControl.getId()+'"'); 
     oRM.write('>'); 
     oRM.renderControl(oControl.getIcon()); 
     oRM.write('<span class="inputLabelContent">'); 
     oRM.write(oControl.getLabel()); 
     oRM.write('</span>'); 
     oRM.write('</label>'); 

     oRM.write('</div>'); 
} 

が、これはあなたのために働くなら、私に教えてください。 :)

関連する問題