0

Good morning,

Today I'm trying to make a live search PHP+MySQL+Json program in my website and sometimes it's difficult because I don't know a lot of Json code.

URL: http://bdebeauty.es/index.php?option=com_jumi&view=application&fileid=14&Itemid=258

I'm displaying the table with the information with the following code:

<script>

  function makeTable(data){
   var tbl_body = "";
      $.each(data, function() {
        var tbl_row = "";
        $.each(this, function(k,v)
        {
            tbl_row += "<td>"+v+"</td>";
        })
        tbl_body += "<tr>"+tbl_row+"</tr>";         
      })


    return tbl_body;
  }

  function getEmployeeFilterOptions(){
    var opts = [];
    $checkboxes.each(function(){
      if(this.checked){
        opts.push(this.name);
      }
    });

    return opts;
  }

  function updateEmployees(opts){
    $.ajax({
      type: "POST",
      url: "submit.php",
      dataType : 'json',
      cache: false,
      data: {filterOpts: opts},
      success: function(records){
        $('#employees tbody').html(makeTable(records));
      // here, after the content is inside DOM/visible we activate the plugin
        $('#employees').dataTable({
            "paging":   true,
            "ordering": false,
            "info":     false
        });
      }
    });
  }

  var $checkboxes = $("input:checkbox");
  $checkboxes.on("change", function(){
    var opts = getEmployeeFilterOptions();
    updateEmployees(opts);
  });

  updateEmployees();
</script>

The problem is that I need to limit the results and having a "pagination", because at the moment it's showing a lot of entries and the scroll is heavy.

How can I do that?

Thanks, much appreciated.

Regards.

1 Answers1

0

This is not a problem about json, you just need a tool for display your table. I used this awsome plugin for jquery. It supports pagination, ordering, filtering, num. of entries per page. It's really simple to use and configure:

  1. add these to your page:

  2. create your table as you are doing right now with full result set, remember thead and tfoot

activate the plugin on this table with:

$(document).ready(function() {
    $('#example').dataTable( {
        "paging":   false,
        "ordering": false,
        "info":     false
    } );
} );

UPDATE: After you call makeTable() you have to display this table by putting it inside an element like , only now you can activate the plugin.

function updateEmployees(opts){
    $.ajax({
      type: "POST",
      url: "submit.php",
      dataType : 'json',
      cache: false,
      data: {filterOpts: opts},
      success: function(records){
        $('#employees tbody').html(makeTable(records));
      // here, after the content is inside DOM/visible we activate the plugin
        $('#employees').dataTable({
            "paging":   true,
            "ordering": true,
            "info":     true
        });
      }
    });
  }

!Important Remember to write the full table with thead and tbody tags like here

Sasha
  • 431
  • 3
  • 15
  • Thanks Sasha, but I'm lost at the moment. I have updated my first message with my JQuery code, because I don't know where to place that code you gave me...! Regards. –  Jul 01 '14 at 10:47
  • That's what I got at the moment: http://bdebeauty.es/index.php?option=com_jumi&view=application&fileid=14&Itemid=258 –  Jul 01 '14 at 11:02
  • 1
    oh, you made it. You dont see the pagination because of the configuration, set true to paging and play with other 2 fields (ordering and info). $('#myTable').dataTable( { "paging": true, "ordering": false, "info": false }); – Sasha Jul 01 '14 at 11:06
  • Thanks Sasha, I'm keep trying but at the moment I'm a little bit lost because it never displays the pagination, and it always shows the full results. Also, when I change the dropdown list (10 items per page) it deletes the items. Any clue on that? Much appreciated for your help, I have updated the first message with my script. –  Jul 01 '14 at 11:22
  • 1
    this happens because you activate the plugin immidiatly after the page is loaded, now see the update, notice that inside updateEmployees(opts) Im activating it after the content is displayed – Sasha Jul 01 '14 at 13:25
  • Much appreciated Sasha, now the table is showing fine, with the correct pagination, but now I have one more problem: When I check a checkbox it displays the results but I got a pop-up saying that there is an error (etc, etc). How can I avoid that to show? And also, how can I delete the dropdown menu of the "Show X items"? I just want to display like 20 by default per page. Thanks again Sasha. –  Jul 02 '14 at 06:33
  • Hi Wiraj, I see you dont have these problems anymore :) – Sasha Jul 02 '14 at 09:29
  • Thanks Sasha, now I'm trying to change some checkboxes into a dropdown list (select option). But when I try to pass the select option throught JQuery I can't because I'm not an expert with JQuery. With the checkbox I have the following code working: var $checkboxes = $("input:checkbox"); $checkboxes.on("change", function(){ But with the select option I'm not able to make this work...! Any ideas Sasha? Much apreciated for your help. –  Jul 02 '14 at 09:33
  • i suggest you to google these simple questions, you'll find an answer really quick, heres an answer with demos: http://stackoverflow.com/questions/11179406/jquery-get-value-of-select-onchange – Sasha Jul 02 '14 at 09:44
  • Thanks again Sasha, I google it a lot but it's difficult to find usefull information. Regards. –  Jul 02 '14 at 09:45
  • 1
    i just searched "jquery select change" and found that link on second result :) – Sasha Jul 02 '14 at 09:49