mindmup / editable-table

tiny jQuery/Bootstrap widget that makes a HTML table editable
MIT License
686 stars 324 forks source link

Trying AJAX update of tables #19

Open xDaizu opened 9 years ago

xDaizu commented 9 years ago

First of all, I like your plugin very much, it's easy and simple. I wanted to tweak it a little bit so it updates the server-side data through ajax on change.

For that I made my .on("change") return a deferred object and changed your code to expect that.

Changed from:

 if (evt.result === false) {
            active.html(originalContent);
           }

to:

jQuery.when(evt.result)
    .done(function (result) {
                if (result !== null && typeof result === 'object') {
                    //if it's an object
                    if (result.success) {
                        //if the property exist or has a truthy value
                        return true;
                    }
                } else {
                    //it's a value or null
                    if (result) {
                        //if it has a truthy value
                        return true;
                    }
                }
               //it has a falsey value
                active.html(originalContent);
            })
    .fail(function () {
                active.html(originalContent);
            });

The beauty of using ".when()" is that it keeps working if you return a not deferred object. For instance, false, so it doesn't break your current functionality, but I have a problem I don't know how to solve.

The originalContent attribute gets overwritten, so if I change a cell and quickly change another cell and both fail, the 2nd one will get the originalContent of the 1st one, and the 1st one will keep the new content.

Any idea about how to fix this? I think that if we manage to, this would be a great addition to your already excellent widget. :)