2013-04-18 152 views
4

有沒有可能取消選擇一行而沒有按住控制鍵,只能通過點擊它?我的意思是,如果你點擊一個已經選擇的行,它應該取消選擇,而不必按住控制鍵。Rowunselect沒有按Ctrl +點擊

回答

2

我與Primefaces 3.4.2測試: XHTML頁面:

<script type="text/javascript"> 
       function test(xhr, status, args){ 
        if(args.unselecttest % 2 == 1){ 
         stest.unselectAllRows(); 
        } 
       } 
      </script> 
<p:dataTable widgetVar="stest" selectionMode="single" selection="#{tabview.car}" 
<p:ajax event="rowSelect" oncomplete="test(xhr, status, args);" /> 

豆:

private int count = 0; 

    public Car getCar() { 
     return car; 
    } 

    public void setCar(Car car) { 
     if (car.equals(this.car)) { 
      count++; 
      RequestContext reqCtx = RequestContext.getCurrentInstance(); 
      reqCtx.addCallbackParam("unselecttest", count); 
     } else { 
      count = 0; 
     } 
     this.car = car; 
    } 
+0

如果我有多選數據表,我該怎麼辦? – leostiw 2013-04-18 08:07:15

+1

不,多重是不可能的,你必須使用控制鍵,你想象如何得到多個,如果不使用控制鍵:) – 2013-04-18 08:12:47

+3

@leostiw你可以真正避免使用RichFaces數據表組件啓用「多鍵盤免費」選項的數據表組件。 [RichFaces DataTable - 多鍵盤免費](http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=extendedDataTable&sample=exTableSelection&skin=blueSky)問候, – 2013-04-18 08:36:53

-1

使用jquery切換功能。

$(selector).toggle(); 
+0

你能更具體的pleasa嗎?我正在使用primefaces datatable順便說一句。 – leostiw 2013-04-18 06:59:38

0

我得到了sollution。

我剛剛覆蓋了primefaces.js,實際上,我只是複製了Primefaces.Datatable的一部分,只是刪除了使用CtrlKey取消選擇該行所需的條件。

下面的例子:

原始的JavaScript的報價:

onRowClick: function (e, d, a) { 
    if ($(e.target) .is('td,span:not(.ui-c)')) { 
     var g = $(d), 
     c = g.hasClass('ui-state-highlight'), 
     f = e.metaKey || e.ctrlKey, 
     b = e.shiftKey; 
     if (c && f) { 
     this.unselectRow(g, a) 
     } else { 
     if (this.isSingleSelection() || (this.isMultipleSelection() && e && !f && !b && this.cfg.rowSelectMode === 'new')) { 
      this.unselectAllRows() 
     } 
     if (this.isMultipleSelection() && e && e.shiftKey) { 
      this.selectRowsInRange(g) 
     } else { 
      this.originRowIndex = g.index(); 
      this.cursorIndex = null; 
      this.selectRow(g, a) 
     } 
     } 
     PrimeFaces.clearSelection() 
    } 
    }, 

你只要這部分改成這樣:

onRowClick: function (e, d, a) { 
    if ($(e.target) .is('td,span:not(.ui-c)')) { 
     var g = $(d), 
     c = g.hasClass('ui-state-highlight'), 

     // I changed it to true 
     f = true; 

     b = e.shiftKey; 
     if (c && f) { 
     this.unselectRow(g, a) 
     } else { 
     if (this.isSingleSelection() || (this.isMultipleSelection() && e && !f && !b && this.cfg.rowSelectMode === 'new')) { 
      this.unselectAllRows() 
     } 
     if (this.isMultipleSelection() && e && e.shiftKey) { 
      this.selectRowsInRange(g) 
     } else { 
      this.originRowIndex = g.index(); 
      this.cursorIndex = null; 
      this.selectRow(g, a) 
     } 
     } 
     PrimeFaces.clearSelection() 
    } 
    }, 

,如果你需要幫助,你可以給我一個信息。