PHP中類似GWT的架構 PHP-EXT

來源:互聯網
上載者:User

http://php-ext.quimera-solutions.com/

 

  現在從事PHP網站開發的人員並不在少數,使用ExtJs的人更加不在少數,但很多PHP程式員並不熟悉Javascript,但又非常喜歡使用ExtJs.以前他們只能看著JAVA或者.NET的開發人員可以使用伺服器端的語言來寫ExtJs,現在PHP中也有了類似的框PHP-EXT .

下面我們來看一下簡單DataGrid的例子:

要實現上面的效果,不用寫一句js代碼.

view plaincopy to clipboardprint?
  1. <?php   
  2. /*   
  3.  * @author Matthias Benkwitz  
  4.  * @website <A href="http://www.bui-hinsche.de" target=_blank>http://www.bui-hinsche.de</A>
     
  5.  */    
  6. set_include_path(get_include_path().PATH_SEPARATOR.realpath('../../library'));   
  7. include_once 'PhpExt/Javascript.php';   
  8. PhpExt_Javascript::sendContentType();   
  9. $httpHost = "http://".$_SERVER['HTTP_HOST'];   
  10. $docRoot = str_replace("//","/",realpath($_SERVER['DOCUMENT_ROOT']));   
  11. $dir = str_replace("//","/",realpath(dirname(__FILE__)."/.."));   
  12. $baseUrl = str_replace($docRoot,$httpHost,$dir);   
  13.   
  14.   
  15.   
  16. include_once 'PhpExt/Ext.php';   
  17. include_once 'PhpExt/Data/SimpleStore.php';   
  18. include_once 'PhpExt/Data/ArrayReader.php';   
  19. include_once 'PhpExt/Data/JsonReader.php';   
  20. include_once 'PhpExt/Data/ScriptTagProxy.php';   
  21. include_once 'PhpExt/Data/FieldConfigObject.php';   
  22. include_once 'PhpExt/Data/StoreLoadOptions.php';   
  23. include_once 'PhpExt/Data/HttpProxy.php';   
  24. include_once 'PhpExt/Data/JsonStore.php';   
  25.   
  26. include_once 'PhpExt/Toolbar/PagingToolbar.php';   
  27. include_once 'PhpExt/Grid/ColumnModel.php';   
  28. include_once 'PhpExt/Grid/ColumnConfigObject.php';   
  29. include_once 'PhpExt/Grid/GridPanel.php';   
  30.   
  31.   
  32. $PageSize = 10;   
  33.   
  34.   
  35. $changeRenderer = PhpExt_Javascript::functionDef("change","if(val > 0){   
  36.             return '<span style=/"color:green;/">' + val + '</span>';   
  37.         }else if(val < 0){   
  38.             return '<span style=/"color:red;/">' + val + '</span>';   
  39.         }   
  40.         return val;",array("val"));   
  41. $pctChangeRenderer = PhpExt_Javascript::functionDef("pctChange","if(val > 0){   
  42.             return '<span style=/"color:green;/">' + val + '%</span>';   
  43.         }else if(val < 0){   
  44.             return '<span style=/"color:red;/">' + val + '%</span>';   
  45.         }   
  46.         return val;",array("val"));   
  47.   
  48.   
  49. $reader = new PhpExt_Data_JsonReader();   
  50. $reader->setRoot("topics")   
  51.        ->setTotalProperty("totalCount")   
  52.        ->setId("id");   
  53. $reader->addField(new PhpExt_Data_FieldConfigObject("company"));   
  54. $reader->addField(new PhpExt_Data_FieldConfigObject("price",null,"float"));   
  55. $reader->addField(new PhpExt_Data_FieldConfigObject("change",null,"float"));
      
  56. $reader->addField(new PhpExt_Data_FieldConfigObject("pctChange",null,"float"));   
  57. $reader->addField(new PhpExt_Data_FieldConfigObject("lastChange",null,"date","n/j h:ia"));
      
  58. $reader->addField(new PhpExt_Data_FieldConfigObject("industry"));   
  59.   
  60. // Store   
  61. $store = new PhpExt_Data_Store();   
  62. $store->setUrl($baseUrl.'/grid/json_exampledata.php')      
  63.    ->setReader($reader)   
  64.    ->setBaseParams(array("limit"=>$PageSize));   
  65.   
  66.   
  67.   
  68. // ColumnModel   
  69. $colModel = new PhpExt_Grid_ColumnModel();   
  70. $colModel->addColumn(PhpExt_Grid_ColumnConfigObject::createColumn("Company","company","company",160, null, null, true, false))
      
  71.          ->addColumn(PhpExt_Grid_ColumnConfigObject::createColumn("Price","price",null,75,null,PhpExt_Javascript::variable("Ext.util.Format.usMoney"), true, true))
      
  72.          ->addColumn(PhpExt_Grid_ColumnConfigObject::createColumn("Change","change",null,75,null,PhpExt_Javascript::variable('change'), null, true))
      
  73.          ->addColumn(PhpExt_Grid_ColumnConfigObject::createColumn("% Change","pctChange",null,75,null,PhpExt_Javascript::variable('pctChange'), null, true))
      
  74.          ->addColumn(PhpExt_Grid_ColumnConfigObject::createColumn("Last Updated","lastChange",null,85,null,PhpExt_Javascript::variable("Ext.util.Format.dateRenderer('m/d/Y')"), null, true))
      
  75.          ->addColumn(PhpExt_Grid_ColumnConfigObject::createColumn("Industry","industry",null,85,null,null, true, true));
      
  76.   
  77. // Grid   
  78. $grid = new PhpExt_Grid_GridPanel();   
  79. $grid->setStore($store)   
  80.      ->setColumnModel($colModel)   
  81.      ->setStripeRows(true)   
  82.      ->setAutoExpandColumn("company")   
  83.      ->setHeight(350)   
  84.      ->setWidth(600)   
  85.      ->setTitle("Json Grid");   
  86.   
  87.   
  88. $paging = new PhpExt_Toolbar_PagingToolbar();   
  89. $paging->setStore($store)   
  90.        ->setPageSize($PageSize)   
  91.        ->setDisplayInfo("Topics {0} - {1} of {2}")   
  92.        ->setEmptyMessage("No topics to display");   
  93. $grid->setBottomToolbar($paging);   
  94. // Ext.OnReady -----------------------   
  95. echo PhpExt_Ext::onReady(   
  96.     $changeRenderer,   
  97.     $pctChangeRenderer,   
  98.     $store->getJavascript(false, "store"),   
  99.     $store->load(new PhpExt_Data_StoreLoadOptions(array(   
  100.             "start"=>0,"limit"=>$PageSize))   
  101.     ),   
  102.     $grid->getJavascript(false, "grid"),   
  103.     $grid->render("grid-example")   
  104. );   
  105.   
  106.   
  107. ?> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.