2012-08-01 7 views
1

我試圖截斷從我的數據庫中檢索到的書籍標題打印在條形碼標籤上。我無法從查詢中截斷它,因爲我需要將整個標題顯示在同一頁面上的表格中。這是我有:截斷書名與jQuery和子字符串

$.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++; 
    }   

一切工作正常,除了標題太長。目前我收到一個錯誤,指出:TypeError:title.substr不是一個函數。我在這裏和谷歌上做了一些研究,似乎我的代碼是正確的。有任何想法嗎?

編輯:這裏有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)"] 

編輯2:我更新了腳本來顯示整個Ajax調用,並將結果返回方式。

+3

你確定'title'的類型是'string'嗎? – Matt 2012-08-01 17:44:47

+0

查詢的子串,只查詢兩次。合理? Soo .... SELECT columnA title,substr(columnA,0,16)sku FROM DUAL – 2012-08-01 17:45:52

+1

查找'title'的數據類型的快速方法是使用'alert(typeof title);' – 2012-08-01 17:46:06

回答

1

您正在使用多個標題,以剛剛得到的數組中的第一個:

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

如果你想建立條形碼爲每個標題:

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

    // Build barcode 

} 
+0

我以爲我不能在對象上使用substr。 – Jim 2012-08-01 18:03:27

+2

你不能。你會在字符串上使用它。 'data.title [0]'等於「瞭解和應用醫學人類學」。增加數組索引,進入數組中的下一個標題。 – 2012-08-01 18:04:17

+0

經過一段代碼的其餘部分,這完美的作品。謝謝 – Jim 2012-08-01 18:31:15

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