Programmation » Framework SLIM MVC
Catégorie:  
   
Framework SLIM MVC
Publié le 14/05/2016 @ 11:00:07,
Par keza
Bonjour,

Je suis nouveau et je suis en train de faire une api en utilisant le framework slim j'ai du mal pour sélectionner une ligne dans un tableau pour pouvoir les afficher dans les champs:

Quelqu'un pour m'aider ..... je vous remercie de votre aide


index.php
----------------

  1. <?php 
  2. // include  
  3. require 'Slim-2.6.0/Slim/Slim.php'
  4. require './models/model.php'
  5. \Slim\Slim::registerAutoloader(); 
  6. $app = new \Slim\Slim(); 
  7. $app->get('/contacts(/:id)''getContacts'); 
  8. $app->delete('/contacts/:id','deleteContact'); 
  9. $app->put('/contacts/:id','updateContact'); 
  10. $app->run(); 
  11. function getContacts($id=null) { 
  12. $response=\Slim\Slim::getInstance()->response(); 
  13. $response->headers->set('Content-Type''application/json'); 
  14. if (!isset($id)){ 
  15. $response->setStatus(200); 
  16. echo json_encode(Contacts::find_array()); 
  17. else 
  18. if ($contact = Contacts::find_one($id)) 
  19. $response->setStatus(200); 
  20. echo json_encode($contact->as_array()); 
  21. else 
  22. $response->setStatus(404); 
  23. function deleteContact($id) { 
  24. $response=\Slim\Slim::getInstance()->response(); 
  25. $response->headers->set('Content-Type''application/json'); 
  26. $contact = Contacts::find_one($id); 
  27. if ($contact) { 
  28. $contact->delete(); 
  29. $response->setStatus(200); 
  30. }else
  31. $reponse->setStatus(404); 
  32. function updateContact($id) { 
  33. $response=\Slim\Slim::getInstance()->response(); 
  34. $response->headers->set('Content-Type''application/json'); 
  35. $contact = Contacts::find_one($id); 
  36. if ($contact) { 
  37. $contact->delete(); 
  38. $response->setStatus(200); 
  39. }else
  40. $reponse->setStatus(404); 
  41. ?>


Model.js
----------------------

  1. $.extend({ 
  2. Contact:function(id,nom,prenom,email){ 
  3. this.data={ 
  4. "id":id, 
  5. "nom":nom, 
  6. "prenom":prenom, 
  7. "email":email 
  8. }; 
  9. }); 
  10. $.Contact.getAll=function(f){ 
  11. $.getJSON("/contacts/rest/contacts",null,function(data){ 
  12. var items=[]; 
  13. $.each(data,function(i,el){ 
  14. items.push(new $.Contact(el.id,el.nom,el.prenom,el.email));  
  15. }); 
  16. f(items); 
  17. }); 
  18. };





Controller.js
-------------------

  1. $.extend({ 
  2. Controller:function(view,contacts){ 
  3. var that=this
  4. this.selection=null
  5. contacts.getAll( 
  6. function(items){ 
  7. view.updateTable(items); 
  8. }); 
  9. });



Vieuw.js
---------------------

  1. $.extend({ 
  2. View:function(){ 
  3. var form=$('#form'); 
  4. var table=$('#ct'); 
  5. var moi=this
  6. this.sel=null
  7. this.updateTable=function(contacts){ 
  8. var matable=$("#ct").children("tbody").empty(); 
  9. $.each(contacts,function(i,contact){ 
  10. matable.append("<tr data-id='"+contact.data.id+"'>" 
  11. +"<td>"+contact.data.nom+"</td>" 
  12. +"<td>"+contact.data.prenom+"</td>" 
  13. +"<td>"+contact.data.email+"</td>" 
  14. +"</tr>" 
  15. );  
  16. }); 
  17. }; 
  18. this.listeners={ 
  19. clickAjouter:function(){}, 
  20. clickEditer:function(){}, 
  21. clickSupprimer:function(){}, 
  22. clickSelection:function(){}, 
  23. clickDeselection:function(){} 
  24. }; // listeners vide par défaut, que le 
  25.    // controleur complétera pour etre notifié 
  26.    // des événements dans la vue. 
  27.   
  28. });


table.html
--------------------

  1. <html lang="en"
  2. <head
  3. <meta charset="UTF-8" /> 
  4. <title></title
  5. <link href="./css/bootstrap.css" rel="stylesheet" /> 
  6. </head
  7. <body
  8. <div class="container"
  9. <div class="row"
  10. <div class="span8"
  11. <h4>Liste des contacts </h4
  12. <hr
  13. <table id="ct" class="table table-hover table-striped table-bordered table-condensed" cellspacing="0"
  14. <thead
  15. <tr
  16. <th>Nom</th
  17. <th>Prénom</th
  18. <th>Email</th
  19. </tr
  20. </thead
  21. <tbody
  22. </tbody
  23. </table
  24. <nav></nav
  25. </div
  26. <div class="span4"
  27. <form  id="form"
  28. <h4>Contact</h4
  29. <hr
  30. <label>Prénom</label
  31. <input id="prenom" name="prenom" placeholder="Prénom"  type="text"
  32. <label>Nom</label
  33. <input id="nom" name="nom" placeholder="Nom"  type="text"
  34. <label>Email</label
  35. <input id="email" name="email" placeholder="Email"  type="text"
  36. <br
  37. <button   id="supprimer" name="spprimer" class="btn btn-danger disabled"><i class="icon-trash icon-white"></i></button
  38. <button   id="editer" name="editer" class="btn btn-primary disabled"><i class="icon-pencil icon-white"></i></button
  39. <button  id="ajouter" name="ajouter" class="btn btn-success"><i class="icon-plus icon-white"></i></button
  40. </form
  41. <div id="message"></div
  42. </div
  43. </div>  
  44. </div
  45. <!--script src="//code.jquery.com/jquery-1.11.2.min.js"></script--> 
  46. <script src="./js/jquery-1.10.1.js"></script
  47. <script src="./js/model.js"></script
  48. <script src="./js/view.js"></script
  49. <script src="./js/controller.js"></script
  50. <script
  51. var model=$.Contact; 
  52. var view=new $.View(); 
  53. var controller=new $.Controller(view,model); 
  54. </script
  55. </body
  56. </html>
Fichier: table.png ( 15.5 KB - 957 )

Dernière édition: 17/05/2016 @ 15:50:09
   
Framework SLIM MVC
Publié le 17/05/2016 @ 15:50:57,
Par ovh
(HS pour zion: tiens, le coloriseur syntaxique ne supporte pas le doctype pour le format html :oh: )
Je n'ai rien à voir avec www.ovh.com
Répondre - Catégorie:  
Informaticien.be - © 2002-2024 AkretioSPRL  - Generated via Kelare
The Akretio Network: Akretio - Freedelity - KelCommerce - Votre publicité sur informaticien.be ?