Create custom type

Create custom type

gbersacgbersac Posts: 5Questions: 2Answers: 0
edited September 2014 in Free community support

I want to create new value for the columns.type format.
For instance if I define a column

$('#' + idTable).dataTable( {
    data: content,
    columns: [{
        "data": "rate",
        "type": "percentage",
    }]
});

Every time I set type to percentage, I want it to :

  • call a specific render function for rendering the value (for instance multiply the value by 100 and append " %")
  • call specific function for searching and sorting.
  • why not automatically set specific option like searchable = false for percentage value.

How to archieve it ?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    call a specific render function

    There is no option for that via a type at the moment. You need to use columns.render to do that.

    call specific function for searching and sorting.

    It will. See the sorting legacy documentation (I'm still working on updating it for the new site style - but the old method still works).

    why not automatically set specific option like searchable = false for percentage value.

    I don't understand I'm afraid. Are you suggesting that the default for DataTables should be to disable searching of percentage values? I can't think of any reason that should be the default operation to be honest.

    Allan

  • gbersacgbersac Posts: 5Questions: 2Answers: 0
    edited September 2014

    First thanks for your answer.

    I don't understand I'm afraid. Are you suggesting that the default for DataTables should be to disable searching of percentage values? I can't think of any reason that should be the default operation to be honest.

    I am suggesting that type could be bound to specific default option.
    Let's say I create a custom type image. It should be great to state that, unless specified the contrary, image are not searcheable.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    I see - thanks for the clarification! To some degree that is possible already by proving a filtering plug-in for the set type - but I take your point, this is an area that could use some improvement! I've been thinking that for a while but not yet sure how to do it without breaking backwards compatibility... I'll keep thinking about it, but it could potentially fall into DataTables 2 which will break backwards compatibility (it is a way off though!).

    Allan

  • gbersacgbersac Posts: 5Questions: 2Answers: 0
    edited September 2014

    to delete

  • gbersacgbersac Posts: 5Questions: 2Answers: 0
    edited September 2014

    I am thinking about something like that :

    jQuery.fn.dataTableExt.newType({
        "name": "percentage"
        //The default value of the option searchable 
        //for the columns of this type
        "searchable": false,
        //Call each time a cell of this type is rendered
        //Let's say data is a double
        "render": function(data, type, row, meta){
            return (parseFloat(data) * 100).toFixed(2) + " %";
        },
        //Call each time a cell of this type is sort
        "sortup": function(a, b){
            return parseFloat(a) > parseFloat(b);
        },
        "sortdown": function(a, b){
            return parseFloat(a) < parseFloat(b);
        }
    })
    

    Then :

    $('#' + idTable).dataTable( {
        data: content,
        columns: [{"data": "myData", "type": "percentage", "title": "rate"}]
    });
    

    And the render and search function will authomatically be called for the column entitled rate !

    This looks like a simple extension of the functionnality already provided by data tables (maybe no backward compatibility breakdown), and would be quite handy.
    What do you think about it ?

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    It looks like a good idea - it would introduce several new code branches and indirection into the DataTables code, so I won't be implementing it immediately as it would be a lot of work. But I will certainly keep it in mind when I do get around to updating that part of the library.

    Allan

This discussion has been closed.