PHP Array with JsTree make node selected from a selected array

时间:2018-01-11 08:40:06

标签: php arrays jstree

i am trying to implement JsTree from where i am selecting some nodes to store in database. After once the node is selected and submitted to database, i want to keep those nodes selected after fetching it from database. Or you can say when i come to edit the form it should show me the selected categories with sub-categories selected in JsTree. Here is how i am trying to do it.

My JSON data of tree looks like this,

[{
    "id": 1,
    "name": "PHONE DEALS",
    "slug": "phone-deals",
    "featured_image": "",
    "slider_image": "",
    "featured_category": 0,
    "description": "",
    "type": "Product",
    "parent_id": null,
    "lft": 1,
    "rgt": 130,
    "depth": 0,
    "user_id": 5,
    "active": 1,
    "old_id": "1",
    "publish_at": "2017-12-29 12:04:30",
    "tax": 0,
    "created_at": "2017-12-29 06:34:30",
    "updated_at": "2018-01-02 12:34:46",
    "children": [{
        "id": 2,
        "name": "Apple",
        "slug": "apple",
        "featured_image": "",
        "slider_image": "",
        "featured_category": 0,
        "description": "",
        "type": "Product",
        "parent_id": 1,
        "lft": 2,
        "rgt": 27,
        "depth": 1,
        "user_id": 5,
        "active": 1,
        "old_id": "2",
        "publish_at": "2017-12-29 12:04:48",
        "tax": 0,
        "created_at": "2017-12-29 06:34:48",
        "updated_at": "2018-01-02 12:37:53",
        "children": [{
            "id": 3,
            "name": "iphone 5",
            "slug": "iphone-5",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 3,
            "rgt": 4,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "131",
            "publish_at": "2017-12-29 12:05:32",
            "tax": 0,
            "created_at": "2017-12-29 06:35:32",
            "updated_at": "2018-01-02 12:38:22",
            "children": []
        }, {
            "id": 5,
            "name": "iphone 4s",
            "slug": "iphone-4s",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 7,
            "rgt": 8,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "132",
            "publish_at": "2017-12-29 12:15:08",
            "tax": 0,
            "created_at": "2017-12-29 06:45:08",
            "updated_at": "2018-01-02 12:38:41",
            "children": []
        }, {
            "id": 6,
            "name": "iphone 5se",
            "slug": "iphone-5se",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 9,
            "rgt": 10,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "133",
            "publish_at": "2017-12-29 12:15:30",
            "tax": 0,
            "created_at": "2017-12-29 06:45:30",
            "updated_at": "2018-01-02 12:39:19",
            "children": []
        }, {
            "id": 7,
            "name": "iphone 5s",
            "slug": "iphone-5s",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 11,
            "rgt": 12,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "134",
            "publish_at": "2017-12-29 12:17:09",
            "tax": 0,
            "created_at": "2017-12-29 06:47:09",
            "updated_at": "2018-01-02 12:40:12",
            "children": []
        }, {
            "id": 8,
            "name": "iphone 5c",
            "slug": "iphone-5c",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 13,
            "rgt": 14,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "135",
            "publish_at": "2017-12-29 12:17:29",
            "tax": 0,
            "created_at": "2017-12-29 06:47:29",
            "updated_at": "2018-01-02 12:40:36",
            "children": []
        }, {
            "id": 9,
            "name": "iphone 6",
            "slug": "iphone-6",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 15,
            "rgt": 16,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "136",
            "publish_at": "2017-12-29 12:17:48",
            "tax": 0,
            "created_at": "2017-12-29 06:47:48",
            "updated_at": "2018-01-02 12:41:02",
            "children": []
        }, {
            "id": 10,
            "name": "iphone 6s",
            "slug": "iphone-6s",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 17,
            "rgt": 18,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "137",
            "publish_at": "2017-12-29 12:18:07",
            "tax": 0,
            "created_at": "2017-12-29 06:48:07",
            "updated_at": "2018-01-02 12:41:21",
            "children": []
        }, {
            "id": 11,
            "name": "iphone 6s plus",
            "slug": "iphone-6s-plus",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 19,
            "rgt": 20,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "138",
            "publish_at": "2017-12-29 12:18:25",
            "tax": 0,
            "created_at": "2017-12-29 06:48:25",
            "updated_at": "2018-01-02 12:41:39",
            "children": []
        }, {
            "id": 12,
            "name": "iphone 6 plus",
            "slug": "iphone-6-plus",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 21,
            "rgt": 22,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "139",
            "publish_at": "2017-12-29 12:18:42",
            "tax": 0,
            "created_at": "2017-12-29 06:48:42",
            "updated_at": "2018-01-02 12:42:17",
            "children": []
        }, {
            "id": 13,
            "name": "iphone 7",
            "slug": "iphone-7",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 23,
            "rgt": 24,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "140",
            "publish_at": "2017-12-29 12:19:04",
            "tax": 0,
            "created_at": "2017-12-29 06:49:04",
            "updated_at": "2018-01-02 12:42:44",
            "children": []
        }, {
            "id": 14,
            "name": "iphone 7 plus",
            "slug": "iphone-7-plus",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 2,
            "lft": 25,
            "rgt": 26,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "141",
            "publish_at": "2017-12-29 12:19:27",
            "tax": 0,
            "created_at": "2017-12-29 06:49:27",
            "updated_at": "2018-01-02 12:42:58",
            "children": []
        }]
    }, {
        "id": 16,
        "name": "Samsung",
        "slug": "samsung",
        "featured_image": "",
        "slider_image": "",
        "featured_category": 0,
        "description": "",
        "type": "Product",
        "parent_id": 1,
        "lft": 30,
        "rgt": 69,
        "depth": 1,
        "user_id": 5,
        "active": 1,
        "old_id": "4",
        "publish_at": "2017-12-29 12:23:23",
        "tax": 0,
        "created_at": "2017-12-29 06:53:23",
        "updated_at": "2018-01-02 12:43:15",
        "children": [{
            "id": 17,
            "name": "GALAXY j3",
            "slug": "galaxy-j3",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 31,
            "rgt": 32,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "154",
            "publish_at": "2017-12-29 12:23:44",
            "tax": 0,
            "created_at": "2017-12-29 06:53:44",
            "updated_at": "2018-01-02 12:43:30",
            "children": []
        }, {
            "id": 18,
            "name": "GALAXY j5",
            "slug": "galaxy-j5",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 33,
            "rgt": 34,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "155",
            "publish_at": "2017-12-29 12:24:05",
            "tax": 0,
            "created_at": "2017-12-29 06:54:05",
            "updated_at": "2018-01-02 12:43:59",
            "children": []
        }, {
            "id": 19,
            "name": "GALAXY j7",
            "slug": "galaxy-j7",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 35,
            "rgt": 36,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "156",
            "publish_at": "2017-12-29 12:24:24",
            "tax": 0,
            "created_at": "2017-12-29 06:54:24",
            "updated_at": "2018-01-03 06:21:33",
            "children": []
        }, {
            "id": 20,
            "name": "GALAXY note 2",
            "slug": "galaxy-note-2",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 37,
            "rgt": 38,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "157",
            "publish_at": "2017-12-29 12:24:42",
            "tax": 0,
            "created_at": "2017-12-29 06:54:42",
            "updated_at": "2018-01-02 12:45:01",
            "children": []
        }, {
            "id": 25,
            "name": "GALAXY S4",
            "slug": "galaxy-s4",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 47,
            "rgt": 48,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "166",
            "publish_at": "2017-12-29 12:26:34",
            "tax": 0,
            "created_at": "2017-12-29 06:56:34",
            "updated_at": "2018-01-02 12:47:06",
            "children": []
        }, {
            "id": 26,
            "name": "GALAXY S5",
            "slug": "galaxy-s5",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 49,
            "rgt": 50,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "167",
            "publish_at": "2017-12-29 12:26:51",
            "tax": 0,
            "created_at": "2017-12-29 06:56:51",
            "updated_at": "2018-01-02 12:47:25",
            "children": []
        }, {
            "id": 27,
            "name": "GALAXY S6",
            "slug": "galaxy-s6",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 51,
            "rgt": 52,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "168",
            "publish_at": "2017-12-29 12:27:11",
            "tax": 0,
            "created_at": "2017-12-29 06:57:11",
            "updated_at": "2018-01-02 12:48:02",
            "children": []
        }, {
            "id": 28,
            "name": "GALAXY S6 EDGE",
            "slug": "galaxy-s6-edge",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 53,
            "rgt": 54,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "169",
            "publish_at": "2017-12-29 12:27:30",
            "tax": 0,
            "created_at": "2017-12-29 06:57:30",
            "updated_at": "2018-01-02 12:48:28",
            "children": []
        }, {
            "id": 29,
            "name": "GALAXY S7 ",
            "slug": "galaxy-s7",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 55,
            "rgt": 56,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "171",
            "publish_at": "2017-12-29 12:27:52",
            "tax": 0,
            "created_at": "2017-12-29 06:57:52",
            "updated_at": "2018-01-02 12:50:04",
            "children": []
        }, {
            "id": 30,
            "name": "GALAXY S7 EDGE",
            "slug": "galaxy-s7-edge",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 57,
            "rgt": 58,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "172",
            "publish_at": "2017-12-29 12:28:14",
            "tax": 0,
            "created_at": "2017-12-29 06:58:14",
            "updated_at": "2018-01-02 12:50:59",
            "children": []
        }, {
            "id": 31,
            "name": "mega 6.3",
            "slug": "mega-63",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 59,
            "rgt": 62,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "173",
            "publish_at": "2017-12-29 12:28:40",
            "tax": 0,
            "created_at": "2017-12-29 06:58:40",
            "updated_at": "2018-01-02 12:51:39",
            "children": [{
                "id": 32,
                "name": "Blu life XL",
                "slug": "blu-life-xl",
                "featured_image": "",
                "slider_image": "",
                "featured_category": 0,
                "description": "",
                "type": "Product",
                "parent_id": 31,
                "lft": 60,
                "rgt": 61,
                "depth": 3,
                "user_id": 5,
                "active": 1,
                "old_id": "366",
                "publish_at": "2017-12-29 12:29:20",
                "tax": 0,
                "created_at": "2017-12-29 06:59:20",
                "updated_at": "2018-01-02 12:52:33",
                "children": []
            }]
        }, {
            "id": 33,
            "name": "Samsung S8 Plus",
            "slug": "samsung-s8-plus",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 63,
            "rgt": 64,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "312",
            "publish_at": "2017-12-29 12:29:50",
            "tax": 0,
            "created_at": "2017-12-29 06:59:50",
            "updated_at": "2018-01-02 12:53:10",
            "children": []
        }, {
            "id": 34,
            "name": "Samsung S8",
            "slug": "samsung-s8",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 65,
            "rgt": 66,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "313",
            "publish_at": "2017-12-29 12:30:09",
            "tax": 0,
            "created_at": "2017-12-29 07:00:09",
            "updated_at": "2018-01-02 12:53:36",
            "children": []
        }, {
            "id": 35,
            "name": "GALAXY S6 EDGE PLUS",
            "slug": "galaxy-s6-edge-plus",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 16,
            "lft": 67,
            "rgt": 68,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "335",
            "publish_at": "2017-12-29 12:30:26",
            "tax": 0,
            "created_at": "2017-12-29 07:00:26",
            "updated_at": "2018-01-02 12:53:53",
            "children": []
        }]
    }, {
        "id": 36,
        "name": "lg",
        "slug": "lg",
        "featured_image": "",
        "slider_image": "",
        "featured_category": 0,
        "description": "",
        "type": "Product",
        "parent_id": 1,
        "lft": 70,
        "rgt": 89,
        "depth": 1,
        "user_id": 5,
        "active": 1,
        "old_id": "33",
        "publish_at": "2017-12-29 12:31:18",
        "tax": 0,
        "created_at": "2017-12-29 07:01:18",
        "updated_at": "2018-01-02 12:54:09",
        "children": [{
            "id": 37,
            "name": "lg g2",
            "slug": "lg-g2",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 36,
            "lft": 71,
            "rgt": 72,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "142",
            "publish_at": "2017-12-29 12:31:39",
            "tax": 0,
            "created_at": "2017-12-29 07:01:39",
            "updated_at": "2018-01-02 12:54:51",
            "children": []
        }, {
            "id": 38,
            "name": "lg g3",
            "slug": "lg-g3",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 36,
            "lft": 73,
            "rgt": 74,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "143",
            "publish_at": "2017-12-29 12:32:00",
            "tax": 0,
            "created_at": "2017-12-29 07:02:00",
            "updated_at": "2018-01-02 12:55:14",
            "children": []
        }, {
            "id": 39,
            "name": "lg g4",
            "slug": "lg-g4",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 36,
            "lft": 75,
            "rgt": 76,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "144",
            "publish_at": "2017-12-29 12:32:19",
            "tax": 0,
            "created_at": "2017-12-29 07:02:19",
            "updated_at": "2018-01-02 12:55:29",
            "children": []
        }]
    }, {
        "id": 46,
        "name": "htc",
        "slug": "htc",
        "featured_image": "",
        "slider_image": "",
        "featured_category": 0,
        "description": "",
        "type": "Product",
        "parent_id": 1,
        "lft": 90,
        "rgt": 97,
        "depth": 1,
        "user_id": 5,
        "active": 1,
        "old_id": "34",
        "publish_at": "2017-12-29 12:34:56",
        "tax": 0,
        "created_at": "2017-12-29 07:04:56",
        "updated_at": "2018-01-02 12:57:15",
        "children": [{
            "id": 47,
            "name": "desire 530",
            "slug": "desire-530",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 46,
            "lft": 91,
            "rgt": 92,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "128",
            "publish_at": "2017-12-29 12:35:15",
            "tax": 0,
            "created_at": "2017-12-29 07:05:15",
            "updated_at": "2018-01-02 12:57:58",
            "children": []
        }, {
            "id": 48,
            "name": "Desire 626s",
            "slug": "desire-626s",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 46,
            "lft": 93,
            "rgt": 94,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "129",
            "publish_at": "2017-12-29 12:35:34",
            "tax": 0,
            "created_at": "2017-12-29 07:05:34",
            "updated_at": "2018-01-02 12:58:10",
            "children": []
        }, {
            "id": 49,
            "name": "DESIRE 510",
            "slug": "desire-510",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 46,
            "lft": 95,
            "rgt": 96,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "334",
            "publish_at": "2017-12-29 12:35:52",
            "tax": 0,
            "created_at": "2017-12-29 07:05:52",
            "updated_at": "2018-01-02 12:58:29",
            "children": []
        }]
    }, {
        "id": 50,
        "name": "sky",
        "slug": "sky",
        "featured_image": "",
        "slider_image": "",
        "featured_category": 0,
        "description": "",
        "type": "Product",
        "parent_id": 1,
        "lft": 98,
        "rgt": 103,
        "depth": 1,
        "user_id": 5,
        "active": 1,
        "old_id": "35",
        "publish_at": "2017-12-29 12:36:20",
        "tax": 0,
        "created_at": "2017-12-29 07:06:20",
        "updated_at": "2018-01-02 12:58:50",
        "children": [{
            "id": 51,
            "name": "sky 4.o d",
            "slug": "sky-4o-d",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 50,
            "lft": 99,
            "rgt": 100,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "174",
            "publish_at": "2017-12-29 12:36:40",
            "tax": 0,
            "created_at": "2017-12-29 07:06:40",
            "updated_at": "2018-01-02 12:59:13",
            "children": []
        }, {
            "id": 52,
            "name": "sky 5.o w",
            "slug": "sky-5o-w",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 50,
            "lft": 101,
            "rgt": 102,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "175",
            "publish_at": "2017-12-29 12:37:03",
            "tax": 0,
            "created_at": "2017-12-29 07:07:03",
            "updated_at": "2018-01-02 12:59:32",
            "children": []
        }]
    }, {
        "id": 53,
        "name": "zte",
        "slug": "zte",
        "featured_image": "",
        "slider_image": "",
        "featured_category": 0,
        "description": "",
        "type": "Product",
        "parent_id": 1,
        "lft": 104,
        "rgt": 111,
        "depth": 1,
        "user_id": 5,
        "active": 1,
        "old_id": "39",
        "publish_at": "2017-12-29 12:37:43",
        "tax": 0,
        "created_at": "2017-12-29 07:07:43",
        "updated_at": "2018-01-02 12:59:56",
        "children": [{
            "id": 54,
            "name": "zte avid plus",
            "slug": "zte-avid-plus",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 53,
            "lft": 105,
            "rgt": 106,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "151",
            "publish_at": "2017-12-29 12:38:04",
            "tax": 0,
            "created_at": "2017-12-29 07:08:04",
            "updated_at": "2018-01-02 13:00:14",
            "children": []
        }, {
            "id": 55,
            "name": "zte avid trio",
            "slug": "zte-avid-trio",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 53,
            "lft": 107,
            "rgt": 108,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "152",
            "publish_at": "2017-12-29 12:38:25",
            "tax": 0,
            "created_at": "2017-12-29 07:08:25",
            "updated_at": "2018-01-02 13:00:25",
            "children": []
        }, {
            "id": 56,
            "name": "zte zmax pro",
            "slug": "zte-zmax-pro",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 53,
            "lft": 109,
            "rgt": 110,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "153",
            "publish_at": "2017-12-29 12:38:57",
            "tax": 0,
            "created_at": "2017-12-29 07:08:57",
            "updated_at": "2018-01-02 13:00:44",
            "children": []
        }]
    }, {
        "id": 57,
        "name": "BLU PHONES",
        "slug": "blu-phones",
        "featured_image": "",
        "slider_image": "",
        "featured_category": 0,
        "description": "",
        "type": "Product",
        "parent_id": 1,
        "lft": 112,
        "rgt": 127,
        "depth": 1,
        "user_id": 5,
        "active": 1,
        "old_id": "176",
        "publish_at": "2017-12-29 12:39:19",
        "tax": 0,
        "created_at": "2017-12-29 07:09:19",
        "updated_at": "2018-01-02 13:01:12",
        "children": [{
            "id": 58,
            "name": "BLU DASH X2",
            "slug": "blu-dash-x2",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 57,
            "lft": 113,
            "rgt": 114,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "177",
            "publish_at": "2017-12-29 12:39:53",
            "tax": 0,
            "created_at": "2017-12-29 07:09:53",
            "updated_at": "2018-01-02 13:01:43",
            "children": []
        }, {
            "id": 64,
            "name": "Blu M7 Touch boom",
            "slug": "blu-m7-touch-boom",
            "featured_image": "",
            "slider_image": "",
            "featured_category": 0,
            "description": "",
            "type": "Product",
            "parent_id": 57,
            "lft": 125,
            "rgt": 126,
            "depth": 2,
            "user_id": 5,
            "active": 1,
            "old_id": "365",
            "publish_at": "2017-12-29 12:42:30",
            "tax": 0,
            "created_at": "2017-12-29 07:12:30",
            "updated_at": "2018-01-02 13:03:40",
            "children": []
        }]
    }]
}]

Now i have a separate SELECTED NODES array

[{
    "id": 10945,
    "product_id": 5099,
    "product_parent_id": 5099,
    "category_id": 1,
    "product_no": 2801,
    "created_at": "2018-01-11 13:53:05",
    "updated_at": "2018-01-11 13:53:05"
}, {
    "id": 10944,
    "product_id": 5099,
    "product_parent_id": 5099,
    "category_id": 5,
    "product_no": 2801,
    "created_at": "2018-01-11 13:53:00",
    "updated_at": "2018-01-11 13:53:00"
}, {
    "id": 10943,
    "product_id": 5099,
    "product_parent_id": 5099,
    "category_id": 3,
    "product_no": 2801,
    "created_at": "2018-01-11 13:53:00",
    "updated_at": "2018-01-11 13:53:00"
}, {
    "id": 10939,
    "product_id": 5099,
    "product_parent_id": 5099,
    "category_id": 2,
    "product_no": 2801,
    "created_at": "2018-01-11 13:32:00",
    "updated_at": "2018-01-11 13:32:00"
}]

And here is now i have this function which help to render the nodes and create the JSON in required format.

 public static function renderSelectedNode($node, $selectedNodes) {
        //dd($selectedNodes);
        $fullchild = [];
        $fulltext = $node->name;
        $fullid = $node->id;
        $fullparentid = $node->parent_id;
        $fullslug = $node->slug;

        if ( $node->children()->count() > 0 ) {
            foreach($node->children as $child){
                $fullchild[] = self::renderSelectedNode($child, $selectedNodes);
            }
        } 
        else
        {               
            foreach($selectedNodes as $selectedNode){
                if($node->id == $selectedNode['category_id']){
                    return $mainArray = ['text'=>$fulltext,'slug'=>$fullslug, 'id'=> $fullid  ,"parent_id"=> $fullparentid, 'state' => ['selected'=>true]];
                }
            }
            return $mainArray = ['text'=>$fulltext,'slug'=>$fullslug, 'id'=> $fullid  ,"parent_id"=> $fullparentid]; 
        }

        if(count($fullchild)>0) {
               return $mainArray = ['text'=>$fulltext ,'slug'=>$fullslug , 'id'=> $fullid ,"parent_id"=> $fullparentid,'children'=>$fullchild];

        }
        else{
            return $mainArray = ['text'=>$fulltext,'slug'=>$fullslug, 'id'=> $fullid  ,"parent_id"=> $fullparentid];
        }
    }

