change disabled td in a table with x-editable

0

I want to be able to make a table that disables me td depending on the select option that I select.

Example: If I select Wood1 the first and second td are enabled and the third one is disabled with the enabled button td next.

But if I select Wood2, the second and third are enabled (by removing the% enabled td button) and the first td is disabled with the% enabled td button.

And if I select Madera3 only the first td is enabled and the second one with the third one is disabled with the enabled button td next.

$('#table').editable({
        container: 'body',
        selector: 'td.task',
        title: 'task',
        type: "POST",
        showbuttons: false,
         type: 'text',
        validate: function(value) {
            if ($.trim(value) == '') {
                return 'Empty!';
            }
        },  
        success: function(response) {
  
           $(this).parent().find(".Item").click();
        }
    });
    var ITEM = [];
    $.each({
        "Item1": "Item1",
        "Item2": "Item2",
        "Item3": "Item3",
        "Item4": "Item4"
    }, function(k, v) {
        ITEM.push({
            value: k,
            text: v
        });
    });
    
    $('#table').editable({
        container: 'body',
        selector: 'td.Item',
        title: 'Item',
        type: "POST",
        showbuttons: false,
        source: ITEM,
        validate: function(value) {
            if ($.trim(value) == '') {
                return 'Empty!';
            }
        }
    });
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> 
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/js/bootstrap-editable.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/css/bootstrap-editable.css" rel="stylesheet"/>
    <div class="col-sm-3">
             <small>Clasificacion Producto</small>
        <select class="form-group browser-default custom-select" id="clasificacion">
           <option selected disabled>.::Clasificacion::.</option>
           <option value="Madera1">Madera1</option>
           <option value="Madera2">Madera2</option>
           <option value="Madera3">Madera3</option>
              
                        </select>
                      </div>
<table id="table" class="table table-bordered">
  <thead>
    <tr>
      <th>Id</th>
      <th>Task</th>
      <th>Item</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td data-name="task" class="task" data-type="text">001</td>
  <td data-name="Item" class="Item" data-type="select">Item2</td>
  <td></td>
  </tr>
    <tr>
  <td>2</td>
  <td data-name="task" data-disabled="true" class="task" data-type="text">002</td>
  <td data-name="Item" data-disabled="true" class="Item" data-type="select">Item1</td>
  <td>  <button id="enable" class="btn btn-sm btn-default">enable td</button></td>
  </tr>
   <tr>
  <td>3</td>
  <td data-name="task" class="task" data-type="text">003</td>
  <td data-name="Item" class="Item" data-type="select">Item3</td>
  <td></td>
  </tr>
  </tbody>
</table>

I tried to do it with a switch.

<script>
$("#clasificacion").change(function() {

        var valor = $(this).val(); // Capturamos el valor del select
        var texto = $(this).find('option:selected').text(); // Capturamos el texto del option seleccionado

  switch (texto) {
          case 'Madera1':
            var celdas = $('#table tbody > tr').find('td'); //ENCONTRAMOS EL TD
            break;
          case 'Madera2':
            var celdas = $('#table tbody > tr').find('td'); //ENCONTRAMOS EL TD
            break;
          case 'Madera3':
            var celdas = $('#table tbody > tr').find('td'); //ENCONTRAMOS EL TD
            break;

    });
</script>

I hope to have explained myself well. regards PS: Also if it is possible that the% enabled button td can remove the disabled from that td .

    
asked by MoteCL 08.11.2018 в 18:41
source

1 answer

1

Sincerely I had never worked with x-editable , the code does what you need, it has some details that you could correct yourself:

$('#table').editable({
    container: 'body',
    selector: 'td.task',
    title: 'task',
    type: "POST",
    showbuttons: false,
    type: 'text',
    validate: function(value) {
        if ($.trim(value) == '') {
            return 'Empty!';
        }
    },
    success: function(response) {

        $(this).parent().find(".Item").click();
    }
});
var ITEM = [];
$.each({
    "Item1": "Item1",
    "Item2": "Item2",
    "Item3": "Item3",
    "Item4": "Item4"
}, function(k, v) {
    ITEM.push({
        value: k,
        text: v
    });
});

$('#table').editable({
    container: 'body',
    selector: 'td.Item',
    title: 'Item',
    type: "POST",
    showbuttons: false,
    source: ITEM,
    validate: function(value) {
        if ($.trim(value) == '') {
            return 'Empty!';
        }
    }
});


$("#clasificacion").change(function() {

    var valor = $(this).val(); // Capturamos el valor del select
    var texto = $(this).find('option:selected').text();
    var opt1 = $(".opt1").data('blocked');
    var opt2 = $(".opt2").data('blocked');
    var opt3 = $(".opt3").data('blocked'); // Capturamos el texto del option seleccionado
    $(".btntd").remove();

    if (opt1 == true) {
        $(".opt1 > td.task").editable('option', 'disabled', false);
        $(".opt1 > td.Item").editable('option', 'disabled', false);
    }
    if (opt2 == true) {
        $(".opt2 > td.task").editable('option', 'disabled', false);
        $(".opt2 > td.Item").editable('option', 'disabled', false);
    }
    if (opt3 == true) {
        $(".opt3 > td.task").editable('option', 'disabled', false);
        $(".opt3 > td.Item").editable('option', 'disabled', false);

    }



    switch (texto) {
        case 'Madera1':
            //ENCONTRAMOS EL TD
            $(".opt3 > td.task").editable('option', 'disabled', true);
            $(".opt3 > td.Item").editable('option', 'disabled', true);
            $(".opt3").data('blocked', true);
            $(".opt2").data('blocked', false);
            $(".opt1").data('blocked', false);
            $(".opt3").append("<td class='btntd'><button id='enable' data-val='opt3' class='btn btn-sm btn-default'>enable td</button></td>");
            break;
        case 'Madera2':
            $(".opt1 > td.task").editable('option', 'disabled', true);
            $(".opt1 > td.Item").editable('option', 'disabled', true);
            $(".opt1").data('blocked', true);
            $(".opt2").data('blocked', false);
            $(".opt3").data('blocked', false);
            $(".opt1").append("<td class='btntd'><button id='enable' data-val='opt1' class='btn btn-sm btn-default'>enable td</button></td>"); //ENCONTRAMOS EL TD
            break;
        case 'Madera3':
            $(".opt2 > td.task").editable('option', 'disabled', true);
            $(".opt2 > td.Item").editable('option', 'disabled', true);
            $(".opt2").data('blocked', true);
            $(".opt1").data('blocked', false);
            $(".opt2").append("<td class='btntd'><button id='enable' data-val='opt2' class='btn btn-sm btn-default'>enable td</button></td>");
            $(".opt3 > td.task").editable('option', 'disabled', true);
            $(".opt3 > td.Item").editable('option', 'disabled', true);
            $(".opt3").data('blocked', true);

            $(".opt3").append("<td class='btntd'><button id='enable' data-val='opt3' class='btn btn-sm btn-default'>enable td</button></td>"); //ENCONTRAMOS EL TD
            break;
    }

});
$("#table > tbody > tr").on('click', "#enable", function(event) {
    var valor = $(this).data("val");

    $("." + valor + " > td.task").editable('option', 'disabled', false);
    $("." + valor + " > td.Item").editable('option', 'disabled', false);

});
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<script
   src="https://code.jquery.com/jquery-3.3.1.min.js"
   integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
   crossorigin="anonymous"></script> 
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/js/bootstrap-editable.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/css/bootstrap-editable.css" rel="stylesheet"/>
<div class="col-sm-3">
   <small>Clasificacion Producto</small>
   <select class="form-group browser-default custom-select" id="clasificacion">
      <option selected disabled>.::Clasificacion::.</option>
      <option value="opt1">Madera1</option>
      <option value="opt2">Madera2</option>
      <option value="opt3">Madera3</option>
   </select>
</div>
<table id="table" class="table table-bordered">
   <thead>
      <tr>
         <th>Id</th>
         <th>Task</th>
         <th>Item</th>
         <th></th>
      </tr>
   </thead>
   <tbody>
      <tr class="opt1">
         <td>1</td>
         <td data-name="task" class="task" data-type="text">001</td>
         <td data-name="Item" class="Item" data-type="select">Item2</td>
      </tr>
      <tr class="opt2">
         <td>2</td>
         <td data-name="task" class="task" data-type="text">002</td>
         <td data-name="Item" class="Item" data-type="select">Item1</td>
      </tr>
      <tr class="opt3">
         <td>3</td>
         <td data-name="task" class="task" data-type="text">003</td>
         <td data-name="Item" class="Item" data-type="select">Item3</td>
      </tr>
   </tbody>
</table>

Basically what it does, is that depending on the value you select in your select it will take the actions in the table, enable and disable the fields, in addition to the functionality of the button to enable the entire row.

You can simplify it and improve it as best suits you, this is just an example of how you can do it based on switch .

I hope and serve you, Regards.

    
answered by 09.11.2018 / 20:11
source