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

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);
<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

still the same error.

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

thanks

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)

Put var in front of it (in both instances):


[B][COLOR="Blue"]var[/COLOR][/B] id_con = id_con.substring(id_con.lastIndexOf('row')+3);

hi

<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.

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

thanks


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 }

:slight_smile:

[size=1]PS. You can use [noparse]


[/noparse] instead of [noparse]


[/noparse] to get JavaScript highlighted (and indented) code in your post :)[/size]

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>

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)

thanks

Which tutorial?

thought i would try another method which i found. but still the same error.

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

"page":"1","total":"26","rows":[{"id_con":"1","cell":["1"

so does that mean that id_con is defined? thanks