Ho使用jtable插件在mvc中的点后用2位格式化十进制字段

Ho to format decimal field with 2 places after dot in mvc using jtable plug in

我无法格式化价格字段。这是我的看法。我从控制器发送记录作为 JSON 对象,它工作得很好。 我想将输出格式设置为小数点后 2 位,如 100.00.

<link href="~/Content/jtable.2.4.0/jtable.2.4.0/themes/metro/blue/jtable.css" rel="stylesheet" type="text/css">
<link href="~/Content/jquery-ui-themes-1.11.2/themes/start/jquery-ui.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="~/Scripts/jquery-1.11.1.js"></script>
<script type="text/javascript" src="~/Scripts/jquery-ui.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.jtable.js"></script>

<script type="text/javascript">
   $(document).ready(function() {

      var lokalizacijaSrpski = {
         serverCommunicationError: 'Greska u komunikaciji sa serverom.',
         loadingMessage: 'Ucitavanje...',
         noDataAvailable: 'Podaci nisu dostupni',
         addNewRecord: 'Dodaj novi slog',
         editRecord: 'Izmjeni',
         areYouSure: 'Da li ste sigurni?',
         deleteConfirmation: 'Da li ste sigurni da zelite da obrisete slog?',
         save: 'Sacuvaj',
         saving: 'Snimanje',
         cancel: 'Odustani',
         deleteText: 'Obrisi',
         deleting: 'Brisanje',
         error: 'Greska',
         close: 'Zatvori',
         cannotLoadOptionsFor: '{0} nemoguce ucitati!',
         pagingInfo: 'Ukupno {2}, prikaz {0} od {1}',
         gotoPageLabel: 'Idi na stranicu',
         pageSizeChangeLabel: 'Broj redova',
         canNotDeletedRecords: '{1} nemogucnost brisanja {0} sloga!'
      };

      $('#tXMLIndex').jtable({
         messages: lokalizacijaSrpski,
         title: 'XML PARSER',
         paging: true,
         pageSize: 10,
         sorting: true,
         actions: {
            listAction: '/XmlParser/ListXML'
         },
         fields: {
            XMLId: {
               title: 'Xml id',
               key: true,
               create: false,
               edit: false
            },
            Naziv: {
               title: 'Naziv'
            },
            Price: {
               title: 'Price'

            }
         },

         recordUpdated: function(event, data) {
            $('#tXMLIndex').jtable('load');
         }
      });

      $('#tXMLIndex').jtable('load');

      $.fn.focusTextToEnd = function() {
         this.focus();
         var $thisVal = this.val();
         this.val('').val($thisVal);
         return this;
      }
      $('#tbXMLIndexPretraga').focusTextToEnd();

      $("#tbXMLIndexPretraga").keyup(function() {
         $("#tXMLIndex").jtable('load', {
            searchString: $("#tbXMLIndexPretraga").val()
         });
      });
   });
</script>

<div id="tXMLIndex"></div>

有什么建议吗?

客户端:

我用 number_format.js 完成了数字格式化,效果很好。

如果您愿意将 js 添加到您的页面,您可以这样做:

  Price: { title: 'Price',
           edit: true,
           list: true,
           display: function (data) {
                      return number_format(data.record.price, 2, '.', ',');
                    }
         },

number_format 是这样的:

function number_format(number, decimals, dec_point, thousands_sep)

服务器端:

为了在这方面帮助您,您必须 post 一些代码。你的 JSON 长什么样? /XmlParser/ListXML 的结果是什么?

服务器端看起来像

public JsonResult ListXML(int jtStartIndex = 0,int jtPageSize = 0,字符串 jtSorting = null,字符串 searchString = null) {

        XmlReader readXML = new XmlReader();

        var data = readXML.RetrunListOfProducts();

        var lista = (from obj in data select new { XMLId=obj.ProductId,Naziv=obj.ProductName,Cjena=obj.ProductCost });



        if (searchString != null)
        {
            lista = lista.Where(b => b.Naziv.Contains(searchString));
        }

        if (jtSorting == null)
        {
            jtSorting = "XMLId DESC";
        }

        switch (jtSorting)
        {
            case "XMLId ASC":
                lista = lista.OrderBy(a => a.XMLId);
                break;
            case "Naziv ASC":
                lista = lista.OrderBy(a => a.Naziv);
                break;
            case "Naziv DESC":
                lista = lista.OrderByDescending(a => a.Naziv);
                break;

            case "Cjena ASC":

                //lista = from obj in lista
                //        orderby Convert.ToDecimal(obj.Cjena) ascending
                //        select new { XMLId=obj.XMLId,Naziv=obj.Naziv,Cjena=obj.Cjena };

                lista = from obj in lista
                        orderby obj.Cjena ascending
                        select new { XMLId = obj.XMLId, Naziv = obj.Naziv, Cjena = obj.Cjena };

                break;
            case "Cjena DESC":

                //lista = from obj in lista
                //        orderby Convert.ToDecimal(obj.Cjena) descending
                //        select new { XMLId = obj.XMLId, Naziv = obj.Naziv, Cjena = obj.Cjena };
                lista = from obj in lista
                        orderby obj.Cjena descending
                        select new { XMLId = obj.XMLId, Naziv = obj.Naziv, Cjena = obj.Cjena };


                break;



            default:
                lista = lista.OrderByDescending(a => a.XMLId);
                break;
        }

        var pagingLista = lista.Skip(jtStartIndex).Take(jtPageSize);

        return this.Json(
                          new
                          {
                              Result = "OK",
                              Records = pagingLista.ToList(),
                              TotalRecordCount = lista.Count()
                          }
                          , JsonRequestBehavior.AllowGet
                       );
    }