2010-12-30 10 views
17

मैं एक सारणी रखने की कोशिश कर रहा हूं जो डेटा को प्रदर्शित करता है जो उपयोगकर्ता इनपुट करता है और डेटा को संपादित करता है। मैंने यह पता लगाया है कि टेक्स्ट के साथ ऐसा कैसे करें (यानी, वे तालिका में किसी चीज़ का नाम संपादित कर सकते हैं), लेकिन मैं इसे चयन कक्षों के साथ काम नहीं कर सकता।जीडब्ल्यूटी का उपयोग कर चयन चयन में गतिशील रूप से अद्यतन कैसे करें?

यह चयन करता है कि चयन कक्ष में आइटम पूर्वनिर्धारित हैं, लेकिन मैं सेल बनाने के बाद नई चीजों को शामिल करने के लिए सेल में आइटम को गतिशील रूप से अपडेट नहीं कर सकता।

अधिक व्याख्या करने के लिए, मेरे पास "प्रकार" कॉलम है। उपयोगकर्ता किसी दिए गए प्रकार के साथ तालिका में आइटम दर्ज करता है, लेकिन वह बाद में नए प्रकार भी जोड़ सकता है। जब वे टाइप कॉलम में आइटम पर क्लिक करते हैं, तो मैं ड्रॉपडाउन बॉक्स को उन सभी नए प्रकारों को शामिल करना चाहता हूं जो उन्होंने दर्ज किए हैं, लेकिन मुझे नहीं पता कि इसे कैसे पूरा किया जाए।

यहां मेरे पास अभी तक का कोड है (जो अपडेट नहीं है जैसा मैं चाहता हूं)। record.getTypeList() में नए प्रकार के प्रवेश करने के बाद अतिरिक्त प्रविष्टियां होंगी।

SelectionCell editTypeComboBox = new SelectionCell(record.getTypeList()); 

    Column<Assignment, String> typeColumn = new Column<Assignment, String>(editTypeComboBox) { 
     @Override 
     public String getValue(Assignment object) { 
      return object.getType(); 
     } 
    }; 
    typeColumn.setFieldUpdater(new FieldUpdater<Assignment, String>() { 

     @Override 
     public void update(int index, Assignment object, String value) { 
      int row = index; 
      String newType = value; 
      record.editAssignType(row, newType); 
      updateClassGradeLabel(); 
      log.info("Set type to " 
        + value); 
      cellTable.redraw(); 
     } 
    }); 

    cellTable.addColumn(typeColumn, "Type"); 

संपादित करें: मुझे यह पता लगा की मदद करने पीटर Knego दुश्मन को धन्यवाद। यहाँ संशोधित DynamicSelectionCell वर्ग है अगर किसी को दिलचस्पी है, तो:

/* 
* Copyright 2010 Google Inc. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License. You may obtain a copy of 
* the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
* License for the specific language governing permissions and limitations under 
* the License. 
*/ 
package com.google.gwt.cell.client; 

import com.google.gwt.core.client.GWT; 
import com.google.gwt.dom.client.Element; 
import com.google.gwt.dom.client.NativeEvent; 
import com.google.gwt.dom.client.SelectElement; 
import com.google.gwt.safehtml.client.SafeHtmlTemplates; 
import com.google.gwt.safehtml.shared.SafeHtml; 
import com.google.gwt.safehtml.shared.SafeHtmlBuilder; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

/** 
* A {@link Cell} used to render a drop-down list. 
*/ 
public class DynamicSelectionCell extends AbstractInputCell<String, String> { 

    interface Template extends SafeHtmlTemplates { 
    @Template("<option value=\"{0}\">{0}</option>") 
    SafeHtml deselected(String option); 

    @Template("<option value=\"{0}\" selected=\"selected\">{0}</option>") 
    SafeHtml selected(String option); 
    } 

    private static Template template; 

    private HashMap<String, Integer> indexForOption = new HashMap<String, Integer>(); 

    private final List<String> options; 

    /** 
    * Construct a new {@link SelectionCell} with the specified options. 
    * 
    * @param options the options in the cell 
    */ 
    public DynamicSelectionCell(List<String> options) { 
    super("change"); 
    if (template == null) { 
     template = GWT.create(Template.class); 
    } 
    this.options = new ArrayList<String>(options); 
    int index = 0; 
    for (String option : options) { 
     indexForOption.put(option, index++); 
    } 
    } 

