MySQL数据库:

CREATE DATABASE `tree`;

DROP TABLE IF EXISTS `mytree`;
CREATE TABLE `mytree` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'node_id',
  `text` varchar(20) NOT NULL COMMENT 'node_name',
  `parent_id` int(11) NOT NULL,
  `leaf` varchar(5) NOT NULL COMMENT 'true/false',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=95 DEFAULT CHARSET=latin1;

INSERT INTO `mytree` VALUES (36,'Folder 1',0,'false'),
(38,'Folder 2',0,'false'),(42,'Text 1',36,'true'),
(43,'Folder 3',0,'false'),(44,'Text 2',36,'true'),
(52,'Text 6',38,'true'),(57,'Text 5',38,'true'),
(58,'Text 4',38,'true'),(73,'Subfolder 1',36,'false'),
(74,'Text 7',73,'true'),(75,'Text 10',73,'true'),
(76,'Text 9',73,'true');


tree.php

<?php
mysql_connect("localhost", "root", "") or die("Could not connect");
mysql_select_db("tree") or die("Could not select database");

$parent_id = $_GET['node'];
$id = $_GET['id'];
    // 1) Remove false as checked
    $query = "SELECT id, text,parent_id, leaf FROM mytree WHERE parent_id='".$parent_id."' ORDER BY text ASC";
    $rs = mysql_query($query);
    $arr = array();
    while($obj = mysql_fetch_object($rs)) {

      // 2) If no leaf then destroy the attribute
      if($obj->leaf != "false"){
         // 1) set checked attribute with an boolean value
         $obj->checked = false;
      }
     $arr[] = $obj;
    }
    echo json_encode($arr);
?>


脚本:

Ext.onReady(function() {
    var store = Ext.create('Ext.data.TreeStore', {
        proxy: {
    type: 'ajax',
    url: 'tree.php',
    extraParams :{id:'38,43'},
    // Here Magic comes
    actionMethods: {
      create : 'GET',
      read   : 'GET',
      update : 'GET',
      destroy: 'GET'
    }
        },
        sorters: [{
            property: 'leaf',
            direction: 'ASC'
        }, {
            property: 'text',
            direction: 'ASC'
        }]
    });


题:

1)我只想显示树面板的根仅在id 38和43处。如果我们在.php中更改代码,则单击树面板时树无法扩展数据。所以,我不认为因此我们更改了代码在PHP脚本?如何只显示我想要的数据在树面板?
P / S:id的传递参数非常灵活,不仅38和43。

最佳答案

我认为这将满足您的需求:

   <?php
    mysql_connect("localhost", "root", "") or die("Could not connect");
    mysql_select_db("tree") or die("Could not select database");

    $parent_id = $_GET['node'];
    $id = $_GET['id'];
    // 1) Remove false as checked
    $query = "SELECT id, text,parent_id, leaf FROM mytree WHERE parent_id='".$parent_id."' ";
    if($parent_id == 0)
        $query = $query."AND id IN (".$id.") ";
    $query = $query."ORDER BY text ASC";
    $rs = mysql_query($query);
    $arr = array();
    while($obj = mysql_fetch_object($rs)) {

      // 2) If no leaf then destroy the attribute
      if($obj->leaf != "false"){
         // 1) set checked attribute with an boolean value
         $obj->checked = false;
      }
     $arr[] = $obj;
    }
    echo json_encode($arr);
?>

关于php - extjs 4:仅按特定ID显示树面板节点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14829098/

10-09 07:45
查看更多