views:

222

answers:

3

Here is function ,

 <script type="text/javascript">
    $(document).ready(function() {
        getRecordspage(1, 5);
        $("a.page-numbers").click(function() {
            alert(1);
            getRecordspage($(this).text(), 5);
            return false;
        });
    });

And in my page i am appending anchors dynamically to this div,

<div id="pager" class="pager">
//my anchors will be present here...
</div>

i am appending anchors dynamically... All anchors will have class="page-numbers"... How it can be done...

When inspected through firebug my pager div had this when i clicked 3,

<div class="pager" id="pager">
<a class="page-numbers prev" href="#">Prev</a>
<a class="page-numbers" href="#">1</a>
<a class="page-numbers" href="#">2</a>
<span class="page-numbers current">3</span>
<a class="page-numbers" href="#">4</a>
<a class="page-numbers next" href="#">Next</a></div>

EDIT:

I using jquery 1.4...

function getRecordspage(curPage, pagSize) {
    $.ajax({
        type: "POST",
        url: "Default.aspx/GetRecords",
        data: "{'currentPage':" + curPage + ",'pagesize':" + pagSize + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(jsonObj) {
            var strarr = jsonObj.d.split('##');
            var jsob = jQuery.parseJSON(strarr[0]);
            var divs = '';
            $.each(jsob.Table, function(i, employee) {
                divs += '<div class="resultsdiv"><br /><span class="resultName">' + employee.Emp_Name + '</span><span class="resultfields" style="padding-left:100px;">Category&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" class="resultfields">Salary Basis&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.SalaryBasis + '</span><span class="resultfields" style="padding-left:25px;">Salary&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.FixedSalary + '</span><span style="font-size:110%;font-weight:bolder;padding-left:25px;">Address&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.Address + '</span></div>';
            });
            $("#ResultsDiv").append(divs);
            $(".pager").pagination(strarr[1], { 
                current_page: curPage - 1, items_per_page: '5', num_display_entries
: '5', next_text: 'Next', prev_text: 'Prev', num_edge_entries: '1'
            });
            $(".resultsdiv:even").addClass("resultseven");
            $(".resultsdiv").hover(function() {
                $(this).addClass("resultshover");
            }, function() {
                $(this).removeClass("resultshover");
            });
        }
    });
+2  A: 

Use .live():

$("a.page-numbers").live('click',function() {
        alert(1);
        getRecordspage($(this).text(), 5);
        return false;
});

or execute your handler assignment after you created the links. If there are no elements with class page-numbers when this function is executed then of course nothing happens.

But live() takes care of that:

Description: Attach a handler to the event for all elements which match the current selector, now or in the future.

Update:

Well I am not sure. One thing you can try is to move your function inside the success function of the Ajax call:

function getRecordspage(curPage, pagSize) {
    $.ajax({
        type: "POST",
        url: "Default.aspx/GetRecords",
        data: "{'currentPage':" + curPage + ",'pagesize':" + pagSize + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(jsonObj) {
            // .. lot of stuff here..
            // at  the end:
            $("a.page-numbers").click(function() {
                alert(1);
                getRecordspage($(this).text(), 5);
                return false;
            }
       }
   });
}
Felix Kling
@felix that didnt work...
Pandiya Chendur
@Pandiya Chendur: Then you have to provide more code. When/how do you create those links?
Felix Kling
@felix look at my question now you ll get a fair idea what i am doing...
Pandiya Chendur
@Pandiya Chendur: See my updated answer.
Felix Kling
@Felix that too didnt work because i think pagination has a `callback` but when i used that too didnt work...
Pandiya Chendur
@Felix i made it too work...
Pandiya Chendur
@Pandiya Chendur: Good to hear that :)
Felix Kling
+1  A: 

you must use .live when you'll dynamically add elements.

<script>
   $(document).ready(function() {
      getRecordspage(1, 5);
        $("a.page-numbers").live('click', function() {
            alert(1);
            getRecordspage($(this).text(), 5);
            return false;
      });
   });
</script>
j.
+1  A: 

you should use jquery 1.42 and .delegate() instead of .live()

$("a.page-numbers").delegate('click', 'a.page-numbers', function(){
    alert('1');
});

Kind Regards, --Andy

jAndy
Delegate seems to be the most overlooked feature in 1.4. Everyones just `live` `live` `live` `live` `live`!
Matt
@Andy delegate doesn't seem to get executed...
Pandiya Chendur
isn't it `$("a.page-numbers").delegate('a.page-numbers', 'click', function(){ ...` ?
j.
@Matt: Seems to be new in 1.4.2 not in 1.4
Felix Kling
@j that too didnt work...
Pandiya Chendur