如何查看jquery中的条件?
How to check the condition in the jquery?
我正在创建 js 树来显示文件夹路径名称,我的问题是如果数据库 table status 为 0(不活动),如何检查条件然后将在 js 树中显示 line-through。 Else table status is 1(active) 刚刚恢复正常。以下是我的编码:
<?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>
现在我的输出显示了 js 树中的所有直通,而不是检测哪个是活动的或非活动的。
我在这里工作的 JSFiddle 代码:https://jsfiddle.net/ason5861_cs/9x0dsotz/3/
希望有人能指导我哪里错了。
查看您的代码。你没有比较它应该是 status == '0'
而不是 status = '0'
的状态
还有jstree提供的可用选项。 data
选项这可以是任何你想要的。它是您想要附加到点头的元数据。您以后可以随时访问和修改它 - 它对节点的视觉效果没有影响。
[{
"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 "";
};
我已经编辑了你的 fiddle https://jsfiddle.net/Hafizu/1xt7bhem/11/
我正在创建 js 树来显示文件夹路径名称,我的问题是如果数据库 table status 为 0(不活动),如何检查条件然后将在 js 树中显示 line-through。 Else table status is 1(active) 刚刚恢复正常。以下是我的编码:
<?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>
现在我的输出显示了 js 树中的所有直通,而不是检测哪个是活动的或非活动的。
我在这里工作的 JSFiddle 代码:https://jsfiddle.net/ason5861_cs/9x0dsotz/3/
希望有人能指导我哪里错了。
查看您的代码。你没有比较它应该是 status == '0'
而不是 status = '0'
还有jstree提供的可用选项。 data
选项这可以是任何你想要的。它是您想要附加到点头的元数据。您以后可以随时访问和修改它 - 它对节点的视觉效果没有影响。
[{
"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 "";
};
我已经编辑了你的 fiddle https://jsfiddle.net/Hafizu/1xt7bhem/11/