1

I am attempting to truncate a book title that is retrieved from my db to print on a bar code label. I can't truncate it from the query as I need to entire title to appear in a table on the same page. Here is what I have:

   $.ajax({
        type: "POST",
        url: "advanceProcess.php",
        dataType: "json",
        data: ({sourceID: $('#sourceID').val(), fromDate: $('#fromDate').val(), toDate: $('#toDate').val()}),
        success: function(data){
            if(data.isbn == null ){
                $("#acctRecords").append('<tr><td>No Records Found</td></tr>');
            }else{
                //append general data
                for(var x=0;x<data.isbn.length;x++)
                {
                    $("#acctRecords").append('<tr><td id="tableSKU">'+data.tempsku[x]+
                    '</td><td id="tableISBN">'+data.isbn[x]+
                    '</td><td id="tableTitle">'+data.title[x]+
                    '</td><td id="tableOrderid">'+data.orderId[x]+
                    '</td><td id="tableQtyBought">'+data.quantity[x]+
                    '</td><td id="tableCost">'+data.cost[x]+
                    '</td><td id="tabledateCreated">'+data.dateCreated[x]+
                    '</td><td id="tableWeight">'+data.weight[x]+
                    '</td><td id="tableCheckNumber">'+data.checkNumber[x]+'</td></tr>');
                }// end of for loop
                //refreshes the tablesorter plugin
                $("#acctRecords").trigger("update");
    //Print the bar codes
    sku = data.tempsku;
    title = data.title[x];
    //console.log(JSON.stringify(data));
    title = title.substr(0,16);
    var x=0;
    for (x=0;x<title.length;x++)
    {   
        first += '$("#'+indexGlobal+'").barcode("'+sku[x]+'", "codabar",{barHeight:40, fontSize:30, output:"bmp"});';
        second += '<div class="wrapper"><img src="../images/temp.jpg" /><div id="'+indexGlobal+
        '"></div><div class="fullSKU">&nbsp &nbsp &nbsp '+sku[x]+'</div><br/><div class="title">'+title[x]+
        '</div></div><br/><div class="page-break"></div>';
        indexGlobal++;
    }           

Everything workd fine, except the title is too long. Currently I am getting an error that states: TypeError: title.substr is not a function. I have done some research on here and on Google and it seems that my code is correct. Any ideas?

EDIT: here are the results of the JSON:

"title":["Understanding and Applying Medical Anthropology","The Ethical Chemist : Professionalism and Ethics in Science","Magic, Witchcraft, and Religion: A Reader in the Anthropology of Religion","Principles of Cancer Biology","AIDS and Accusation: Haiti and the Geography of Blame","In Search of Respect: Selling Crack in El Barrio (Structural Analysis in the Social Sciences)"] 

EDIT 2: I updated the script to show the entire ajax call and how the results are returned.

Jim
  • 1,315
  • 4
  • 17
  • 45
  • 3
    Are you sure that `title` is of type `string`? – Matt Aug 01 '12 at 17:44
  • Substring on the query, just query the row twice. Make sense? Soo.... SELECT columnA title, substr(columnA,0,16) sku FROM DUAL – Roberto Navarro Aug 01 '12 at 17:45
  • 1
    A quick way to find out the data type of `title` is to use `alert(typeof title);` – Jeremy Harris Aug 01 '12 at 17:46
  • I did what @cillosis suggested and got object. does that mean I need to do title.object.substring(0,16)? – Jim Aug 01 '12 at 17:49
  • 1
    No. You can't perform `substr()` on an object. Only on strings. What's the structure of `title`? – Matt Aug 01 '12 at 17:52
  • `console.log(title)` to see what it contains. – gen_Eric Aug 01 '12 at 17:52
  • To figure out the structure, you can convert it JSON and show us. You should be able to do something like `console.log(JSON.stringify(title));` or if using an older browser, implement the stringify function to be able to use it. And then show us the JSON. http://stackoverflow.com/questions/3593046/jquery-json-to-string – Jeremy Harris Aug 01 '12 at 17:53
  • Seems `title` is an array of titles, not just one. WHere is this coming from, and which one do you want? – gen_Eric Aug 01 '12 at 17:58

2 Answers2

1

You are using multiple titles, in order to just get the first one from the array:

title = data.title[0];
title = title.substr(0,16);

If you want to build barcodes for each title:

for(x=0; x<data.title.length; x++)
{
   title = data.title[x];
   title = title.substr(0,16);

   // Build barcode

}
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
0
//Your object [JSON]
var obj = {"title":["Understanding and Applying Medical Anthropology","The Ethical     Chemist : Professionalism and Ethics in Science","Magic, Witchcraft, and Religion: A Reader in the Anthropology of Religion","Principles of Cancer Biology","AIDS and Accusation: Haiti and the Geography of Blame","In Search of Respect: Selling Crack in El Barrio (Structural Analysis in the Social Sciences)"]}

//Result
obj.title[0] // Understanding and Applying Medical Anthropology
Praveen Vijayan
  • 6,521
  • 2
  • 29
  • 28