How to Enable/Disable JEditable

喜夏-厌秋 提交于 2019-12-03 22:19:40

JEditable supports this natively at this point:

$.fn.editable = function(target, options) {

    if ('disable' == target) {
        $(this).data('disabled.editable', true);
        return;
    }
    if ('enable' == target) {
        $(this).data('disabled.editable', false);
        return;
    }

This seems to work:

$(...).editable("disable")
$(...).editable("enable")
Arman P.

Try to unbind previous event (probably dblclick) that jeditable added to the element, add your own with alert and then when the element is unlocked call jeditable again.

Another solution would be to add transparent div or something with absolute positioning on your elements when they're locked and bind dblclik event with your own message to that transpatrent elements. .remove() them when your original elements've been unlocked.

The problem I see in your code is: when first user dblclicks element you apply jeditable and lock it and when second user dbclicks it you want to alert, but the problem is that jeditable has already applied dblclick event to element. Instead of this change your check for lock to mouseenter event and if your condition for lock succeeds unbind dblclick event (see unbind), if nobody is editing simply apply jeditable, the function body of editText(). There is no need to have seperate function for it. And why are using .live(). Do you have dinamically added elements?

You can find also some answers to your question here JQuery JEditable - How to Disable on click editing

The solution I came up for you looks like this.

HTML file:

<html>
  <head>
    <title>Lock wirh jEditable</title>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://www.appelsiini.net/download/jquery.jeditable.mini.js"></script>
    <script type="text/javascript">
      $(function() {
        function makeEditable() {
          $('#editable').editable("save.php", {
            tooltip : 'Doubleclick to edit...',
            event : 'dblclick',
            onblur : 'ignore',
            callback : function(value, settings) {
              console.log('unlocked');
            },
            'onreset' : function() {
              console.log('unlocked');
              $.post('unlock.php');
            }
          });
        };

        makeEditable();

        $('#editable input').live('focus', function() {
          console.log('locked');
          $.post('lock.php');
        });

        $('#editable').hover(function() {
          $.post('checklock.php', function(response) {
            // USE SAME OPTION IN BOTH PLACES
            // IF YOU USE 1ST OPTION, YOU CAN TAKE JEDITABLE OUT OF makeEditable() and do everything without that function
            if (response) {
              // Option 1
              $('#editable').editable('disable');
              // Option 2
              //$('#editable').unbind('dblclick');
            } else {
              // Option 1
              $('#editable').editable('enable')
              // Option 2
              //makeEditable();
            }
          });
        });
      });
    </script>
  </head>

  <body>
    <div id="editable">Here is the text that can be edited.</div>
  </body>
</html>

LOCK.PHP

<?php
  $fileName = 'lock.txt';
  file_put_contents($fileName, '1');
?>

UNLOCK.PHP

<?php
  $fileName = 'lock.txt';
  file_put_contents($fileName, '0');
?>

CHECKLOCK.PHP

<?php
  $fileName = 'lock.txt';

  $locked = file_get_contents($fileName);
    if ($locked == '0') {
      echo false;
    } else {
      echo true;
    }
 ?>

SAVE.PHP

<?php   
  $fileName = 'lock.txt';
  file_put_contents($fileName, '0');

  echo $_POST['value'];
?>

LOCK.TXT (Initial)

0

You can grab the idea from the example. Of course you can do it more elegantly. This is rough example. You can try on your own simply by creating those files in same directory on your web server and copy-pasting contents. Sorry for not putting demo online, jsFiddle doesn't support ajax with change state (at least I think so). If you need any further assistance, let me know. Good Luck.

This does require a bit of messing with classes, and placement, but it works well. Here is the jquery UI based code to create a button that controls its own state and the state of the editable text

function makeEditable(params){ //params excluded for now
$("div#body").append("<button id='editTable'>Edit Table</button>"); //inserts a button into a div

$("div#body button#editTable").button({icons: {primary:'ui-icon-pencil'}}); //creates button
$("div#body button#editTable").unbind(); //removes all listeners
$("div#body button#editTable").hover(    //sets hover behaviour
    function(event){ //over
        $(this).addClass("ui-state-hover");
    }, 
    function(event){ //over
        $(this).removeClass("ui-state-hover");
    }
);

//uses a class called editText which is assigned to the td elements of the blocks you want to make editable

$("div#body table .editText").editable("http://www.blah.com/Controller/CEditTable.php", {
   indicator : 'Saving...',
   tooltip   : 'Click to edit...'
}); //basic editable initialization

$("div#body table .editText").editable("disable"); //disables by default

//adds a toggle to the button which enables/disables the editable functionality
$("div#body button#editTable").toggle(
   function(event){
       $("button#editTable").removeClass("ui-state-default");
       $("button#editTable").addClass("ui-state-active");
       $("div#body table .editText").editable("enable");
   },
   function(event){
        $("button#editTable").removeClass("ui-state-active");
        $("button#editTable").addClass("ui-state-default");
        $("div#body table .editText").editable("disable");
   }
);

$("div#body button#editTable").appendTo("div#body div#tableControls"); //move button to wherever to want it.
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!