SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    code help needed

    hi
    i have just started to learn js/ajax and would be grateful if someone could show me how to close this function with correct brace placement etc. i have tried loads of variations but cannot get it to work. many thanks

    Code:
    function doCommand(com, grid) {
    if (com == 'Edit') {
    $('.trSelected', grid).each(function() {
    var id_con = $(this).attr('id_con');
    id_con = id_con.substring(id_con.lastIndexOf('row')+3);
    alert("Edit row " + id_con);
    });
    } else if (com == 'Delete') {
    $('.trSelected', grid).each(function() {
    var id_con = $(this).attr('id_con');
    id_con = id_con.substring(id_con.lastIndexOf('row')+3);
    alert("Delete row " + id_con);

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Code javascript:
    function doCommand(com, grid) {
      if (com == 'Edit') {
        $('.trSelected', grid).each(function() {
          var id_con = $(this).attr('id_con');
          id_con = id_con.substring(id_con.lastIndexOf('row')+3);
          alert("Edit row " + id_con);
        });
      } else if (com == 'Delete') {
        $('.trSelected', grid).each(function() {
          var id_con = $(this).attr('id_con');
          id_con = id_con.substring(id_con.lastIndexOf('row')+3);
          alert("Delete row " + id_con);
        });
      }
    }

    As far as I can see you just missed the last }) and the last two }



    PS. You can use [highlight="javascript"][/highlight] instead of [code][/code] to get JavaScript highlighted (and indented) code in your post
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    scallio
    thanks for that. however, the function causes an errror of undefined or not an object. i have pasted the code i am using from the tutorial and would be grateful if you could explain the error please. many thanks for your time.

    <script type="text/javascript">
    $(function() {
    $("#flex1").flexigrid(
    {
    url: 'staff.php',
    dataType: 'json',
    colModel : [
    {display: 'ID', name : 'id_con', width : 40, sortable : true, align: 'left'},
    {display: 'Name', name : 'name_con', width : 150, sortable : true, align: 'left'},
    {display: 'Email', name : 'email_con', width : 150, sortable : true, align: 'left'},
    {display: 'Phone', name : 'phone_con', width : 250, sortable : true, align: 'left'},
    {display: 'Mobile', name : 'mobile_con', width : 150, sortable : true, align: 'left'},
    {display: 'Fax', name : 'fax_con', width : 150, sortable : true, align: 'left'},
    {display: 'Notes', name : 'notes_con', width : 250, sortable : true, align: 'left'}
    ],
    buttons : [
    {name: 'Edit', bclass: 'edit', onpress : doCommand},
    {name: 'Delete', bclass: 'delete', onpress : doCommand},
    {separator: true}
    ],
    searchitems : [
    {display: 'Name', name : 'name_con'},
    {display: 'Email', name : 'email_con', isdefault: true},
    {display: 'Position', name : 'position'}
    ],
    sortname: "name_con",
    sortorder: "asc",
    usepager: true,
    title: "Staff",
    useRp: true,
    rp: 10,
    showTableToggleBtn: false,
    resizable: false,
    autowidth: true,
    autoheight: true,
    singleSelect: true
    }

    );
    });
    function doCommand(com, grid) {
    if (com == 'Edit') {
    $('.trSelected', grid).each(function() {
    var id_con = $(this).attr('id_con');
    id_con = id_con.substring(id_con.lastIndexOf('row')+3);
    alert("Edit row " + id_con);
    });
    } else if (com == 'Delete') {
    $('.trSelected', grid).each(function() {
    var id_con = $(this).attr('id_con');
    id_con = id_con.substring(id_con.lastIndexOf('row')+3);
    alert("Delete row " + id_con);
    }); }}
    </script>

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by ploppy View Post
    i have pasted the code i am using from the tutorial and would be grateful if you could explain the error please.
    Which tutorial?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    The code looks fine to me.
    Did you include jQuery and flexigrid.js in the head of your HTML document?
    (they need to go before all this code)
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    Code:
    <script src="js/jquery.js"></script>
    <script src="js/flexigrid.js"></script>
    <link rel="stylesheet" href="css/flexigrid/flexigrid.css" type="text/css" />
    the main grid is working just not the edit or delete functions. this is what firebug reports.

    Code:
    id_con is undefined
    id_con = id_con.substring(id_con.lastIndexOf('row')+3);
    thanks

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Put var in front of it (in both instances):

    Code:
    var id_con = id_con.substring(id_con.lastIndexOf('row')+3);
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still the same error.

    id_con is undefined
    var id_con = id_con.substring(id_con.lastIndexOf('row')+3);

    thanks

  10. #10
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Then the elements with class trSelected don't have an attribute id_con. i.e., the error is in the HTML.

    There should be lines like these:

    <td class="trSelected" id_con="some_value">...</td>

    And it seems id_con="some_value" is missing from your HTML.

    If you're not sure what I mean please post the HTML.

    PS. You can ignore post #8 from me. Turns out what I said there is not (entirely) true (although it'll still work afaik)
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  11. #11
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <tr class="trSelected"><td align="left" class="sorted"><div style="text-align: left; width: 40px;">3</div></td><td align="left"><div style="text-align: left; width: 150px;">spme user</div></td><td align="left"><div style="text-align: left; width: 150px;">&nbsp;</div></td><td align="left"><div style="text-align: left; width: 250px;">000-000-0000</div></td><td align="left"><div style="text-align: left; width: 150px;">&nbsp;</div></td><td align="left"><div style="text-align: left; width: 150px;">&nbsp;</div></td><td align="left"><div style="text-align: left; width: 250px;">&nbsp;</div></td></tr>
    the 3 in the above code is the id_con value. is this what you wanted? thanks

  12. #12
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thought i would try another method which i found. but still the same error.

    Code:
    function test(com,grid) 
                {
               if($('.trSelected',grid).length>0){
    		   if(confirm('Delete ' + $('.trSelected',grid).length + ' items?')){
                var items = $('.trSelected',grid);
                var itemlist ='';
            	for(i=0;i<items.length;i++){
    				itemlist+= items[i].id_con.substr(3)+",";
    			}
    			$.ajax({
    			   type: "POST",
    			   dataType: "json",
    			   url: "delete1.php",
    			   data: "items="+itemlist,
    			   success: function(data){
    			   	alert("Query: "+data.query+" - Total affected rows: "+data.total);
    			   $("#flex1").flexReload();
    			   }
    			 });
    			}
    
                    else if (com=='Add') 
                        { 
                            alert('Add New Item'); 
                        } 
    					else if (com=='Edit') 
                        { 
                            alert('Edit Item'); 
                        } 
                } 
    
    
            $('b.top').click 
            ( 
                function () 
                    { 
                        $(this).parent().toggleClass('fh'); 
                    } 
            ); 
    
    }
    this is a part of what firebug is passing

    Code:
    "page":"1","total":"26","rows":[{"id_con":"1","cell":["1"
    so does that mean that id_con is defined? thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •