2012-03-14 25 views
0

所以我想設置細胞上的蒼蠅,但我有一些自定義格式爲我的網格,例如:與jqGrid的使用自定義格式setCell

formatter: function(cellvalue, options, rowObject) { 
        if (rowObject.myId){ 
         return '<span class="editable" data-id="' + rowObject.myId + '">$' + cellvalue + '</span>'; 
        } 
        else{ 
         return cellvalue; 
        } 
       } 

,並在電網完成後的一些點我想要做的事:

$('#table').jqGrid('setCell', 1,'colName', '500', 'test'); 

然而,問題是,這寫在格式化程序,只是用該單元格值替換整個事情。有沒有辦法通過自定義格式化程序或其他方式解決此問題?

樣本數據:

{"metaData":null,"records":[{"spendD1":2520.88,"spendD2":0,"cpaD1":11.95,"cpaD2":0,"conversionsD1":211,"conversionsD2":0,"conversionRateD1":11.00,"conversionRateD2":0.00,"clicksD1":1872,"clicksD2":0,"cpcD1":1.35,"cpcD2":0,"statusId":1,"statusName":"Active","creativesCount":0,"adGroupsCount":0,"keywordsCount":0,"queryTermsCountD1":0,"queryTermsCountD2":0,"trafficSourcesCount":0,"campaignId":6824,"adgroupId":151464,"adgroupName":".com General Credit Offers","bid":2.000}]} 

編輯網格PARAMS:

所有的
altRows 
    false 

altclass 
    "ui-priority-secondary" 

autoencode 
    false 

autowidth 
    false 

beforeProcessing 
    null 

beforeSelectRow 
    null 

caption 
    "" 

cellEdit 
    false 

cellLayout 
    5 

cellsubmit 
    "remote" 

cmTemplate 
    Object {} 

colModel 
    [Object { name="adgroupName", index="adgroupName", width=240, more...}, Object { name="campaignStatus", index="campaignStatus", width=60, more...}, Object { name="spendD1", index="spendD1", width=80, more...}, 8 more...] 

colNames 
    ["Ad Group", "Status", "Spend", 8 more...] 

data 
    [] 

datatype 
    "json" 

deselectAfterSort 
    true 

direction 
    "ltr" 

disableClick 
    false 

editurl 
    null 

emptyrecords 
    "No records to view" 

footerrow 
    true 

forceFit 
    false 

gridstate 
    "visible" 

gridview 
    false 

grouping 
    false 

groupingView 
    Object { groupField=[0], groupOrder=[0], groupText=[0], more...} 

headertitles 
    false 

height 
    "100%" 

hiddengrid 
    false 

hidegrid 
    true 

hoverrows 
    true 

id 
    "advertiser_table" 

idPrefix 
    "" 

ignoreCase 
    false 

jsonReader 
    Object { root="records", page="pageNumber", total="totalPages", more...} 

keyIndex 
    false 

lastpage 
    1 

lastsort 
    2 

loadBeforeSend 
    null 

loadError 
    null 

loadonce 
    false 

loadtext 
    "Loading..." 

loadui 
    "enable" 

localReader 
    Object { root="rows", page="page", total="total", more...} 

mtype 
    "GET" 

multiboxonly 
    false 

multikey 
    false 

multiselect 
    false 

multiselectWidth 
    20 

nv 
    0 

onHeaderClick 
    null 

onPaging 
    null 

onRightClickRow 
    null 

onSelectAll 
    null 

onSelectRow 
    null 

ondblClickRow 
    null 

page 
    1 

pager 
    "" 

pagerpos 
    "center" 

pgbuttons 
    true 

pginput 
    true 

pgtext 
    "Page {0} of {1}" 

postData 
    Object { _search=false, nd=1331821970112, rows=20, more...} 

prmNames 
    Object { page="page", rows="rows", sort="sidx", more...} 

reccount 
    2 

recordpos 
    "right" 

records 
    2 

recordtext 
    "View {0} - {1} of {2}" 

remapColumns 
    [] 

resizeclass 
    "" 

rowList 
    [] 

rowNum 
    20 

rowTotal 
    null 

rownumWidth 
    25 

rownumbers 
    false 

savedRow 
    [] 

scroll 
    false 

scrollOffset 
    18 

scrollTimeout 
    40 

scrollrows 
    false 

search 
    false 

selarrrow 
    [] 

selrow 
    null 

shrinkToFit 
    true 

sortable 
    Object { update=function()} 

sortname 
    "spendD1" 

sortorder 
    "desc" 

subGrid 
    false 

subGridModel 
    [] 

subGridWidth 
    20 

tblwidth 
    1233 

toolbar 
    [false, ""] 

toppager 
    false 

totaltime 
    9 

treeANode 
    -1 

treeGrid 
    false 

treeGridModel 
    "nested" 

treeReader 
    Object {} 

tree_root_level 
    0 

url 
    "/advertiser_services/amp/report/adgroup/get" 

useProp 
    true 

userData 
    Object { spendD1=2520.88, spendD2=0, cpaD1=11.95, more...} 

userDataOnFooter 
    true 

viewrecords 
    false 

viewsortcols 
    [false, "vertical", true] 

width 
    1233 

回答

1

首先我不知道爲什麼你需要這樣的格式。 data-id的屬性只有在DOM元素爲單元元素或者jQuery對象爲myCell時才能被訪問,但在這種情況下,$(myCell).closest("tr.jqgrow").attr("id")會得到rowid。

不過,您所描述的問題很常見。

我建議你重寫格式的代碼

formatter: function(cellvalue, options) { 
    var id = options.rowId; 
    return id ? 
      '<span class="editable" data-id="' + id + '">$' + cellvalue + '</span>' : 
      cellvalue; 
} 

,你必須是在自定義格式的設計問題的問題。特別是如果一個裝載來自服務器的數據與該行的數據的標準格式類似

{"id" :"1", "cell": ["cell11", "cell12", "cell13"]}, 

loadonce: true使用另外很清楚。的情況下的rowObject在第一柵格負荷將Array

["cell11", "cell12", "cell13"] 

在下一網格刷新數據將已經在格式等

{_id_: "1", colName1: "cell11", colName2: "cell12", colName3: "cell13"} 

setCell格式化使用的情況下將被調用rowObject中的第三格式。您如何從the source codesetCell中看到rowObject將被初始化爲$('#table')[0].rows.namedItem(rowid)rowObject將是DOM元素,它對應於當前行中的<tr>。因此,要獲得電池包含你要在索引中colModel使用類似

其中iCol在您需要的列。您可以使用getColumnIndexByName獲取索引(例如,請參閱the answer)。

+0

im不知道這回答了這個問題。我知道如何獲得單元格值,但問題是我正在更改單元格的html結構,所以我失去了原始格式。 (我添加在一個輸入框,用戶可以輸入一個新的值爲單元格等)等即時通訊,即時通訊,我沒有放在rowObject id,而是一個不同的ID,來自我需要的服務器用於保存新的單元格值。 – Evan 2012-03-15 14:08:01

+0

@Evan:在這種情況下,你必須按照最後一個建議與'children'。您應該發佈更多網格的完整定義。我如何解釋你是否知道使用'datatype'是很重要的:local或remote('xml','json'),不管你使用'loadonce:true'等等。所以,只需將您的問題附加到jqGrid的完整定義以及用於填充網格的數據示例中即可。 – Oleg 2012-03-15 14:22:25

+0

添加了網格參數和一些示例數據 – Evan 2012-03-15 14:39:58