0

I have a column in DataTables which holds some amounts and they are formatted like 1,200.00 and I want to get the max amount on the column. In order to do that I use the below code which I got a problem with at the moment if I have 22.50, 250.00, and 1,200.00 in the column it suppose to give me 1,200.00 as the max amount but it will bring back 250.00 as the biggest.

var maxPrice = table
        .column(2)
        .data()
        .sort(function(a,b){
            return a - b 
        })
        .reverse()[0];

1 Answer 1

1

You can use datatable api, the toArray() method.

Then use JS or jquery to parse the numbers and sort them.

Also use extractContent() function to get text instead of html.

        /**
        * Get Max Price
        */
        function extractContent(s) {
          var span = document.createElement('span');
          span.innerHTML = s;
          return span.textContent || span.innerText;
        };
        
        var PriceArray = [];
        $( table.column(2).data().toArray() ).each(function( index ) {
            PriceArray.push(extractContent(this).replace(',',''));
        });
        var maxPrice = PriceArray.sort(function(a,b){return a-b}).reverse()[0];
        console.log(maxPrice);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.