Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Prepend an item to a DataSource bound SelectItem

    Hello. I'm trying to add a custom item to the data returned by the server for a picklist and I can't figure it out. Basically the dataSource provides a collection of options and I want to insert a new item at position 0 called "All".

    I'm trying to use dataArrived to intercept the data returned by the server. The last argument, data, is of type ResultSet and according to the documentation should implement the List interface, along with the add and addAt methods. However, that is not the case, calling those methods results in a Javascript error saying those methods are not defined.

    Code:
    dataArrived : function (startRow, endRow, data)
    {
    	data.addAt({name: "All", value:"All"}, 0);
    }
    Alternatively I tried using transformData on the data object, but the data is not modified in any way.

    Code:
    dataArrived : function (startRow, endRow, data)
    {
    	data.transformData = function (data, dsResponse) {
    		data.addAt({name: "All", value:"All"}, 0);
    		return data;
    	}
    }
    I figure there's also the problem of increasing the number of totalRows and the endRow so I might need some help with that too.

    Meanwhile, can anyone help me with this?

    Thanks

    #2
    This is built-in - set allowEmptyValue:true and set emptyDisplayValue to "All". We'll clarify/interlink the docs a bit more in this area.

    Comment


      #3
      Not able to use add or addAt on a resultSet

      Hi ,

      I have a similar issue, I need to perform addAt operation for a listgrid data returned by the server ( resultSet). But When I try to use add or addAt methods it
      results in a Javascript error saying those methods are not defined.
      I tried using

      List Grid Code :

      isc.ListGrid.create({
      ID:"dimensionsFeatureGridView",
      //autoDraw: false,
      dataSource: dimensionValuesDS,
      height:"100%",
      autoFetchData: false,
      showAllRecords:true,
      width:500,
      left:500,
      selectionType: "simple",
      canEdit:true,
      editByCell: true,
      modalEditing: true,
      allowEmptyValue:true,
      emptyDisplayValue:"All",
      dataProperties: {
      allowEmptyValue:true ,
      emptyDisplayValue:"All"

      },
      saveAllEdits:true

      })



      I tried using allowEmptyValue and emptyDisplayValue as listgrid fields as well as data Properties, but it didnt help me.

      Could you please tell me where I am going wrong.

      Comment


        #4
        Hi Kirti,

        Those properties (allowEmptyValue / emptyDisplayValue) are properties on FormItem / SelectItem, not on ListGrid.

        ResultSet.add() is not what you want - if it were there, you would expect it to save a new record to the server. Instead, you should provide an implementation of DataSource.transformResponse() that adds the dynamically generated record.

        If you are trying to use data paging, be aware of the possible consequences of generating records on the client - in this particular case, subsequent requests for data will be using an index one higher because of the record you inserted.

        Comment


          #5
          Hi,

          Isomorphic - fantastic work!

          I am trying to accomplish the same as the previous posters, and adding data dynamically on DS.transformResponse appears to be a workaround for ResultSet not implementing the List interface as advertised.

          I would like the DS to contain only data that is available on the server, and I'd like to be able to manipulate the ResultSet outside of the DS.

          I can appreciate there are reasons that the ResultSet may not be modified on the client (as Isomorphic indicated previously) but does that not imply the ResultSet should implement an interface other than List?

          My issue here is that the ResultSet claims to implement the List interface but clearly doesn't. Alternatively I may be interpreting the documentation/source code incorrectly.

          BTW, the reason I'd like the extra result prepended is to fix display issues in a ListGrid that isGroup:true, and I understand that my cause is a separate issue entirely.

          Thanks, Andrew

          SmartClient Version: 7.0rc2 (built 2009-05-30)
          System Version: Mac OS X 10.5.8 (9L31a)
          Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9

          Comment


            #6
            Using transformResponse() is not a workaround, it is a clean approach that makes sense with the stated use case of modifying the real response to add records that don't really exist. As previously indicated, if ResultSet.add() were available, you would expect it to add a new record like DataSource.addData(), which is not what you want.

            Yes, ResultSet should implement List. It's analogous to Java's read-only Collections.

            Comment


              #7
              We seem to be missing the point here.

              I am not disputing the behaviour, I'm pointing out that the API is misleading. You've just said ResultSet should implement list, but it doesn't - instead it raises an error.

              If result set does indeed implement List then it's quite fine to have a noop for the method, but as the interface implies that method should at least exist!

              Again, it's likely my interpretation is incorrect. Could you please clarify what the purpose of an interface is?

              A

              Comment


                #8
                .. and I can agree that transformResponse is the correct solution for the original issue.

                OK, so clearly I'm fairly new at JS : http://knol.google.com/k/programming-to-the-interface-in-javascript-yes-it-can-be-done-er-i-mean-faked

                http://forums.smartclient.com/showthread.php?t=973 touches on this exact topic and indicates that interfaces are not enforced, so I can only suggest that maybe the documentation could reflect this somehow (due to time taken to trawl the forum)?

                Alternatively there could be a set of "should read" documents that people like myself could be pointed to before wasting Isomorphic time?

                Thanks again for the awesome product,

                Andrew

                Comment


                  #9
                  It does implement List and all of the List methods are there. Think of the crash you are seeing as similar to the exception you get calling the wrong method on a read-only Collection in Java. The exception could be clearer or the docs call it out, but really, hundreds of methods link to transformResponse() as the way to do anything to a response..

                  Comment


                    #10
                    I'm having trouble buying that "It does implement List and all of the List methods are there".

                    In the List.js source file there is the following comment in the docblock for addAt:

                    expected to be implemented by target

                    In addition, I cannot find the addAt method when inspecting a ResultSet with firebug, or within the source for ResultsSet. The error message that is raised when attempting to use "add" on a ResultSet is:

                    TypeError: this.addAt is not a function source=with(_FirebugCommandLine){rs.add({});\n};

                    I have no trouble being wrong and would like to know how I can find this information for myself in the future.

                    Thanks, Andrew

                    Comment


                      #11
                      Originally posted by Isomorphic
                      Hi Kirti,

                      Those properties (allowEmptyValue / emptyDisplayValue) are properties on FormItem / SelectItem, not on ListGrid.

                      ResultSet.add() is not what you want - if it were there, you would expect it to save a new record to the server. Instead, you should provide an implementation of DataSource.transformResponse() that adds the dynamically generated record.

                      If you are trying to use data paging, be aware of the possible consequences of generating records on the client - in this particular case, subsequent requests for data will be using an index one higher because of the record you inserted.
                      Is there an example of using DataSource.transformResponse() to add new records? I can't find one and don't know how to go about it. I am having the same problem as the OP - needing to add records into a DS-bound ListGrid.

                      Thanks

                      Comment

                      Working...
                      X