Column rendering depending on editing state

Column rendering depending on editing state

TitaniaTitania Posts: 6Questions: 3Answers: 0

Is it possible to render column depending on some state/attribute/etc?

I have table that shows only id and name, but it also has EDIT button in the last column.
I would like to render column with name attribute like an input element with name value when EDIT button is selected and like normal text otherwise. How can I achieve this kind of behavior?

Answers

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    Sure - use a renderer. You can use it as a function, so you can use whatever logic conditions you want in it.

    Allan

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    I would like to render column with name attribute like an input element with name value when EDIT button is selected and like normal text otherwise.

    Hmmm - actually, that would require a rerender on each state change. You could call rows().invalidate() to do that if that is what is needed.

    Allan

  • TitaniaTitania Posts: 6Questions: 3Answers: 0

    Thank you for your answer. I managed to do it using render function and rows().invalidate() but I have one more question. Currently I'm using some variable "editing" defined outside table definition and my render function looks like this:

    columns: [
     {data : 'id'},
     {
        data : 'name',
        render: function (data, type, row) {
                        if(editing) {
                            var $input = $('<input value="'+ row.name +'">').addClass('form-control');
                            return $('<div>').append($input).html();
                        } else {
                            return row.name;
                        }
                    }
       }
    
       .....
    ]
    })
    

    Is there any better way than using variable? I wanted to add class "editing" to <tr> element being edited but I don't have reference to it in render function.

    Thank you!

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    The only other way would be to add an editing parameter to the row's data object. As you say, the render function doesn't really have access to the node (since it is possible that it will be called before the node exists).

    Allan

This discussion has been closed.