Conditional DELETE

Conditional DELETE

nskwortsownskwortsow Posts: 120Questions: 0Answers: 0
edited November 2012 in Editor
Hi,
How would I show a DELETE button conditionally, based on the value of a (hidden) field?

E.g.

you cannot delete a user who has already logged in.

> where do I set the value (hidden) when the user last logged in
> how do I conditionally show the Delete button?

Thanks.

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    What you'd need to do is use the TableTools (assuming you are using TableTools) `fnRowSelected` method: http://datatables.net/extras/tabletools/initialisation#fnRowSelected . With that you can also use fnGetSelectedData to get the value for the row(s) and then use jQuery / DOM methods to set the visibility of the TableTools delete button. You'll also need a fnRowDeselected method which would fire when a row is unselected.

    This selector is what you could use to get the delete button:

    [code]
    $('a.DTTT_button').filter(":contains('Delete')")
    [/code]

    So the extension would be:

    [code]
    $('a.DTTT_button').filter(":contains('Delete')").css( 'display', data.loggedIn ? 'none' : 'block' );
    [/code]

    Allan
  • nskwortsownskwortsow Posts: 120Questions: 0Answers: 0
    Thanks, but the lastlogindate is not shown in the table, but is, rather, a hidden field in the editor.

    I trigger my editor with a .on("click",...) event. Ideally, I could test the condition of the value of this hidden field before showing the delete button?

    What do you suggest?
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    > Thanks, but the lastlogindate is not shown in the table, but is, rather, a hidden field in the editor.

    Which is presumably feed from your row's data source in the DataTable? So fnGetSelectedData will allow you to access that property. `row.lastlogindate` for example.

    > I trigger my editor with a .on("click",...) event. Ideally, I could test the condition of the value of this hidden field before showing the delete button?

    I don't really understand - are you not using a 'Delete' button in TableTools like that which is typically used in the examples?

    Allan
  • nskwortsownskwortsow Posts: 120Questions: 0Answers: 0
    edited November 2012
    The code below triggers the editor window to edit a record.

    It shows three buttons.

    Where & when would I call the code to hide the delete button if row.lastlogindate NOT NULL?

    /N

    [code]
    $('#tblOne tbody').on('click', function(e)
    {
    var $target = $(e.target);
    if ($target.hasClass('btn'))
    {
    // removed
    else
    {
    var $parent = $(e.target).parent();
    //console.log($parent[0]);
    editor.title('Edit');
    editor.edit($parent[0]);
    editor.buttons([
    {
    "label": "Cancel",
    "className": "btn btn-link",
    "fn": function()
    {
    editor.close();
    }
    }, {
    "label": "Delete",
    "className": "btn btn-inverse pull-left",
    "fn": function()
    {
    this.remove($parent[0], 'Delete?', {
    "label": "Confirm",
    "className": "btn",
    "fn": function()
    {
    this.submit();
    }
    });
    }
    }, {
    "label": "Save",
    "className": "btn",
    "fn": function()
    {
    editor.submit();
    }
    }]);
    }
    });
    [/code]
  • nskwortsownskwortsow Posts: 120Questions: 0Answers: 0
    I figured out another solution (ajax call inside the button); more reliable as well.
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    You'd just put an `if` condition around the buttons array would you not? If the condition is met add the delete button, if it isn't then just use the array with two buttons. Like the Editor fields on the server-side, the buttons array doesn't need to be static - manipulate it as you need :-)

    Allan
This discussion has been closed.