【问题标题】:Unable to set jstree checkbox node to unchecked from JSON无法将 jstree 复选框节点设置为未从 JSON 中选中
【发布时间】:2011-03-17 16:45:19
【问题描述】:

我无法将有子节点的 CheckBox 设置为 unchecked["jstree-unchecked"]。当我使用firebug确定前端节点的类时,它显示“jstree-undetermined”。我真正想要的是“jstree-unchecked”成为类,因为这是我从服务器传递的。

以下是我使用 JSON 进行的 AJAX 调用。我还添加了从服务器获取的 JSON 数据。我在服务器端格式化 JSON 数据的方式有什么问题吗?
对于以下示例,“Dogs”节点导致问题。虽然“Dogs”的孩子没有被选中并且节点类是 jstree-unchecked;我看到“狗”节点被选中。非常感谢任何修复或改进代码的建议。

$(function () {
        $("#idTOC").bind("loaded.jstree", function (e, data) {
            data.inst.open_all(-1); // -1 opens all nodes in the container
        })
        .jstree({
            "json_data": {
                "ajax": {
                    "url": "http://localhost/eSiriusAjax/jqTOC.wc",
                    "aync": false,
                    "data": function (n) {
                        return { id: n.attr ? n.attr("id") : 0 };
                    }
                }
            },
            "core": { "html_titles": true },
            "themes": { "theme": "apple", "dots": false, "icons": false },
            "plugins": ["themes", "json_data", "checkbox"],
            "ui": { "select_limit": 1 }
        });

        $("#idTOC").bind('before.jstree', function (event, data) {
            if ((data.func === 'check_node') || (data.func === 'uncheck_node')) {
                event.stopImmediatePropagation(); //stops changing select/deselect when clicked
                return false;
            }
        });
    });

JSON 数据:

[
  {
    "data": "Intro",
    "attr": {
      "id": "1",
      "class": "jstree-checked",
      "href": "ProtocolIntro.aspx"
    }
  },
  {
    "data": "Overview",
    "attr": {
      "id": "3",
      "class": "jstree-unchecked",
      "href": "ProtocolOverview.aspx"
    }
  },
  {
    "data": "Funding",
    "attr": {
      "id": "5",
      "class": "jstree-checked"
    }
  },
  {
    "data": "Use TYPE(s)",
    "attr": {
      "id": "7",
      "class": "jstree-unchecked"
    }
  },
  {
    "data": "Transportation",
    "attr": {
      "id": "9",
      "class": "jstree-unchecked"
    }
  },
  {
    "data": "Dogs",
    "attr": {
      "id": "11",
      "class": "jstree-unchecked"
    },
    "children": [
      {
        "data": "Info",
        "attr": {
          "id": "12",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "# Justification",
        "attr": {
          "id": "13",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Choice Justification",
        "attr": {
          "id": "14",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Source",
        "attr": {
          "id": "15",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Enrichment/Excercise",
        "attr": {
          "id": "16",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Quarantine/Conditioning",
        "attr": {
          "id": "17",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Housing",
        "attr": {
          "id": "18",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Use Labs",
        "attr": {
          "id": "19",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Surgery",
        "attr": {
          "id": "20",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Procedures",
        "attr": {
          "id": "21",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Euthanasia",
        "attr": {
          "id": "22",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Methodology",
        "attr": {
          "id": "23",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "USDA Categories",
        "attr": {
          "id": "24",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Adverse Consq.",
        "attr": {
          "id": "25",
          "class": "jstree-unchecked"
        }
      }
    ]
  },
  {
    "data": "Mice",
    "attr": {
      "id": "26",
      "class": "jstree-unchecked"
    },
    "children": [
      {
        "data": "Info",
        "attr": {
          "id": "27",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "# Justification",
        "attr": {
          "id": "28",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Choice Justification",
        "attr": {
          "id": "29",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Source",
        "attr": {
          "id": "30",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Housing",
        "attr": {
          "id": "31",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Use Labs",
        "attr": {
          "id": "32",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Surgery",
        "attr": {
          "id": "33",
          "class": "jstree-checked",
          "href": "ProtocolSurgery.aspx"
        }
      },
      {
        "data": "Breeding",
        "attr": {
          "id": "34",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Methodology",
        "attr": {
          "id": "35",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "USDA Categories",
        "attr": {
          "id": "36",
          "class": "jstree-unchecked"
        }
      },
      {
        "data": "Adverse Consq.",
        "attr": {
          "id": "37",
          "class": "jstree-unchecked"
        }
      }
    ]
  },
  {
    "data": "Personnel",
    "attr": {
      "id": "38",
      "class": "jstree-checked",
      "href": "ProtocolPersonnel.aspx"
    }
  },
  {
    "data": "Database Searches",
    "attr": {
      "id": "40",
      "class": "jstree-unchecked"
    }
  },
  {
    "data": "Error Verification",
    "attr": {
      "id": "42",
      "class": "jstree-unchecked"
    }
  },
  {
    "data": "Preview",
    "attr": {
      "id": "44",
      "class": "jstree-unchecked"
    }
  },
  {
    "data": "Cancel Un-finished Protocol",
    "attr": {
      "id": "46",
      "class": "jstree-unchecked"
    }
  }
]

【问题讨论】:

    标签: jquery json checkbox jstree


    【解决方案1】:

    在 JSON 数据中,而不是 class="jstree-checked",您可以发送自定义属性,例如 "checked" : "true" | “检查”:“假”。

    然后在 ajax 请求中添加一个 complete 回调,在其中检查所有已检查属性设置为“true”的元素。

    “json_data”:{ “阿贾克斯”:{ "url": "http://localhost/eSiriusAjax/jqTOC.wc", “aync”:错误, “数据”:函数(n){ 返回 { id: n.attr ? n.attr("id") : 0 }; }, “完成”:函数(){ $('#idTOC li[checked=true]').each(function() { $.jstree._reference('#idTOC').check_node(this); }); } } }

    【讨论】:

      【解决方案2】:

      这对我有用 -

      .bind("load_node.jstree", function (event, data) { 
      
                      var productId = $("#Product_ProductId").val();
                      if (!productId || productId < 1) {
                          data.inst.hide_checkboxes();
                      } else
                          data.inst.change_state('li[selected=selected]', false);
                  })
      

      json数据-

      [{"data":{"title":"Electronics","icon":null},"metadata":{"id":3,"level":1,"leaf":false,"mapId":0},"state":"closed","attr":{"id":null,"rel":"folder","href":null,"selected":false},"children":[{"data":{"title":"Folder Name","icon":null},"metadata":{"id":28,"level":2,"leaf":true,"mapId":0},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":false},"children":[{"data":{"title":"New Category","icon":null},"metadata":{"id":29,"level":3,"leaf":true,"mapId":0},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":false},"children":[]}]},{"data":{"title":"Folder Name","icon":null},"metadata":{"id":30,"level":2,"leaf":false,"mapId":0},"state":"closed","attr":{"id":null,"rel":"folder","href":null,"selected":false},"children":[{"data":{"title":"File Name","icon":null},"metadata":{"id":31,"level":3,"leaf":false,"mapId":0},"state":"closed","attr":{"id":null,"rel":"folder","href":null,"selected":false},"children":[{"data":{"title":"File Name","icon":null},"metadata":{"id":32,"level":4,"leaf":true,"mapId":0},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":false},"children":[]}]},{"data":{"title":"Folder Name","icon":null},"metadata":{"id":33,"level":3,"leaf":false,"mapId":0},"state":"closed","attr":{"id":null,"rel":"folder","href":null,"selected":false},"children":[{"data":{"title":"New Category","icon":null},"metadata":{"id":34,"level":4,"leaf":true,"mapId":13},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":true},"children":[]}]}]}]},{"data":{"title":"Furniture","icon":null},"metadata":{"id":4,"level":1,"leaf":false,"mapId":0},"state":"closed","attr":{"id":null,"rel":"folder","href":null,"selected":false},"children":[{"data":{"title":"Folder Name","icon":null},"metadata":{"id":20,"level":2,"leaf":true,"mapId":0},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":false},"children":[]},{"data":{"title":"File Name","icon":null},"metadata":{"id":22,"level":2,"leaf":true,"mapId":14},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":true},"children":[]},{"data":{"title":"File Name","icon":null},"metadata":{"id":25,"level":2,"leaf":true,"mapId":0},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":false},"children":[]},{"data":{"title":"Folder Name","icon":null},"metadata":{"id":26,"level":2,"leaf":true,"mapId":12},"state":"","attr":{"id":null,"rel":"default","href":null,"selected":true},"children":[]}]}]
      

      在“attr”对象中添加一个“selected”属性

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-07
        • 1970-01-01
        • 2012-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-28
        相关资源
        最近更新 更多