最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Render currency and symbol and combine with data from a different cell - Stack Overflow

programmeradmin8浏览0评论

I am working with latest jQuery DataTables v1.10.7 and I am trying to parse a number into the following format: $239.90 USD

I am being able make the currency working with this mand:

columns: [
    { data: "Price", render: $.fn.dataTable.render.number(',', '.', 2, '$') },

however the output of this is $239.90 without the USD

the USD should be ing from other data that I have in a different row named Currency: row['Currency']

What I'm trying to do is something like this:

columns: [
{
    data: "Price",
    render: function (data, type, row) {
       var v = $.fn.dataTable.render.number(',', '.', 2, '$');
       return data + ' ' + row['Currency'];
    }
},

but I don't understand how I can keep the result of the render into var v, this is not working.

I am working with latest jQuery DataTables v1.10.7 and I am trying to parse a number into the following format: $239.90 USD

I am being able make the currency working with this mand:

columns: [
    { data: "Price", render: $.fn.dataTable.render.number(',', '.', 2, '$') },

however the output of this is $239.90 without the USD

the USD should be ing from other data that I have in a different row named Currency: row['Currency']

What I'm trying to do is something like this:

columns: [
{
    data: "Price",
    render: function (data, type, row) {
       var v = $.fn.dataTable.render.number(',', '.', 2, '$');
       return data + ' ' + row['Currency'];
    }
},

but I don't understand how I can keep the result of the render into var v, this is not working.

Share Improve this question edited Jun 22, 2015 at 16:51 Gyrocode. 58.9k16 gold badges156 silver badges191 bronze badges asked Jun 22, 2015 at 15:47 user829174user829174 6,36225 gold badges81 silver badges132 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

Use the code below instead for column definition.

columns: [
{
    data: "Price",
    render: function (data, type, row, meta) {
       if(type === 'display'){
          var abbr = row['Currency'];

          var symbol = "";              
          if(abbr == "USD"){
             symbol = "$";

          } else if(abbr == "GBP"){
             symbol = "£";

          } else if(abbr == "EUR"){
             symbol = "€";
          }

          var num = $.fn.dataTable.render.number(',', '.', 2, symbol).display(data);              
          return num + ' ' + abbr;           
       } else {           
          return data;
       }
    }
},

See the example below for demonstration.

$(document).ready(function() {
   var table = $('#example').DataTable({
     'columns': [
       null,
       null,
       null,
       null,
       null,
       { 
         render: function(data, type, row, meta){
            if(type === 'display'){
               var abbr = "EUR";
              
               var symbol = "";              
               if(abbr == "USD"){
                 symbol = "$";
               
               } else if(abbr == "GBP"){
                 symbol = "£";
               
               } else if(abbr == "EUR"){
                 symbol = "€";
               }
                 
               var num = $.fn.dataTable.render.number(',', '.', 2, symbol).display(data);              
               return num + ' ' + abbr;           
              
            } else {
               return data;
            }
         }
       }
     ]
   });
});
<link href="http://datatables/release-datatables/media/css/jquery.dataTables.css" rel="stylesheet"/>

<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://datatables/release-datatables/media/js/jquery.dataTables.js"></script>

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>
 
    <tfoot>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </tfoot>
 
    <tbody>
        <tr>
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>320800</td>
        </tr>
        <tr>
            <td>Garrett Winters</td>
            <td>Accountant</td>
            <td>Tokyo</td>
            <td>63</td>
            <td>2011/07/25</td>
            <td>170750</td>
        </tr>
        <tr>
            <td>Ashton Cox</td>
            <td>Junior Technical Author</td>
            <td>San Francisco</td>
            <td>66</td>
            <td>2009/01/12</td>
            <td>86000</td>
        </tr>
    </tbody>
</table>

1 Answer

It's Work for me

{
  "data": "Quality", "name": "Quality", "autoWidth": true,
  render: function (data, type, row) {
          var iData = $.fn.dataTable.render.number(',').display(data); 
          return '<a class="editable editable-click fa fa-minus-square" style="color:firebrick"></a> ' +
                 '<span>' + iData + '</span> ' +
                 '<div class="fa fa-plus-square" style="color:forestgreen"></div>';
          }

  },
发布评论

评论列表(0)

  1. 暂无评论