    public void addOption(String newOp){ 
     String option = new String(newOp); 
     options.add(option); 
     refreshIndexes(); 
    } 

    public void removeOption(String op){ 
     String option = new String(op); 
     options.remove(indexForOption.get(option)); 
     refreshIndexes(); 
    } 

    private void refreshIndexes(){ 
     int index = 0; 
     for (String option : options) { 
      indexForOption.put(option, index++); 
     } 
    } 

    @Override 
    public void onBrowserEvent(Context context, Element parent, String value, 
     NativeEvent event, ValueUpdater<String> valueUpdater) { 
    super.onBrowserEvent(context, parent, value, event, valueUpdater); 
    String type = event.getType(); 
    if ("change".equals(type)) { 
     Object key = context.getKey(); 
     SelectElement select = parent.getFirstChild().cast(); 
     String newValue = options.get(select.getSelectedIndex()); 
     setViewData(key, newValue); 
     finishEditing(parent, newValue, key, valueUpdater); 
     if (valueUpdater != null) { 
     valueUpdater.update(newValue); 
     } 
    } 
    } 

    @Override 
    public void render(Context context, String value, SafeHtmlBuilder sb) { 
    // Get the view data. 
    Object key = context.getKey(); 
    String viewData = getViewData(key); 
    if (viewData != null && viewData.equals(value)) { 
     clearViewData(key); 
     viewData = null; 
    } 

    int selectedIndex = getSelectedIndex(viewData == null ? value : viewData); 
    sb.appendHtmlConstant("<select tabindex=\"-1\">"); 
    int index = 0; 
    for (String option : options) { 
     if (index++ == selectedIndex) { 
     sb.append(template.selected(option)); 
     } else { 
     sb.append(template.deselected(option)); 
     } 
    } 
    sb.appendHtmlConstant("</select>"); 
    } 

    private int getSelectedIndex(String value) { 
    Integer index = indexForOption.get(value); 
    if (index == null) { 
     return -1; 
    } 
    return index.intValue(); 
    } 
} 

उत्तर

11

दुर्भाग्य SelectionCell भंडार एक निजी सूची में विकल्प और वहाँ इस हेरफेर करने के लिए जाने के बाद उन्हें निर्माता में स्थापित किया है कोई तरीके हैं।

सौभाग्य से, SelectionCell एक काफी सरल वर्ग है। बस अपनी खुद की (नामित) प्रतिलिपि बनाएं और addOption(..)/removeOption(..)List<String> options में हेरफेर करने के तरीकों को जोड़ें।

+0

मुझे यह विचार पसंद है, लेकिन मुझे नए वर्ग के काम को करने के लिए सही कक्षाओं को आयात करने में परेशानी हो रही है। मैंने सेलेक्शनसेल क्लास की प्रतिलिपि बनाई और इसका नाम बदलकर डायनामिक सिलेक्शनसेल रखा, लेकिन प्रस्तुत करने और ऑन ब्राउजर इवेंट विधियों ने शिकायत की कि उन्हें अतिरंजित करने की आवश्यकता नहीं है (अलग-अलग पैरामीटर के साथ लागू करने के लिए प्रस्तुत करने के लिए प्रस्तुत किया जाता है, जबकि यह कहता है कि ब्रॉउज़रएवेंट को बिल्कुल लागू करने की आवश्यकता नहीं है) । साथ ही, मुझे यकीन नहीं है कि "संदर्भ" वर्ग आयात करने के लिए कौन सा है। मुझे यकीन नहीं है कि यहां से कहां जाना है, कोई सलाह? –

+0

आप किस जीडब्ल्यूटी का उपयोग कर रहे हैं? जिस वर्ग को मैंने आपको संदर्भित किया है वह ट्रंक से है। यहां 2.1.1 संस्करण है: http://code.google.com/p/google-web-toolkit/source/browse/tags/2.1.1/user/src/com/google/gwt/cell/client/ SelectionCell.java?r=9485 –

+0

मैंने अपना संस्करण चेक किया और मैं अभी भी 2.1.0 चला रहा था। 2.1.1 तक अपग्रेड करने के बाद समस्या दूर हो गई। सब कुछ जिस तरह से मैं चाहता हूं काम कर रहा हूं। सहायता के लिए धनयवाद। –

संबंधित मुद्दे