Search code examples
phpjquerydomstrikethrough

How to check the condition in the jquery?


I am create the js tree to show the folder path name, and my problem is how to check the condition if database table status is 0(inactive) then will show the line-through in the js tree. Else table status is 1(active) just back to normal. Below is my coding:

<?php 
   $folderData = mysqli_query($mysql_con,"SELECT * FROM filing_code_management");
   $folders_arr = array();
   while($row = mysqli_fetch_assoc($folderData)){
      $parentid = $row['parentid'];
      if($parentid == '0') $parentid = "#";

      $selected = false;$opened = false;
      if($row['id'] == 2){
         $selected = true;$opened = true;
      }
      $folders_arr[] = array(

         "id" => $row['id'],
         "parent" => $parentid,
         "text" => $row['name'] . ' ' . "<span id='category'>". $row['category']."</span>",
         "category" => $row['category'],
         "status" => $row['status'], // status 0 is inactive, status 1 is active
         "state" => array("selected" => $selected,"opened"=>$opened) 

      );
   }

   ?> 

<script style="text/javascript">
    var StrikeNodes = function(nodelist) {
      var tree = $('#folder_jstree').jstree(true);
      nodelist.forEach(function(n) {
        tree.get_node(n.id).a_attr.style = "text-decoration:" + getStrike(parseInt(n.text.substr(0, 3), 10));
        tree.redraw_node(n.id); //Redraw tree
        StrikeNodes(n.children); //Update leaf nodes
      });
    };

        var getStrike = function(i) {
      if (status = '0' ) {
        return "line-through;";
      }  else {
        return "";
      }
    };

    $('#folder_jstree').bind('load_node.jstree', function(e, data) {
      var tree = $('#folder_jstree').jstree(true);
      StrikeNodes(tree.get_json());
    });
</script>

Now my output show all the all the line-through in the js tree, not detect which is active or inactive. Output

My working JSFiddle code here: https://jsfiddle.net/ason5861_cs/9x0dsotz/3/

Hope someone can guide me which part I am getting wrong.


Solution

  • looking at your code. you are not comparing the status it should be status == '0' instead of status = '0'

    also there is available option that jstree provided. data option this can be anything you want. it is metadata you want attached to the nod. you will be able to access and modify it any time later - it has no effect on the visuals of the node.

    [{
        "id": "1",
        "parent": "#",
        "text": "100 PENTADBIRAN <span id='category'>JTM<\/span>",
        "category": "JTM",
        "data": { "status": 0 },
        "state": {
            "selected": false,
            "opened": false
        }
    }]
    
    var StrikeNodes = function(nodelist) {
      var tree = $('#folder_jstree').jstree(true);
      nodelist.forEach(function(n) {
          tree.get_node(n.id).a_attr.style = "text-decoration:" + getStrike(n.data.status);
          tree.redraw_node(n.id); //Redraw tree
          StrikeNodes(n.children); //Update leaf nodes
      });
    };
    
    var getStrike = function(status) {
        if (status == 0) {
           return "line-through;";
        }
        return "";
    };
    

    here's i've edited your fiddle https://jsfiddle.net/Hafizu/1xt7bhem/11/