Jump to content

Help with a Ajax table + mySQL


tomburton87

Recommended Posts

Hi all,

first time poster..

got a little script to display a table for products out of a database but am running into a few errors when it comes to implementing it.

 

i am getting 2 errors - one. JQuery is not defined in ajaxtable.js

 

and two,

 

object does not support property or method in line

 

$(”#product”).ajaxtable({source: “source.php”,

 

 

 

here is the code for the ajaxtable,js file where the first error is

 


(function($){

$.fn.ajaxtable = function(custom) {

 

// Default configuration

var defaults = {

  rowsPerPage: 10,

currentPage: 1,

searchField: "",

loadElement: "",

sortColumnDefault: "",

sortDefault: "asc",

sortHandlers: true,

columnWidth: "10%",

nextPrev: false

};

 

// Combine user and default configuration

var settings = $.extend({}, defaults, custom);

 

// Variable declarations

var table = $(this);

var currentPage = 0;

var searchText;

var sortColumn;

var sortAsc = true;

 

if(settings.sortDefault == "asc"){

sortAsc = true;

}else{

sortAsc  = false;

}

 

// Trigger init

init();

 

// Define searchfield if needed

if(settings.searchField != ""){

$(settings.searchField).show();

$(settings.searchField).keyup(function(){

searchText = this.value;

page(1);

});

}

 

/**

* Load page

*

* @param int number

*/

function page(number){

 

if(number == "<"){

current = table.find(".tablenavigation li.active a").html();

number = parseInt(current) - 1;

}else if(number == ">"){

current = table.find(".tablenavigation li.active a").html();

number = parseInt(current) + 1;

}else{

number = parseInt(number);

}

 

// Show loader

showLoadElement(true);

 

// Default search

if((!table.hasClass("prepared") == true) && (settings.sortColumnDefault != "")){

sortColumn = settings.sortColumnDefault;

}

 

$.post( settings.source, {action: "page", page: number, rowspp: settings.rowsPerPage, searchtext: searchText, sortcolumn: sortColumn, sortasc: sortAsc, nextPrev: settings.nextPrev}, function(response){

 

if(response.code == 200){

 

// Number of columns

var number_columns = response.extra.columns.length;

 

// Column array

var array_columns = new Array(number_columns);

array_columns = response.extra.columns;

 

// Set current page

currentPage = response.extra.currentpage;

 

// Check if the table is prepared

if(!table.hasClass("prepared") == true){

prepare(array_columns, response.extra);

}

 

// Fill class vars

fillClassVars(response.extra, response.navigation);

 

var row = "";

$.each(response.data, function(i, val){

if(response.data['class_name'] != "" && response.data['class_name'] != undefined){

row += '<tr class="' + response.data['class_name'] + '">';

}else{

row += '<tr>';

}

for(j=0;j < number_columns; j++){

classvar = '';

if(j == (number_columns-1)){

classvar = ' class="last"';

}

 

row += '<td' + classvar + '>' + response.data[array_columns[j]] + '</td>';

}

row += '</tr>';

});

table.find("tbody").html(row);

 

// Add table stripes

table.find("tbody tr:visible:even").addClass("even");

table.find("tbody tr:visible:odd").addClass("odd");

 

// Hide loader

showLoadElement(false);

}else{

showError(response.message);

showLoadElement(false);

}

}, "json" );

}

 

/**

* Initialize table

*/

function init(){

showLoadElement(false);

table.find("tbody").html("");

page(settings.currentPage);

}

 

/**

* Prepare table

* Add thead/tbody/tfoot to the table

*

* @param array columns

* @param object extra

*/

function prepare(columns, extra){

 

// Add head

var thead = '<tr>';

$.each(columns, function(i, val){

 

var width;

if(typeof(settings.columnWidth) == "string"){

width = settings.columnWidth;

}else{

width = settings.columnWidth;

}

 

classvar = '';

if(i == 0){

classvar = ' class="first"';

}

 

if(typeof(settings.sortHandlers) == "boolean"){

if(settings.sortHandlers){

thead += '<th width="' + width + '"' + classvar + '><a href="#" onclick="return false;">' + columns + '</a><span class="sorthandle"></span></th>';

}else{

thead += '<th width="' + width + '"' + classvar + '>' + columns + '<span class="sorthandle"></span></th>';

}

}

if((typeof(settings.sortHandlers) == "object")){

if(columns.length==settings.sortHandlers.length && settings.sortHandlers){

thead += '<th width="' + width + '"' + classvar + '><a href="#" onclick="return false;">' + columns + '</a><span class="sorthandle"></span></th>';

}else{

thead += '<th width="' + width + '"' + classvar + '>' + columns + '<span class="sorthandle"></span></th>';

}

}

 

});

 

thead += '</tr>';

table.find("thead").html(thead);

 

// Add sort handler

table.find("thead th a").bind('click', function(){

sortColumn = $(this).html();

var head = $(this).parent().find("span");

var setted = false;

$(this).parent().siblings().find("span").removeClass("desc");

$(this).parent().siblings().find("span").removeClass("asc");

 

// If has class desc

if(head.hasClass("desc")){

sortAsc = true

head.removeClass("desc");

head.addClass("asc");

setted  = true;

}

 

// If has class asc

if(head.hasClass("asc") && !setted){

sortAsc = false;

head.removeClass("asc");

head.addClass("desc");

setted  = true;

}

 

// Set default

if(!setted){

if(settings.sortDefault == "asc"){

head.addClass("asc");

sortAsc = true;

}else{

head.addClass("desc");

sortAsc  = false;

}

}

 

page(1);  

});

 

// Add prepared class

table.addClass("prepared");

}

 

/**

*

*

*/

function showError(message){

table.find("thead").html('<th class="error">Data error</th>');

table.find("tbody").html('<tr><td class="error">' + message + '</td><tr>');

table.find("tfoot").html('');

}

 

 

/**

* Set page x is active

*

* @param int number

*/

function setActivePage(number){

table.find('.tablenavigation li').removeClass("active");

table.find('.tablenavigation li:eq(' + number + ')').addClass("active");

}

 

/**

* Generate html for the navigation

*

* @param object extra

* @param object navigation

*/

function fillClassVars(extra, navigation){

table.find("tfoot tr td").attr("colspan", extra.columns.length);

table.find("tfoot .firstrow").html(extra.beginrow+1);

table.find("tfoot .lastrow").html(extra.endrow+1);

table.find("tfoot .totalrows").html(extra.numberrows);

table.find("tfoot .tablenavigation").html(navigation);

 

// Add navigation handlers

table.find('.tablenavigation li').bind('click', function() {

page(($(this).find("a").html()));

});

 

}

 

function showLoadElement(visible){

if(settings.loadElement != ""){

if(visible){

$(settings.loadElement).show();

}else{

$(settings.loadElement).hide();

}

}

}

 

// Return the jQuery object to allow for chainability.

return this;

 

}

 

})(jQuery);

 

 

 

 

and this is the piece of code from the html that produces the second error..

 

<script>

$(document).ready(function() {

$(“#products”).ajaxtable({source: “source.php”,

currentPage: 1,

loadElement: “.loader1”,

ascImage:“images/down.png”,

descImage: “images/up.png”});

});

</script>

 

 

 

 

any help would be fantastic.

 

 

Link to comment
Share on other sites

Have managed to fix two errors listed however table does not display any results.

 

<?php

// Includes

include("lib/Provider.php");

include("lib/aDriver.php");

include("lib/iDriver.php");

include("lib/Drivers/Mysql.php");

Make link to database

$link = mysqli_connect(db host,user,password,db name, "3306");

$Driver = new Driver_Mysql($link, "SELECT * FROM Plants",

 

$Provider = new Provider($Driver);

$Provider->HandleRequest();

 

This is the script does this all appear valid if i enter the db host im guessing 'localhost' and all other details correct?

 

Plants is a valid table within my said db..

Link to comment
Share on other sites

You seem to using allot of third party code, so its pretty hard to tell what is going on. You might get more assistance if you at least let us know what the libraries are that your using or maybe even post your questions in a forum related to those third party libraries.

Link to comment
Share on other sites

yes your right i am,

im basically first time trying to implement a stock of items on a webpage from a SQL db that is user searchable or sortable.

if you could recommend something that is perhaps better documented etc would be great?

 

Otherwise thank you very much for your replies.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.