简体   繁体   中英

How to allow numbers only in jqGrid cell editing?

When grid is editable accept only numbers when typing how to do it?

JSP grid load code:

<s:url id="mobbillid" action="newmul_mob_gridact" />   
    <sjg:grid caption="EMPLOYEE MOBILEBILL DETAILS"
              gridModel="mobbill_gridModel" 
              height="200"
              href="%{mobbillid}"
              id="gridtab"
              cellEdit="true"
              cellurl="%{mobbillid}"              
              rownumbers="true"
              viewrecords="true"
              pager="true"
              pagerPosition="center"
              navigator="true"
              navigatorSearch="true"
              navigatorSearchOptions="{multipleSearch:true}"
              navigatorDelete="false"
              navigatorEdit="false"
              loadonce="true"
              rowNum="10000"
              multiselect="true"
              reloadTopics="reloadSearchedClaims"
              footerrow="false"
              userDataOnFooter="true"
              onSelectRowTopics="rowselect"
            >
        <sjg:gridColumn name="newsin_mob_faname" index="newsin_mob_faname" title="FAcode" width="100" />
        <sjg:gridColumn name="newsin_mob_name" index="newsin_mob_name" title="FANAME" width="100" />
        <sjg:gridColumn name="newsin_mob_no" index="newsin_mob_no" title="MOBNO" width="100" />
        <sjg:gridColumn name="newsin_mob_billno" index="newsin_mob_billno" title="BILLNO" width="40" editoptions="true" editable="true"  />
        <sjg:gridColumn name="newsin_mob_billamt" index="newsin_mob_billamt" title="BILLAMT" width="90" editable="true" />
        <sjg:gridColumn name="newsin_mob_othchrg" index="newsin_mob_othchrg" title="OTHCHRG" width="95" editable="true" />
        <sjg:gridColumn name="newsin_mob_psts" index="newsin_mob_psts" title="refid" width="70" align="right"  hidden="true"/>
        <sjg:gridColumn name="newsin_mob_rmrk" index="newsin_mob_rmrk" title="REMARK" width="75" align="right"  editable="true"/>
        <sjg:gridColumn name="newsin_mob_opnbal" index="newsin_mob_opnbal" title="OPNBAL" width="75" align="right" hidden="true"/>
        </sjg:grid>  

You can use some jquery to handle this, check this fiddle out : http://jsfiddle.net/yrshaikh/44pc78pj/

You could have your editable textboxes to have a class called .allownumericwithdecimal and use the below code

 $(".allownumericwithdecimal").on("keypress keyup blur",function (event) {
     $(this).val($(this).val().replace(/[^0-9\.]/g,''));
            if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
                event.preventDefault();
            }
        });

If your textboxes of the grid are added dynamically you will need to do something like this

$(document).on('keypress keyup blur', '.allownumericwithdecimal', function () {
    $(this).val($(this).val().replace(/[^0-9\.]/g, ''));
    if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
        event.preventDefault();
    }
});

Jsp code:

<s:url id="mobbillid" action="newmul_mob_gridact" />   
<sjg:grid caption="EMPLOYEE MOBILEBILL DETAILS"
          gridModel="mobbill_gridModel" 
          height="200"
          href="%{mobbillid}"
          id="gridtab"
          cellEdit="true"
          cellurl="%{mobbillid}"              
          rownumbers="true"
          viewrecords="true"
          pager="true"
          pagerPosition="center"
          navigator="true"
          navigatorSearch="true"
          navigatorSearchOptions="{multipleSearch:true}"
          navigatorDelete="false"
          navigatorEdit="false"
          loadonce="true"
          rowNum="10000"
          multiselect="true"
          reloadTopics="reloadSearchedClaims"
          footerrow="false"
          userDataOnFooter="true"
          onSelectRowTopics="rowselect"
        >
    <sjg:gridColumn name="newsin_mob_faname" index="newsin_mob_faname" title="FAcode" width="100" />
    <sjg:gridColumn name="newsin_mob_name" index="newsin_mob_name" title="FANAME" width="100" />
    <sjg:gridColumn name="newsin_mob_no" index="newsin_mob_no" title="MOBNO" width="100" />
    <sjg:gridColumn name="newsin_mob_billno" index="newsin_mob_billno" title="BILLNO" width="40" editoptions="true" editable="true" editrules="{number:true}"/>
    <sjg:gridColumn name="newsin_mob_billamt" index="newsin_mob_billamt" title="BILLAMT" width="90" editable="true" edittype="text" editrules="{number:true}"/>
    <sjg:gridColumn name="newsin_mob_othchrg" index="newsin_mob_othchrg" title="OTHCHRG" width="95" editable="true"  editrules="{number:true}"/>
    <sjg:gridColumn name="newsin_mob_psts" index="newsin_mob_psts" title="refid" width="70" align="right"  hidden="true"/>
    <sjg:gridColumn name="newsin_mob_rmrk" index="newsin_mob_rmrk" title="REMARK" width="75" align="right"  editable="true" />
    <sjg:gridColumn name="newsin_mob_opnbal" index="newsin_mob_opnbal" title="OPNBAL" width="75" align="right" hidden="true"/>
    </sjg:grid>  

use editrules="{number:true}"

when focus out it trigger if it's alphabet show you error message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM