tags:

views:

36

answers:

1

I have a form that displays a number of results based on the value of a select list "#maxrows" (25, 50, 125, 250 results). "#startID" is a hidden input thats value is set to 1 that starts the displayed data from the first one. So each time you click "#next/#prev" it adds or subtracts from its value. Through PHP i display two buttons #previous and #next when the info is returned on the page via AJAX. Is there a better way to step through the results than this?

$(document).ready(function() { 
$("#processing").hide();

var options = { 
    target: '#return',
    beforeSend: function() {
        $('#processing').show()
    },
    complete: function() {
        $('#processing').hide()
    }
    }; 
   $('#SymbolSearchForm').ajaxForm(options); 
}); 

function changestart(direction) 
{
var rowsElement  = $("#maxrows");
var rowsValue    = parseInt(rowsElement.val());
var startElement = $("#startID");
var value        = parseInt(startElement.val());
startElement.val(direction == "forward" ? value + rowsValue : direction == "back" ?
value - rowsValue : 1);
}

"#processing" is just the loading gif, im more concerned with the "function changestart(direction)" part

A: 

I don't think there is anything wrong with it if it works; don't overcomplicate things that work :). Just be sure you comment your code so you (and anyone else who reads your code) will know what is happening if they look at it.

Edit

The reason it isn't working is because you are using the ternary operator incorrectly.

startElement.val(direction == "forward" ? value + rowsValue : direction == "back" ? value - rowsValue : 1);

is invalid. It should be:

startElement.val(direction == "forward" ? value + rowsValue : value - rowsValue);
SimpleCoder
@SimpleCoder - i guess your right, if it ain't broke don't fix it. I just would like an alternative. Right now in the PHP script it's using <... onclick=\"changestart('back')\"/ and i dont like onclick and wanted another way
Dirty Bird Design
$("#next").click(function() { changestart('forward'); }); isn't working any ideas?
Dirty Bird Design
What's not working with it? Is it not doing anything?
SimpleCoder
it's not doing anything. I would like to include that part as a click function in JS if possibleHere it is in PHP { echo "<input type=\"submit\" id=\"previous\" name=\"previous\" value=\"Previous\" onclick=\"changestart('back')\"/>";}
Dirty Bird Design
See my edited post
SimpleCoder
Sorry, I meant what I was trying as a click function wasn't working. the function itself was, I just want to remove it from the PHP that applies it as a onclick, see my post above your last. Is there a way to remove it from the PHP and add it via JS?
Dirty Bird Design
Yes, use: `$("#previous").click(function(){changestart('back');});` in your `ready` handler, or wherever you want to bind the event.
SimpleCoder
@SimpleCoder - thx, fixing the ternary allowed my click functions to work. Awesome!
Dirty Bird Design
@SimpleCoder - for whatever reason, now that this is in a production environment I've had to go back to inline onclick funcitons in the php file. The JS click functions no longer work? Any ideas?
Dirty Bird Design
Make sure you are binding the events in the correct place: in the ready event handler, or when you load content via Ajax.
SimpleCoder
https://www.kinetick.com/Test/supportTest.php#Symbol-Search it fires the beforesend and complete ajax functions, but doesn't change the results. I don't understand why it worked before, but not now?
Dirty Bird Design
I can't spot problem directly in the source. Could you post the code that handles this?
SimpleCoder
http://jsfiddle.net/sMfuX/
Dirty Bird Design
Does it have to do with the table being built in PHP and returned to the page? Should I/can I put the click functions in the PHP file?
Dirty Bird Design