Now i am not sure why i am not getting the selected nodes properly. Specially the parent nodes which are selected like "Apple" and "Phone Deals" sub-child within can be selected easily. There is some problem with my conditions i am sure about it.

I want to add 'state' => ['selected' => true] to the selected nodes.

Thank you! (in advance)

1 个答案:

答案 0 :(得分:1)

您只选择没有孩子的节点,以便永远不会选择父母。

试试这个:

    var myArryValues = [
    "Hello World",
    "Javascript",
    "Jquery"
]

for (var i = 0; i < myArryValues.length; i++) {

    //New code 

    var s = $('<select class="myDropdown">');
    for (var j = 0; j < myArryValues.length; j++) {
        if (i == j) {
            $('<option />', {
                value: myArryValues[j],
                text: myArryValues[j],
                selected: 'selected'
            }).appendTo(s);
        } else {
            $('<option />', {
                value: myArryValues[j],
                text: myArryValues[j]
            }).appendTo(s);
        }
    }
    s.appendTo('.mySelect');

    var bookIndex = 0;

    $('#bookForm').on('click', '.mynewButton', function() {


            ///I had this code before it wasnt find anything
            $('.myDropdown').each(function(index) {
                console.log("operatorString", operatorString);
                var operatorCounter = 0;
                var optionCounter = 0;
                $(this).find('option').filter(function() {
                    if ($(this)[optionCounter] == myArryValues[operatorCounter]) {
                        $(this)[optionCounter].attr('selected', "selected");
                        operatorCounter++;
                    } else {
                        optionCounter++;
                    }

                });
            });
            console.log("mynewButton");
            bookIndex++;
            var bookForm = $('#bookForm');
            var $template = $('#bookTemplate'),
                $clone = $template
                .clone()
                .addClass('myDropdown-row')
                .removeClass('hide')
                .attr('data-book-index', bookIndex)
                .attr('id', '');

            bookForm.append($clone);

            // Update the name attributes
            $clone
                .find('[name="myDropdown"]').attr('name', 'myDropdown[' + bookIndex + '].myDropdown').end()

        })
        // Remove button click handler
        .on('click', '.removeButton', function() {
            $(this).parents('.form-group').remove();
        });