Question

This below code is showing a button in datebox modal popup but I want clear the date when I will click on the button. I tried lots of things but not able to do it by jQuery method.

 <zk>
    <script>
    zk.afterLoad('zul.db', function () {
    var _xRenderer = {};
    zk.override(zul.db.Renderer, _xRenderer, {
    titleHTML: function (wgt, out, localizedSymbols) {
    _xRenderer.titleHTML.apply(this, arguments); //call the original method
    var uuid = wgt.uuid,
       view = wgt._view,
       text = wgt.getZclass() + '-ctrler';

    if(view == 'day') {
    out.push('&lt;button id="', uuid, '-today" class="', text, '"', 
    ' onClick="var cal = zk.Widget.$(\'$', wgt.parent.id, '\')._pop; cal._value = null; cal._setTime();"',
    ' &gt;', ' today', '&lt;/button&gt;');
    }
              out.push('&lt;button id="', uuid, '-clear" class="', text, '"', 
    ' onClick="alert(jq(this.parent.$n()))"',
    ' &gt;', ' clear', '&lt;/button&gt;');
    }
    });
    }); 
    </script>
    <datebox id="db" ></datebox>
    </zk>
Was it helpful?

Solution

In your case, the clear button is a child dom of datebox but not a child widget, this.parent.$n() only works if button is a child widget of datebox.

You can modify it to make it work as below:

  1. Get the id of datebox from the popup calendar (which id is datebox id + '-pp').
  2. Get datebox widget with id.
  3. Clear the value of input node of datebox, then call updateChange_ method of datebox.

I have slightly modified your sample:

<zk>
    <script><![CDATA[
    zk.afterLoad('zul.db', function () {
    var _xRenderer = {};
    zk.override(zul.db.Renderer, _xRenderer, {
    titleHTML: function (wgt, out, localizedSymbols) {
    _xRenderer.titleHTML.apply(this, arguments); //call the original method
    var uuid = wgt.uuid,
       view = wgt._view,
       text = wgt.getZclass() + '-ctrler';

    if(view == 'day') {
    out.push('<button id="', uuid, '-today" class="', text, '"', 
    ' onClick="var cal = zk.Widget.$(\'$', wgt.parent.id, '\')._pop; cal._value = null; cal._setTime();"',
    ' >', ' today', '</button>');
    }
              out.push('<button id="', uuid, '-clear" class="', text, '"', 
    ' onClick="clearDatebox(this)"',
    ' >', ' clear', '</button>');
    }
    });
    });
    function clearDatebox (btn) {
        var id = jq('.z-datebox-pp')[0].id.replace('-pp', ''),
            dbx = zk.Widget.$('#' + id);
        dbx.getInputNode().value = '';
        dbx.updateChange_();
    }
    ]]></script>
    <datebox id="db" ></datebox>
</zk>

OTHER TIPS

I have modified above link code then it's working.

version - zk 6.5.3

<zk>
    <script><![CDATA[
    zk.afterLoad('zul.db', function () {
    var _xRenderer = {};
    zk.override(zul.db.Renderer, _xRenderer, {
    titleHTML: function (wgt, out, localizedSymbols) {
    _xRenderer.titleHTML.apply(this, arguments); //call the original method
    var uuid = wgt.uuid,
       view = wgt._view,
       text = wgt.getZclass() + '-ctrler';

    if(view == 'day') {
    out.push('<button id="', uuid, '-today" class="', text, '"', 
    ' onClick="var cal = zk.Widget.$(\'$', wgt.parent.id, '\')._pop; cal._value = null; cal._setTime();"',
    ' >', ' today', '</button>');
    }
              out.push('<button id="', uuid, '-clear" class="', text, '"', 
    ' onClick="clearDatebox(this)"',
    ' >', ' clear', '</button>');
    }
    });
    });
    function clearDatebox (btn) {

     var str = btn.id;
    var res = str.substring(3,4); 

      if(res==0){
        var id = jq('.z-datebox-inp')[res].id,
            dbx = zk.Widget.$('#' + id);
        dbx.getInputNode().value = '';
        dbx.updateChange_();
      }else{
      var id = jq('.z-datebox-inp')[res/2].id,
            dbx = zk.Widget.$('#' + id);
        dbx.getInputNode().value = '';
        dbx.updateChange_();
      }
    }
    ]]></script>
    <datebox id="db" ></datebox>
  <datebox id="db1" ></datebox>
  <datebox id="db2" ></datebox>
  <datebox id="db3" ></datebox>
</zk>

or

change this method.

function clearDatebox (btn) {

      var d=jq('.z-datebox-pp').length-1;
        var id = jq('.z-datebox-pp')[d].id.replace('-pp', ''),
            dbx = zk.Widget.$('#' + id);
        dbx.getInputNode().value = '';
        dbx.updateChange_();
    }

for zk version 7.0 code here

      <zk>
        <script><![CDATA[
        zk.afterLoad('zul.db', function () {
        var _xRenderer = {};
        zk.override(zul.db.Renderer, _xRenderer, {
        titleHTML: function (wgt, out, localizedSymbols) {
        _xRenderer.titleHTML.apply(this, arguments); //call the original method
        var uuid = wgt.uuid,
           view = wgt._view,
           text = wgt.getZclass() + '-ctrler';

        if(view == 'day') {
        out.push('<button id="', uuid, '-today" class="', text, '"', 
        ' onClick="setDatebox(this)"',
        ' >', ' today', '</button>');
        }
                  out.push('<button id="', uuid, '-clear" class="', text, '"', 
        ' onClick="clearDatebox(this)"',
        ' >', ' clear', '</button>');
        }
        });
        });
        function clearDatebox (btn) {

         var str = btn.id;
        var res = str.substring(3,4); 

          if(res==0){
          document.getElementById(jq('.z-datebox-input')[res].id).value='';
          }else{
            document.getElementById(jq('.z-datebox-input')[res/2].id).value='';
          }
        }
           function setDatebox (btn) {
         var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!

        var yyyy = today.getFullYear();
        if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm}
 today = mm+'/'+dd+'/'+yyyy;

             var str = btn.id;
            var res = str.substring(3,4); 

             if(res==0){
             document.getElementById(jq('.z-datebox-input')[res].id).value=today;
             }else{
                document.getElementById(jq('.z-datebox-input')[res/2].id).value=today;
            }
        }

        ]]></script>
        <datebox id="db" ></datebox>
      <datebox id="db1" ></datebox>
      <datebox id="db2" ></datebox>
      <datebox id="db3" ></datebox>
    </zk>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top